How it works
The module is a Rust crate built with ngx-rust. It drives the mbrotli encoder from NGINX’s filter chain; the bytes the encoder writes are mbrotli’s concern, and changes to them belong in that repository.
Place in the filter chain
The filter sits in the same place in the filter chain as
google/ngx_brotli’s, just above the gzip filter. The dynamic library
exports an ngx_module_order table so that NGINX inserts it there.
C bit-fields
All access to C bit-fields (request, buffer, variable and open-file flags) goes
through src/bitfields.c, compiled against the same headers as NGINX. The bit-field accessors that bindgen generates for ngx_http_request_t
do not match the C compiler’s layout in NGINX 1.30 (they are two bits off from
uri_changes onwards), so they cannot be used safely.
The body filter
The body filter (src/body.rs) reaches NGINX only through a small Host
trait. In production the trait is implemented over NGINX’s buffers and chains;
in tests it is a model of that API (src/body/tests.rs), so the filter’s logic
runs, under Miri too, without NGINX. See Testing.
Thread pool tasks
With brotli_thread_pool, each segment becomes
an ngx_thread_task_t. Its completion handler runs in the event loop and calls
the brotli filter to send what is ready, then runs the connection’s write
handler, as a write event would.
While tasks are posted, the request is blocked (r->main->blocked), so that
terminating it waits for them, and stays buffered, so that it is not finalized
before the last completion handler has run, even when the response has already
been sent.
See Compressing on a thread pool for what this means for configuration.