roadrunner_compress (roadrunner v0.11.0)

View Source

Response 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-Encoding accepts gzip or deflate with a qvalue above 0 (RFC 9110 §12.5.3). When both have the same qvalue, gzip wins.
  • The response has no Content-Encoding header 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_static serves precompressed .br / .gz files instead of compressing on the fly.

Summary

Functions