Skip to content

Commit 0d4dda7

Browse files
author
alonbg
committed
find http_lua_module by name for shared dict
1 parent aafd50b commit 0d4dda7

File tree

3 files changed

+80
-11
lines changed

3 files changed

+80
-11
lines changed

Diff for: src/ngx_stream_lua_shdict.c

+77-8
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,79 @@ ngx_stream_lua_shdict_expire(ngx_stream_lua_shdict_ctx_t *ctx, ngx_uint_t n)
306306

307307

308308
void
309-
ngx_stream_lua_inject_shdict_api(ngx_stream_lua_main_conf_t *lmcf, lua_State *L)
309+
ngx_stream_lua_inject_shdict_api(ngx_log_t *log,
310+
ngx_stream_lua_main_conf_t *lmcf, lua_State *L)
310311
{
311-
ngx_stream_lua_shdict_ctx_t *ctx;
312-
ngx_uint_t i;
313-
ngx_shm_zone_t **zone;
312+
ngx_uint_t i;
313+
ngx_array_t all_zones;
314+
ngx_module_t *http_module = NULL;
315+
ngx_stream_lua_shdict_ctx_t *ctx;
316+
ngx_shm_zone_t **zone, *shm_zone;
317+
ngx_pool_t *temp_pool;
318+
ngx_list_part_t *part;
319+
ngx_module_t **modules;
320+
321+
/* Find ngx_http_lua_module */
322+
modules = lmcf->cycle->modules;
323+
324+
for (i = 0; modules[i]; i++) {
325+
326+
if (ngx_strcmp(modules[i]->name, "ngx_http_lua_module") == 0) {
327+
http_module = modules[i];
328+
break;
329+
}
330+
}
331+
332+
/* place http and stream zones in a single array */
333+
temp_pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, log);
334+
335+
if (temp_pool == NULL) {
336+
ngx_log_error(NGX_LOG_ERR, log, 1,
337+
"error: pool for zone array not allocated");
338+
return;
339+
}
340+
341+
if (ngx_array_init(&all_zones, temp_pool, 2,
342+
sizeof(ngx_shm_zone_t *)) != NGX_OK)
343+
{
344+
ngx_destroy_pool(temp_pool);
345+
ngx_log_error(NGX_LOG_ERR, log, 1,
346+
"error: zone array not allocated");
347+
return;
348+
}
349+
350+
part = &lmcf->cycle->shared_memory.part;
351+
shm_zone = part->elts;
352+
353+
for (i = 0; /* void */ ; i++) {
314354

315-
if (lmcf->shm_zones != NULL) {
316-
lua_createtable(L, 0, lmcf->shm_zones->nelts /* nrec */);
355+
if (i >= part->nelts) {
356+
if (part->next == NULL) {
357+
break;
358+
}
359+
part = part->next;
360+
shm_zone = part->elts;
361+
i = 0;
362+
}
363+
364+
if ((shm_zone->tag == http_module && shm_zone->tag != NULL)
365+
|| shm_zone->tag == &ngx_stream_lua_module)
366+
{
367+
zone = ngx_array_push(&all_zones);
368+
369+
if (zone == NULL) {
370+
ngx_destroy_pool(temp_pool);
371+
ngx_log_error(NGX_LOG_ERR, log, 1,
372+
"error: zone pointer not allocated");
373+
return;
374+
}
375+
376+
*zone = shm_zone;
377+
}
378+
}
379+
380+
if (all_zones.nelts > 0) {
381+
lua_createtable(L, 0, all_zones.nelts /* nrec */);
317382
/* ngx.shared */
318383

319384
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)
357422
lua_pushvalue(L, -1); /* shared mt mt */
358423
lua_setfield(L, -2, "__index"); /* shared mt */
359424

360-
zone = lmcf->shm_zones->elts;
425+
zone = all_zones.elts;
361426

362-
for (i = 0; i < lmcf->shm_zones->nelts; i++) {
427+
for (i = 0; i < all_zones.nelts; i++) {
363428
ctx = zone[i]->data;
364429

430+
dd("injecting shared dict %.*s",
431+
(int) ctx->name.len, ctx->name.data);
432+
365433
lua_pushlstring(L, (char *) ctx->name.data, ctx->name.len);
366434
/* shared mt key */
367435

@@ -381,6 +449,7 @@ ngx_stream_lua_inject_shdict_api(ngx_stream_lua_main_conf_t *lmcf, lua_State *L)
381449
}
382450

383451
lua_setfield(L, -2, "shared");
452+
ngx_destroy_pool(temp_pool);
384453
}
385454

386455

Diff for: src/ngx_stream_lua_shdict.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ typedef struct {
4444
ngx_int_t ngx_stream_lua_shdict_init_zone(ngx_shm_zone_t *shm_zone, void *data);
4545
void ngx_stream_lua_shdict_rbtree_insert_value(ngx_rbtree_node_t *temp,
4646
ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
47-
void ngx_stream_lua_inject_shdict_api(ngx_stream_lua_main_conf_t *lmcf,
48-
lua_State *L);
47+
void ngx_stream_lua_inject_shdict_api(ngx_log_t *log,
48+
ngx_stream_lua_main_conf_t *lmcf, lua_State *L);
4949

5050

5151
#endif /* _NGX_STREAM_LUA_SHDICT_H_INCLUDED_ */

Diff for: src/ngx_stream_lua_util.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2674,7 +2674,7 @@ ngx_stream_lua_inject_ngx_api(lua_State *L, ngx_stream_lua_main_conf_t *lmcf,
26742674

26752675
ngx_stream_lua_inject_req_api(log, L);
26762676
ngx_stream_lua_inject_variable_api(L);
2677-
ngx_stream_lua_inject_shdict_api(lmcf, L);
2677+
ngx_stream_lua_inject_shdict_api(log, lmcf, L);
26782678
ngx_stream_lua_inject_socket_tcp_api(log, L);
26792679
ngx_stream_lua_inject_socket_udp_api(log, L);
26802680
ngx_stream_lua_inject_uthread_api(log, L);

0 commit comments

Comments
 (0)