From bcceaab88e555f686d5ed39dfb238f898df2788c Mon Sep 17 00:00:00 2001 From: Desuwa Date: Thu, 16 Apr 2020 03:03:15 -0700 Subject: [PATCH] Prevent spinning when progress is not being made. (#100) Fixes #80 at least for my examples. --- filter/ngx_http_brotli_filter_module.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/filter/ngx_http_brotli_filter_module.c b/filter/ngx_http_brotli_filter_module.c index 0cfa16a..592b767 100644 --- a/filter/ngx_http_brotli_filter_module.c +++ b/filter/ngx_http_brotli_filter_module.c @@ -336,6 +336,7 @@ static ngx_int_t ngx_http_brotli_body_filter(ngx_http_request_t* r, int rc; ngx_http_brotli_ctx_t* ctx; size_t available_output; + ptrdiff_t available_busy_output; size_t input_size; size_t available_input; const uint8_t* next_input_byte; @@ -375,6 +376,12 @@ static ngx_int_t ngx_http_brotli_body_filter(ngx_http_request_t* r, - if there is more input - push it to encoder */ for (;;) { if (ctx->output_busy || ctx->output_ready) { + if (ctx->output_busy) { + available_busy_output = ngx_buf_size(ctx->out_buf); + } else { + available_busy_output = 0; + } + rc = ngx_http_next_body_filter(r, ctx->output_ready ? ctx->out_chain : NULL); if (ctx->output_ready) { @@ -385,6 +392,11 @@ static ngx_int_t ngx_http_brotli_body_filter(ngx_http_request_t* r, ctx->output_busy = 0; } if (rc == NGX_OK) { + if (ctx->output_busy && + available_busy_output == ngx_buf_size(ctx->out_buf)) { + r->connection->buffered |= NGX_HTTP_BROTLI_BUFFERED; + return NGX_AGAIN; + } continue; } else if (rc == NGX_AGAIN) { if (ctx->output_busy) {