From 0d4dda71915cac0014954de317aa86d62e4f4c7f Mon Sep 17 00:00:00 2001 From: alonbg Date: Wed, 21 Sep 2016 13:25:10 +0300 Subject: [PATCH] find http_lua_module by name for shared dict --- src/ngx_stream_lua_shdict.c | 85 +++++++++++++++++++++++++++++++++---- src/ngx_stream_lua_shdict.h | 4 +- src/ngx_stream_lua_util.c | 2 +- 3 files changed, 80 insertions(+), 11 deletions(-) diff --git a/src/ngx_stream_lua_shdict.c b/src/ngx_stream_lua_shdict.c index 99472ce4..1eb999e1 100644 --- a/src/ngx_stream_lua_shdict.c +++ b/src/ngx_stream_lua_shdict.c @@ -306,14 +306,79 @@ ngx_stream_lua_shdict_expire(ngx_stream_lua_shdict_ctx_t *ctx, ngx_uint_t n) void -ngx_stream_lua_inject_shdict_api(ngx_stream_lua_main_conf_t *lmcf, lua_State *L) +ngx_stream_lua_inject_shdict_api(ngx_log_t *log, + ngx_stream_lua_main_conf_t *lmcf, lua_State *L) { - ngx_stream_lua_shdict_ctx_t *ctx; - ngx_uint_t i; - ngx_shm_zone_t **zone; + ngx_uint_t i; + ngx_array_t all_zones; + ngx_module_t *http_module = NULL; + ngx_stream_lua_shdict_ctx_t *ctx; + ngx_shm_zone_t **zone, *shm_zone; + ngx_pool_t *temp_pool; + ngx_list_part_t *part; + ngx_module_t **modules; + + /* Find ngx_http_lua_module */ + modules = lmcf->cycle->modules; + + for (i = 0; modules[i]; i++) { + + if (ngx_strcmp(modules[i]->name, "ngx_http_lua_module") == 0) { + http_module = modules[i]; + break; + } + } + + /* place http and stream zones in a single array */ + temp_pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, log); + + if (temp_pool == NULL) { + ngx_log_error(NGX_LOG_ERR, log, 1, + "error: pool for zone array not allocated"); + return; + } + + if (ngx_array_init(&all_zones, temp_pool, 2, + sizeof(ngx_shm_zone_t *)) != NGX_OK) + { + ngx_destroy_pool(temp_pool); + ngx_log_error(NGX_LOG_ERR, log, 1, + "error: zone array not allocated"); + return; + } + + part = &lmcf->cycle->shared_memory.part; + shm_zone = part->elts; + + for (i = 0; /* void */ ; i++) { - if (lmcf->shm_zones != NULL) { - lua_createtable(L, 0, lmcf->shm_zones->nelts /* nrec */); + if (i >= part->nelts) { + if (part->next == NULL) { + break; + } + part = part->next; + shm_zone = part->elts; + i = 0; + } + + if ((shm_zone->tag == http_module && shm_zone->tag != NULL) + || shm_zone->tag == &ngx_stream_lua_module) + { + zone = ngx_array_push(&all_zones); + + if (zone == NULL) { + ngx_destroy_pool(temp_pool); + ngx_log_error(NGX_LOG_ERR, log, 1, + "error: zone pointer not allocated"); + return; + } + + *zone = shm_zone; + } + } + + if (all_zones.nelts > 0) { + lua_createtable(L, 0, all_zones.nelts /* nrec */); /* ngx.shared */ lua_createtable(L, 0 /* narr */, 13 /* nrec */); /* shared mt */ @@ -357,11 +422,14 @@ ngx_stream_lua_inject_shdict_api(ngx_stream_lua_main_conf_t *lmcf, lua_State *L) lua_pushvalue(L, -1); /* shared mt mt */ lua_setfield(L, -2, "__index"); /* shared mt */ - zone = lmcf->shm_zones->elts; + zone = all_zones.elts; - for (i = 0; i < lmcf->shm_zones->nelts; i++) { + for (i = 0; i < all_zones.nelts; i++) { ctx = zone[i]->data; + dd("injecting shared dict %.*s", + (int) ctx->name.len, ctx->name.data); + lua_pushlstring(L, (char *) ctx->name.data, ctx->name.len); /* shared mt key */ @@ -381,6 +449,7 @@ ngx_stream_lua_inject_shdict_api(ngx_stream_lua_main_conf_t *lmcf, lua_State *L) } lua_setfield(L, -2, "shared"); + ngx_destroy_pool(temp_pool); } diff --git a/src/ngx_stream_lua_shdict.h b/src/ngx_stream_lua_shdict.h index 83ee421d..42160978 100644 --- a/src/ngx_stream_lua_shdict.h +++ b/src/ngx_stream_lua_shdict.h @@ -44,8 +44,8 @@ typedef struct { ngx_int_t ngx_stream_lua_shdict_init_zone(ngx_shm_zone_t *shm_zone, void *data); void ngx_stream_lua_shdict_rbtree_insert_value(ngx_rbtree_node_t *temp, ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel); -void ngx_stream_lua_inject_shdict_api(ngx_stream_lua_main_conf_t *lmcf, - lua_State *L); +void ngx_stream_lua_inject_shdict_api(ngx_log_t *log, + ngx_stream_lua_main_conf_t *lmcf, lua_State *L); #endif /* _NGX_STREAM_LUA_SHDICT_H_INCLUDED_ */ diff --git a/src/ngx_stream_lua_util.c b/src/ngx_stream_lua_util.c index bc161035..52ce8fbc 100644 --- a/src/ngx_stream_lua_util.c +++ b/src/ngx_stream_lua_util.c @@ -2674,7 +2674,7 @@ ngx_stream_lua_inject_ngx_api(lua_State *L, ngx_stream_lua_main_conf_t *lmcf, ngx_stream_lua_inject_req_api(log, L); ngx_stream_lua_inject_variable_api(L); - ngx_stream_lua_inject_shdict_api(lmcf, L); + ngx_stream_lua_inject_shdict_api(log, lmcf, L); ngx_stream_lua_inject_socket_tcp_api(log, L); ngx_stream_lua_inject_socket_udp_api(log, L); ngx_stream_lua_inject_uthread_api(log, L);