actix-web icon indicating copy to clipboard operation
actix-web copied to clipboard

Conditional Compression

Open ranger-ross opened this issue 2 years ago • 0 comments
trafficstars

Current Behavior

Currently, enabling the compression middleware will enable it for all responses. This is a good default but can lead to wasted compression computation if the response is already very small. This happens when the cost to compress response data is more expensive that simply sending it uncompressed.

Possible Solution

I think Spring has a very nice way of handling this issue: In Spring responses are only compressed if they exceed a threshold size (in bytes). By default in Spring, the threshold is 2KB but is overridable.

Considering the changes in https://github.com/actix/actix-web/pull/2948 I think we can can extend that pattern like so

actix_web::middleware::Compress::new()
     .min_response_size(2048)
     .... // other options

ranger-ross avatar May 07 '23 14:05 ranger-ross