amphp / http-server

An advanced async HTTP server library for PHP, perfect for real-time apps and APIs with high concurrency demands.
https://amphp.org/http-server
MIT License
1.29k stars 100 forks source link

Brotli compression in addition to gzip #337

Closed attisan closed 1 year ago

attisan commented 2 years ago

Brotli provides significantly smaller payload sizes and thus would / could further improve response speed. Could be integrated optionally in case https://github.com/kjdev/php-ext-brotli is detected?

kelunik commented 2 years ago

Sounds like a good addition, so you want to provide a small PR?

attisan commented 2 years ago

I'm on it. Should I Add it to CompressionMiddleware or stack a new BrotliCompressionMiddleware in in case Brotli is available? Doing the latter would keep the CompressionMiddleware a bit more readable as we need to use brotli_compress_init instead of deflate_init (and friends).

trowski commented 2 years ago

I think it makes sense to roll support into CompressionMiddleware. You'll probably want to split out deflate and brotli into separate handler methods and defer to them to keep it clean and readable.

attisan commented 2 years ago

Will do that - just a small side note: compared to gzip, brotli does take a considerable amount of overhead (ballpark of 50-100ms). I have achieved best performance with disabling amphp http-server compression (completely) and reverse proxying through nginx with enabled compression.

Imo: having brotli available in the amphp http-server still has its validity though, as letting nginx handle the compression does outperform amphp http-server with either compressions algorithms.

trowski commented 2 years ago

Yes, it's certainly nice to have available. I wonder though if in v3 we should default to compression being disabled? Probably doesn't matter too much, as named args make disabling compression trivial (i.e., new SocketHttpServer($logger, enableCompression: false))

attisan commented 2 years ago

should there be a setting indicating if Brotli should be used? I'm undecided on whether Brotli should outweigh gzip if it is available on the server as it does slow down the response.

bwoebi commented 2 years ago

I wouldn't prefer Brotli by default if it has significant overhead.

kelunik commented 1 year ago

I'm closing this. If the mentioned overhead is true, this won't be beneficial for application server requests. Static resources should usually be served by a load balancer / CDN in front, not by the application server directly.