Skip to content

Commit

Permalink
Override default brotli compression level 11 -> 4
Browse files Browse the repository at this point in the history
The `brotli` crate used by `async-compression` has a default compression level of 11, which is the maximum for brotli.  This causes extremely slow compression performance for many response bodies and is definitely an inappropriate compression level for dynamic content.

There's currently an open issue (dropbox/rust-brotli#93) on the `brotli` crate's repo to change this default, but it hasn't happened at this time.

This change adds a special case to convert a provided compression level of default to a compression level of 4, which is a reasonable default for dynamic content.
  • Loading branch information
Ameobea authored and jplatte committed Jul 19, 2023
1 parent 77b34f8 commit 9381c9a
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion tower-http/src/compression/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,16 @@ where
type Output = BrotliEncoder<Self::Input>;

fn apply(input: Self::Input, quality: CompressionLevel) -> Self::Output {
BrotliEncoder::with_quality(input, quality.into_async_compression())
// The brotli crate used under the hood here has a default compression level of 11,
// which is the max for brotli. This causes extremely slow compression times, so we
// manually set a default of 4 here.
//
// This is the same default used by NGINX for on-the-fly brotli compression.
let level = match quality {
CompressionLevel::Default => async_compression::Level::Precise(4),
other => other.into_async_compression(),
};
BrotliEncoder::with_quality(input, level)
}

fn get_pin_mut(pinned: Pin<&mut Self::Output>) -> Pin<&mut Self::Input> {
Expand Down

0 comments on commit 9381c9a

Please sign in to comment.