streaming and backend_timeout: what buffering really costs, and the gap streaming leaves
Was this useful?
0 unique visitors
A proxy that buffers responses is simple and safe: it has the whole body
in hand, knows its length, and can rewrite it. It is also, for some
workloads, a memory problem waiting to happen. ProxyAuth 1.2.5 added
streaming for those workloads, and backend_timeout alongside it.
The two are best understood together.
The cost of buffering
The memory a buffered proxy needs is the size of the response multiplied by the number of concurrent requests. For HTML pages and JSON APIs, that is negligible. For a git forge serving a 300 MB packfile to ten clients at once, it is 3 GB resident for as long as the transfers run.
Buffering also changes what the client sees: nothing, then everything. Time to first byte equals the duration of the whole transfer. And for open-ended responses — server-sent events, long-polling — there is no “whole” to wait for, so a buffered route simply never answers.
Turning it on
vhosts:
- vhost: ["git.example.com"]
streaming: true
routes:
- prefix: "/"
target: "http://127.0.0.1:3000"
streaming can be set on a vhosts: group and inherited by every route
under it, or on a single route. It defaults to false.
| Aspect | Buffered (default) | Streamed |
|---|---|---|
| memory per request | the whole response | one chunk |
| time to first byte | end of transfer | immediate |
| SSE / long-polling | never completes | works |
| framing | known Content-Length | chunked |
When ProxyAuth buffers anyway
Three features rewrite the response body and therefore need to see it whole:
tag_proxyauthsubstitutes{{ username }},{{ csrf_token }}and friends anywhere in the document.hidden_blocksfinds an opening tag and its matching close, which can be arbitrarily far apart.- CSRF injection rewrites forms in returned HTML and recomputes the content length.
When any of them is active on a route, that route buffers regardless of
streaming, and a warning naming the route is logged at startup.
Silently dropping a substitution an operator explicitly asked for would
be the worse failure.
In practice the split is clean. HTML served to a browser is small and often wants rewriting. Git traffic, package downloads, API responses and event streams want neither rewriting nor buffering. If you have both on one hostname, put them on separate routes:
vhosts:
- vhost: ["forge.example.com"]
routes:
- prefix: "/"
target: "http://127.0.0.1:3000"
tag_proxyauth: true # buffered: pages get rewritten
- regex: '^/[^/]+/[^/]+\.git/.*$'
prefix: "/git"
target: "http://127.0.0.1:3000"
streaming: true # streamed: packfiles pass through
Request bodies are a different story
streaming concerns the response. Request bodies are still read in
full before the handler runs, bounded by max_body_size (10 MB by
default). That matters for a large git push, not for a clone. Raise
max_body_size if your pushes are big; it is a global setting.
backend_timeout
{ "backend_timeout": 30000 }
How long a proxied request may take before ProxyAuth abandons it, in
milliseconds, default 10000. It applies to both proxy paths and to a
redirect_protect.target fetch. The short probe behind
redirect_protect.paths keeps its own much tighter budget: a session
check that runs ahead of every matching request has no business
inheriting the budget of a large transfer.
0 is restored to the default rather than taken literally. A
zero-length timeout would abandon every request the instant it is
issued, which is never what anyone means — even though 0 does mean
“off” for rate limits and refresh intervals elsewhere.
One setting, two meanings
What backend_timeout bounds depends on the route’s mode:
- Buffered — the whole exchange. The response is collected in full before anything is sent, so the timeout covers headers and body.
- Streamed — only the arrival of the response headers. The body then flows for as long as it takes, which is the entire point of streaming a large download.
The gap this leaves
On a streamed route, a backend that sends its headers and then stalls holds the connection open indefinitely. Nothing bounds the body transfer: an idle timeout between frames would close that gap, and it does not exist yet.
For a backend you operate, that is usually acceptable. For a streamed
route pointing at something you do not control, it is worth knowing
before you enable it: a slow or hostile upstream can pin connections.
max_connections bounds the damage at the process level, but it is not
a substitute for a per-frame deadline.
A related fix: git clone and Content-Encoding
One more detail that surfaced while putting a git forge behind
ProxyAuth: git gzips its upload-pack request. The body reaching the
backend has already been decoded, so forwarding the client’s
Content-Encoding header would announce an encoding the bytes no longer
carry, and the backend would fail to decode them. ProxyAuth therefore
never forwards the client’s Content-Encoding. That is what made
git clone work through the proxy.
Checklist for a git forge or a registry
vhosts:
- vhost: ["git.example.com"]
streaming: true
forward_proxy_headers: true
routes:
- prefix: "/"
target: "http://127.0.0.1:3000"
cache: false
{
"backend_timeout": 30000,
"max_body_size": 524288000
}
And keep tag_proxyauth, hidden_blocks and CSRF injection off the
routes that carry packfiles, or they will quietly buffer again — the
startup log tells you which.
Buffer what you rewrite, stream what you relay. And remember that on a streamed route the timeout watches the headers, not the body — a deliberate trade that makes large transfers possible and leaves one known gap worth planning around.