brotli icon indicating copy to clipboard operation
brotli copied to clipboard

Add HTTPCompressorWithLevels to customize compression levels

Open klondikedragon opened this issue 1 year ago • 5 comments

Also add HTTPCompressorWithCustom that take factories.

Solves https://github.com/andybalholm/brotli/issues/55

klondikedragon avatar Dec 07 '24 06:12 klondikedragon

I'd rather not add the WithCustom function. If the user wants something truly custom, he can always handle the header negotiation himself…

andybalholm avatar Dec 09 '24 17:12 andybalholm

The reason I added that was because in my case I wanted to use the V1 brotli compressor instead of the V2 compressor. (Based on the docs, it sounds like currently the V2 compressor is expected to achieve better results for compression levels >= 2, and since I needed best speed it was unclear if using the V2 compressor was going to be optimal.) All of the other funcs are not public, so re-implementing all of that logic (including detecting whether to use brotli or gzip) would be a waste when it's well done in this package.

Alternatively, another parameter could be added to choose V1 vs V2 compressor, but that seemed to pollute the WithLevel interface a bit. Also, if someone wants to customize other parameters like window size, etc., it would be easier done using a WithCustom interface. What do you think?

klondikedragon avatar Dec 11 '24 22:12 klondikedragon

If someone wants to customize as far as window size, they should be doing the compression themselves instead of using a convenience function.

But you could definitely make the WithLevels function use V1 for some levels and V2 for others.

andybalholm avatar Dec 11 '24 22:12 andybalholm

Instead of adding a WithCustom function, would you be open instead to making negotiateContentEncoding a public function? There is a lot of useful logic behind that function, and that would give a caller all the tools needed to do whatever custom things they wanted to create their own custom Brotli compressor with the right options, but still not have to replicate all of the logic contained in NegotiateContentEncoding, etc.

klondikedragon avatar Dec 12 '24 19:12 klondikedragon

NegotiateContentEncoding already is a public function, in github.com/golang/gddo. I copied it into the brotli package to avoid pulling in a large dependency.

andybalholm avatar Dec 12 '24 22:12 andybalholm

I've added HTTPCompressorWithLevel, which has one level parameter, used for both brotli and gzip.

andybalholm avatar Aug 21 '25 18:08 andybalholm