[njs] Stream: improved vm events handling.

Dmitry Volyntsev xeioex at nginx.com
Wed Nov 25 11:09:21 UTC 2020


details:   https://hg.nginx.org/njs/rev/0684301385d9
branches:  
changeset: 1570:0684301385d9
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed Nov 25 10:47:25 2020 +0000
description:
Stream: improved vm events handling.

diffstat:

 nginx/ngx_stream_js_module.c |  54 ++++++++++++++++++++++---------------------
 1 files changed, 28 insertions(+), 26 deletions(-)

diffs (126 lines):

diff -r e51da8c71f26 -r 0684301385d9 nginx/ngx_stream_js_module.c
--- a/nginx/ngx_stream_js_module.c	Fri Nov 20 12:29:30 2020 +0300
+++ b/nginx/ngx_stream_js_module.c	Wed Nov 25 10:47:25 2020 +0000
@@ -48,8 +48,10 @@ typedef struct {
     ngx_chain_t            *busy;
     ngx_stream_session_t   *session;
     ngx_int_t               status;
-    njs_vm_event_t          upload_event;
-    njs_vm_event_t          download_event;
+#define NGX_JS_EVENT_UPLOAD   0
+#define NGX_JS_EVENT_DOWNLOAD 1
+#define NGX_JS_EVENT_MAX      2
+    njs_vm_event_t          events[2];
     unsigned                from_upstream:1;
     unsigned                filter:1;
     unsigned                in_progress:1;
@@ -73,6 +75,7 @@ static ngx_int_t ngx_stream_js_body_filt
 static ngx_int_t ngx_stream_js_variable(ngx_stream_session_t *s,
     ngx_stream_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_stream_js_init_vm(ngx_stream_session_t *s);
+static void ngx_stream_js_drop_events(ngx_stream_js_ctx_t *ctx);
 static void ngx_stream_js_cleanup_ctx(void *data);
 static void ngx_stream_js_cleanup_vm(void *data);
 static njs_int_t ngx_stream_js_buffer_arg(ngx_stream_session_t *s,
@@ -441,7 +444,7 @@ ngx_stream_js_phase_handler(ngx_stream_s
         }
     }
 
-    if (ctx->upload_event != NULL) {
+    if (ctx->events[NGX_JS_EVENT_UPLOAD] != NULL) {
         ret = ngx_stream_js_buffer_arg(s, njs_value_arg(&ctx->args[1]));
         if (ret != NJS_OK) {
             goto exception;
@@ -452,7 +455,7 @@ ngx_stream_js_phase_handler(ngx_stream_s
             goto exception;
         }
 
-        njs_vm_post_event(ctx->vm, ctx->upload_event,
+        njs_vm_post_event(ctx->vm, ctx->events[NGX_JS_EVENT_UPLOAD],
                           njs_value_arg(&ctx->args[1]), 2);
 
         rc = njs_vm_run(ctx->vm);
@@ -463,7 +466,7 @@ ngx_stream_js_phase_handler(ngx_stream_s
 
     if (njs_vm_pending(ctx->vm)) {
         ctx->in_progress = 1;
-        rc = ctx->upload_event ? NGX_AGAIN : NGX_DONE;
+        rc = ctx->events[NGX_JS_EVENT_UPLOAD] ? NGX_AGAIN : NGX_DONE;
 
     } else {
         ctx->in_progress = 0;
@@ -487,7 +490,8 @@ exception:
 
 
 #define ngx_stream_event(from_upstream)                                 \
-    (from_upstream ? ctx->download_event : ctx->upload_event)
+    (from_upstream ? ctx->events[NGX_JS_EVENT_DOWNLOAD]                 \
+                   : ctx->events[NGX_JS_EVENT_UPLOAD])
 
 
 static ngx_int_t
@@ -720,19 +724,25 @@ ngx_stream_js_init_vm(ngx_stream_session
 
 
 static void
+ngx_stream_js_drop_events(ngx_stream_js_ctx_t *ctx)
+{
+    ngx_uint_t  i;
+
+    for (i = 0; i < NGX_JS_EVENT_MAX; i++) {
+        if (ctx->events[i] != NULL) {
+            njs_vm_del_event(ctx->vm, ctx->events[i]);
+            ctx->events[i] = NULL;
+        }
+    }
+}
+
+
+static void
 ngx_stream_js_cleanup_ctx(void *data)
 {
     ngx_stream_js_ctx_t *ctx = data;
 
-    if (ctx->upload_event != NULL) {
-        njs_vm_del_event(ctx->vm, ctx->upload_event);
-        ctx->upload_event = NULL;
-    }
-
-    if (ctx->download_event != NULL) {
-        njs_vm_del_event(ctx->vm, ctx->download_event);
-        ctx->download_event = NULL;
-    }
+    ngx_stream_js_drop_events(ctx);
 
     if (njs_vm_pending(ctx->vm)) {
         ngx_log_error(NGX_LOG_ERR, ctx->log, 0, "pending events");
@@ -840,10 +850,10 @@ ngx_stream_js_event(ngx_stream_session_t
     }
 
     if (i == 0) {
-        return &ctx->upload_event;
+        return &ctx->events[NGX_JS_EVENT_UPLOAD];
     }
 
-    return &ctx->download_event;
+    return &ctx->events[NGX_JS_EVENT_DOWNLOAD];
 }
 
 
@@ -911,15 +921,7 @@ ngx_stream_js_ext_done(njs_vm_t *vm, njs
 
     ctx->status = status;
 
-    if (ctx->upload_event != NULL) {
-        njs_vm_del_event(ctx->vm, ctx->upload_event);
-        ctx->upload_event = NULL;
-    }
-
-    if (ctx->download_event != NULL) {
-        njs_vm_del_event(ctx->vm, ctx->download_event);
-        ctx->download_event = NULL;
-    }
+    ngx_stream_js_drop_events(ctx);
 
     njs_value_undefined_set(njs_vm_retval(vm));
 


More information about the nginx-devel mailing list