]> git.kaiwu.me - njs.git/commitdiff
Stream: fixed CPU hog when js_filter is registered in both directions.
authorMiao Wang <shankerwangmiao@gmail.com>
Wed, 11 Aug 2021 03:44:12 +0000 (11:44 +0800)
committerMiao Wang <shankerwangmiao@gmail.com>
Wed, 11 Aug 2021 03:44:12 +0000 (11:44 +0800)
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

index bd62dd885abe316a0f2578a44cbab81eb8679e50..9a5db2eafb52f5ff99541d6a5efcf8622b1ab6cc 100644 (file)
@@ -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 {