[njs] Modules: introduced ngx.worker_id.

Dmitry Volyntsev xeioex at nginx.com
Fri Jun 30 03:46:59 UTC 2023


details:   https://hg.nginx.org/njs/rev/453f5503de13
branches:  
changeset: 2169:453f5503de13
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Jun 29 20:44:14 2023 -0700
description:
Modules: introduced ngx.worker_id.

worker_id corresponds to an nginx internal worker id.
The value is between 0 and worker_processes - 1.

diffstat:

 nginx/ngx_js.c   |  20 ++++++++++++++++++++
 ts/ngx_core.d.ts |   6 ++++++
 2 files changed, 26 insertions(+), 0 deletions(-)

diffs (60 lines):

diff -r 7aa15ae3d8be -r 453f5503de13 nginx/ngx_js.c
--- a/nginx/ngx_js.c	Thu Jun 29 18:58:45 2023 -0700
+++ b/nginx/ngx_js.c	Thu Jun 29 20:44:14 2023 -0700
@@ -25,6 +25,8 @@ static njs_int_t ngx_js_ext_prefix(njs_v
     njs_value_t *value, njs_value_t *setval, njs_value_t *retval);
 static njs_int_t ngx_js_ext_version(njs_vm_t *vm, njs_object_prop_t *prop,
     njs_value_t *value, njs_value_t *setval, njs_value_t *retval);
+static njs_int_t ngx_js_ext_worker_id(njs_vm_t *vm, njs_object_prop_t *prop,
+    njs_value_t *value, njs_value_t *setval, njs_value_t *retval);
 static void ngx_js_cleanup_vm(void *data);
 
 static njs_int_t ngx_js_core_init(njs_vm_t *vm);
@@ -149,6 +151,15 @@ static njs_external_t  ngx_js_ext_core[]
         }
     },
 
+    {
+        .flags = NJS_EXTERN_PROPERTY,
+        .name.string = njs_str("worker_id"),
+        .enumerable = 1,
+        .u.property = {
+            .handler = ngx_js_ext_worker_id,
+        }
+    },
+
 };
 
 
@@ -453,6 +464,15 @@ ngx_js_ext_version(njs_vm_t *vm, njs_obj
 
 
 njs_int_t
+ngx_js_ext_worker_id(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value,
+    njs_value_t *setval, njs_value_t *retval)
+{
+    njs_value_number_set(retval, ngx_worker);
+    return NJS_OK;
+}
+
+
+njs_int_t
 ngx_js_ext_log(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t level, njs_value_t *retval)
 {
diff -r 7aa15ae3d8be -r 453f5503de13 ts/ngx_core.d.ts
--- a/ts/ngx_core.d.ts	Thu Jun 29 18:58:45 2023 -0700
+++ b/ts/ngx_core.d.ts	Thu Jun 29 20:44:14 2023 -0700
@@ -311,6 +311,12 @@ interface NgxObject {
      * @since 0.5.1
      */
     readonly WARN: number;
+    /**
+     * A number corresponding to the current worker process id.
+     * Can have values from 0 to worker_processes - 1.
+     * @since 0.8.0
+     */
+    readonly worker_id: number;
 
 }
 


More information about the nginx-devel mailing list