From beba525d01e12b2165a7c98c4353f8f2dd2ab778 Mon Sep 17 00:00:00 2001 From: Miao Wang Date: Wed, 11 Aug 2021 11:44:12 +0800 Subject: [PATCH] Stream: fixed CPU hog when js_filter is registered in both directions. Previously, a single busy chain was used to track filtered data in both directions. This might lead to a situation when busy chunks are not freed properly and pile up. The fix is to separate busy chain for upstream and downstream directions. This closes #413 issue on Github. --- nginx/ngx_stream_js_module.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/nginx/ngx_stream_js_module.c b/nginx/ngx_stream_js_module.c index bd62dd88..9a5db2ea 100644 --- a/nginx/ngx_stream_js_module.c +++ b/nginx/ngx_stream_js_module.c @@ -49,7 +49,8 @@ typedef struct { ngx_buf_t *buf; ngx_chain_t **last_out; ngx_chain_t *free; - ngx_chain_t *busy; + ngx_chain_t *upstream_busy; + ngx_chain_t *downstream_busy; ngx_int_t status; #define NGX_JS_EVENT_UPLOAD 0 #define NGX_JS_EVENT_DOWNLOAD 1 @@ -528,7 +529,7 @@ ngx_stream_js_body_filter(ngx_stream_session_t *s, ngx_chain_t *in, njs_str_t exception; njs_int_t ret; ngx_int_t rc; - ngx_chain_t *out, *cl; + ngx_chain_t *out, *cl, **busy; ngx_connection_t *c, *dst; ngx_stream_js_ev_t *event; ngx_stream_js_ctx_t *ctx; @@ -606,15 +607,17 @@ ngx_stream_js_body_filter(ngx_stream_session_t *s, ngx_chain_t *in, if (from_upstream) { dst = c; + busy = &ctx->downstream_busy; } else { dst = s->upstream ? s->upstream->peer.connection : NULL; + busy = &ctx->upstream_busy; } if (out != NULL || dst == NULL || dst->buffered) { rc = ngx_stream_next_filter(s, out, from_upstream); - ngx_chain_update_chains(c->pool, &ctx->free, &ctx->busy, &out, + ngx_chain_update_chains(c->pool, &ctx->free, busy, &out, (ngx_buf_tag_t) &ngx_stream_js_module); } else { -- 2.47.3