Skip to content

fix(encoding): flush codec after each chunk for streaming responses #4055

Closed
Rachit2323 wants to merge 3 commits intoactix:mainfrom
Rachit2323:fix/compress-middleware-streaming-flush
Closed

fix(encoding): flush codec after each chunk for streaming responses #4055
Rachit2323 wants to merge 3 commits intoactix:mainfrom
Rachit2323:fix/compress-middleware-streaming-flush

Conversation

@Rachit2323
Copy link
Copy Markdown

Fixes #3410

Problem

When using middleware::Compress with a streaming response, chunks don't
arrive at the client progressively — instead everything is buffered and
delivered all at once when the stream ends.

For example, a response that sends one chunk immediately and another after
5 seconds will show nothing for 5 seconds, then both chunks appear at the
same time.

Why it happens

The gzip/deflate/zstd codecs buffer incoming data internally waiting for
more bytes to compress efficiently. write() feeds data in but the codec
decides when to emit output. Without an explicit flush, it holds everything
until finish() is called at stream EOF.

Fix

Added ContentEncoder::flush() which calls sync flush on the underlying
codec, and call it after every write() in poll_next. This forces the
codec to release each chunk immediately rather than waiting for the stream
to end.

Small trade-off: slightly lower compression ratio per chunk, but this is
the same behaviour as nginx/Apache and the right call for streaming.

…ithout an explicit flush, gzip/deflate/zstd buffers all chunks and releases them only at stream EOF, causing middleware::Compress to delay all output until the response stream ends. Fixes actix#3410
@github-actions github-actions Bot added A-http project: actix-http A-web project: actix-web labels May 3, 2026
@JohnTitor
Copy link
Copy Markdown
Member

Please do not paste AI output directly, we don't welcome slops.

@JohnTitor JohnTitor closed this May 4, 2026
@actix actix locked as spam and limited conversation to collaborators May 4, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

A-http project: actix-http A-web project: actix-web

Projects

None yet

Development

Successfully merging this pull request may close these issues.

middleware::Compress seems to be buffering content indefinitely

2 participants