aboutsummaryrefslogtreecommitdiff
path: root/nginx/ngx_http_js_module.c
diff options
context:
space:
mode:
authorDmitry Volyntsev <xeioex@nginx.com>2023-07-03 13:32:41 -0700
committerDmitry Volyntsev <xeioex@nginx.com>2023-07-03 13:32:41 -0700
commit1fe63aba30cd828c46cd12ea7057c9a2f58b45f6 (patch)
tree49fb4305dd33c31d93ef276bf701738e998598e4 /nginx/ngx_http_js_module.c
parent40b01ac098d7835263b1ab80bb040edc3d98b29b (diff)
downloadnjs-1fe63aba30cd828c46cd12ea7057c9a2f58b45f6.tar.gz
njs-1fe63aba30cd828c46cd12ea7057c9a2f58b45f6.zip
Modules: introduced js_shared_dict_zone directive.
The directive allows to declare a dictionary that is shared among the working processes. A dictionary expects strings as keys. It stores string or number as values. The value type is declared using type= argument of the directive. The default type is string. example.conf: # Declares a shared dictionary of strings of size 1 Mb that # removes key-value after 60 seconds of inactivity. js_shared_dict_zone zone=foo:1M timeout=60s; # Declares a shared dictionary of strings of size 512Kb that # forcibly remove oldest key-value pairs when memory is not enough. js_shared_dict_zone zone=bar:512K timeout=30s evict; # Declares a permanent number shared dictionary of size 32Kb. js_shared_dict_zone zone=num:32k type=number; example.js: function get(r) { r.return(200, ngx.shared.foo.get(r.args.key)); } function set(r) { r.return(200, ngx.shared.foo.set(r.args.key, r.args.value)); } function delete(r) { r.return(200, ngx.shared.bar.delete(r.args.key)); } function increment(r) { r.return(200, ngx.shared.num.incr(r.args.key, 2)); } In collaboration with Artem S. Povalyukhin, Jakub Jirutka and 洪志道 (Hong Zhi Dao). This closes #437 issue on Github.
Diffstat (limited to 'nginx/ngx_http_js_module.c')
-rw-r--r--nginx/ngx_http_js_module.c48
1 files changed, 46 insertions, 2 deletions
diff --git a/nginx/ngx_http_js_module.c b/nginx/ngx_http_js_module.c
index df3a0e5a..5f6c73da 100644
--- a/nginx/ngx_http_js_module.c
+++ b/nginx/ngx_http_js_module.c
@@ -252,10 +252,13 @@ static char *ngx_http_js_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_js_var(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_js_content(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
+static char *ngx_http_js_shared_dict_zone(ngx_conf_t *cf, ngx_command_t *cmd,
+ void *conf);
static char *ngx_http_js_body_filter_set(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static ngx_int_t ngx_http_js_init_conf_vm(ngx_conf_t *cf,
ngx_js_loc_conf_t *conf);
+static void *ngx_http_js_create_main_conf(ngx_conf_t *cf);
static void *ngx_http_js_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_js_merge_loc_conf(ngx_conf_t *cf, void *parent,
void *child);
@@ -393,6 +396,13 @@ static ngx_command_t ngx_http_js_commands[] = {
#endif
+ { ngx_string("js_shared_dict_zone"),
+ NGX_HTTP_MAIN_CONF|NGX_CONF_1MORE,
+ ngx_http_js_shared_dict_zone,
+ 0,
+ 0,
+ NULL },
+
ngx_null_command
};
@@ -401,7 +411,7 @@ static ngx_http_module_t ngx_http_js_module_ctx = {
NULL, /* preconfiguration */
ngx_http_js_init, /* postconfiguration */
- NULL, /* create main configuration */
+ ngx_http_js_create_main_conf, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
@@ -775,6 +785,7 @@ static uintptr_t ngx_http_js_uptr[] = {
(uintptr_t) ngx_http_js_fetch_timeout,
(uintptr_t) ngx_http_js_buffer_size,
(uintptr_t) ngx_http_js_max_response_buffer_size,
+ (uintptr_t) 0 /* main_conf ptr */,
};
@@ -798,6 +809,7 @@ njs_module_t *njs_http_js_addon_modules[] = {
*/
&ngx_js_ngx_module,
&ngx_js_fetch_module,
+ &ngx_js_shared_dict_module,
#ifdef NJS_HAVE_OPENSSL
&njs_webcrypto_module,
#endif
@@ -4149,10 +4161,14 @@ ngx_js_http_init(njs_vm_t *vm)
static ngx_int_t
ngx_http_js_init_conf_vm(ngx_conf_t *cf, ngx_js_loc_conf_t *conf)
{
- njs_vm_opt_t options;
+ njs_vm_opt_t options;
+ ngx_js_main_conf_t *jmcf;
njs_vm_opt_init(&options);
+ jmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_js_module);
+ ngx_http_js_uptr[NGX_JS_MAIN_CONF_INDEX] = (uintptr_t) jmcf;
+
options.backtrace = 1;
options.unhandled_rejection = NJS_VM_OPT_UNHANDLED_REJECTION_THROW;
options.ops = &ngx_http_js_ops;
@@ -4293,6 +4309,14 @@ ngx_http_js_content(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
static char *
+ngx_http_js_shared_dict_zone(ngx_conf_t *cf, ngx_command_t *cmd,
+ void *conf)
+{
+ return ngx_js_shared_dict_zone(cf, cmd, conf, &ngx_http_js_module);
+}
+
+
+static char *
ngx_http_js_body_filter_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_js_loc_conf_t *jlcf = conf;
@@ -4331,6 +4355,26 @@ ngx_http_js_body_filter_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
static void *
+ngx_http_js_create_main_conf(ngx_conf_t *cf)
+{
+ ngx_js_main_conf_t *jmcf;
+
+ jmcf = ngx_pcalloc(cf->pool, sizeof(ngx_js_main_conf_t));
+ if (jmcf == NULL) {
+ return NULL;
+ }
+
+ /*
+ * set by ngx_pcalloc():
+ *
+ * jmcf->dicts = NULL;
+ */
+
+ return jmcf;
+}
+
+
+static void *
ngx_http_js_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_js_loc_conf_t *conf =