Add compression strategy parameter to enable_compression method #5909
Closed
Description
Is your feature request related to a problem?
We use aiohttp server and StreamResponse.enable_compression() method for compression such data like images.
And under high load (when we downloading a lot of data by multiple clients) response.write() hangs with 100% CPU load.
After changing this line of code: https://github.com/aio-libs/aiohttp/blob/master/aiohttp/http_writer.py#L66
from self._compress = zlib.compressobj(wbits=zlib_mode) to self._compress = zlib.compressobj(wbits=zlib_mode, strategy=zlib.Z_HUFFMAN_ONLY) the problem disappeared, CPU load decreased and response.write() didn't hang.
Describe the solution you'd like
Change this method from:
def enable_compression(self, encoding: str = "deflate") -> None:
zlib_mode = 16 + zlib.MAX_WBITS if encoding == "gzip" else zlib.MAX_WBITS
self._compress = zlib.compressobj(wbits=zlib_mode)
to kind of that
def enable_compression(self, encoding: str = "deflate", compression_strategy: str = "Z_DEFAULT_STRATEGY") -> None:
zlib_mode = 16 + zlib.MAX_WBITS if encoding == "gzip" else zlib.MAX_WBITS
self._compress = zlib.compressobj(wbits=zlib_mode, strategy=eval(f"zlib.{compression_strategy}"))
Describe alternatives you've considered
Have no idea about alternatives
Related component
Server
Additional context
No response
Code of Conduct
- I agree to follow the aio-libs Code of Conduct