roadrunner_compress (roadrunner v0.11.0)
View SourceResponse compression middleware. Compresses response bodies with gzip or
deflate (RFC 9110 §8.4.1), chosen from the request's Accept-Encoding.
Add it to a listener's middlewares (or a map-form route's middlewares).
It takes no config:
roadrunner:start_listener(my_app, #{
port => 8080,
routes => Routes,
middlewares => [roadrunner_compress]
}).When a response is compressed
A buffered {Status, Headers, Body} response is compressed when all of
these hold:
Accept-Encodingacceptsgzipordeflatewith a qvalue above 0 (RFC 9110 §12.5.3). When both have the same qvalue,gzipwins.- The response has no
Content-Encodingheader yet. - The body is at least 860 bytes. Below that, the compression overhead outweighs the saving.
A compressed response gets a Content-Encoding header, and its
Content-Length is set to the compressed size.
A {stream, ...} response is compressed chunk by chunk whenever an
encoding is negotiated and it has no Content-Encoding yet, whatever its
size. Trailers pass through unchanged.
Every response without its own Content-Encoding gets
Vary: Accept-Encoding, compressed or not, so caches keep the variants
apart (RFC 9110 §12.5.5).
Not covered
{loop, ...},{sendfile, ...}, and{websocket, ...}responses pass through unchanged.- Brotli (
br) is not supported. - For static files,
roadrunner_staticserves precompressed.br/.gzfiles instead of compressing on the fly.
Summary
Functions
-spec call(roadrunner_req:request(), roadrunner_middleware:next(), roadrunner_middleware:state()) -> roadrunner_handler:result().