Skip to content

Commit 60df5b2

Browse files
author
alonbg
committed
cross modules stream/http shared dict
1 parent aafd50b commit 60df5b2

File tree

1 file changed

+66
-7
lines changed

1 file changed

+66
-7
lines changed

src/ngx_stream_lua_shdict.c

+66-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "ngx_stream_lua_shdict.h"
1414
#include "ngx_stream_lua_util.h"
1515

16+
extern ngx_module_t ngx_http_lua_module;
1617

1718
static int ngx_stream_lua_shdict_set(lua_State *L);
1819
static int ngx_stream_lua_shdict_safe_set(lua_State *L);
@@ -308,12 +309,66 @@ ngx_stream_lua_shdict_expire(ngx_stream_lua_shdict_ctx_t *ctx, ngx_uint_t n)
308309
void
309310
ngx_stream_lua_inject_shdict_api(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_stream_lua_shdict_ctx_t *ctx;
315+
ngx_shm_zone_t **zone, *shm_zone;
316+
ngx_pool_t *temp_pool;
317+
ngx_list_part_t *part;
318+
ngx_log_t *log;
319+
320+
log = &lmcf->cycle->log;
321+
322+
/* place http and stream zones in a single array */
323+
temp_pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, log);
324+
325+
if (temp_pool == NULL) {
326+
ngx_log_error(NGX_LOG_ERR, log, 1,
327+
"error: pool for zone array not allocated");
328+
return;
329+
}
330+
331+
if (ngx_array_init(&all_zones, temp_pool, 2,
332+
sizeof(ngx_shm_zone_t *)) != NGX_OK)
333+
{
334+
ngx_destroy_pool(temp_pool);
335+
ngx_log_error(NGX_LOG_ERR, log, 1,
336+
"error: zone array not allocated");
337+
return;
338+
}
339+
340+
part = &lmcf->cycle->shared_memory.part;
341+
shm_zone = part->elts;
342+
343+
for (i = 0; /* void */ ; i++) {
344+
345+
if (i >= part->nelts) {
346+
if (part->next == NULL) {
347+
break;
348+
}
349+
part = part->next;
350+
shm_zone = part->elts;
351+
i = 0;
352+
}
353+
354+
if (&ngx_http_lua_module == shm_zone->tag ||
355+
&ngx_stream_lua_module == shm_zone->tag )
356+
{
357+
zone = ngx_array_push(&all_zones);
358+
359+
if (zone == NULL) {
360+
ngx_destroy_pool(temp_pool);
361+
ngx_log_error(NGX_LOG_ERR, log, 1,
362+
"error: zone pointer not allocated");
363+
return;
364+
}
314365

315-
if (lmcf->shm_zones != NULL) {
316-
lua_createtable(L, 0, lmcf->shm_zones->nelts /* nrec */);
366+
*zone = shm_zone;
367+
}
368+
}
369+
370+
if (all_zones.nelts > 0) {
371+
lua_createtable(L, 0, all_zones.nelts /* nrec */);
317372
/* ngx.shared */
318373

319374
lua_createtable(L, 0 /* narr */, 13 /* nrec */); /* shared mt */
@@ -357,11 +412,14 @@ ngx_stream_lua_inject_shdict_api(ngx_stream_lua_main_conf_t *lmcf, lua_State *L)
357412
lua_pushvalue(L, -1); /* shared mt mt */
358413
lua_setfield(L, -2, "__index"); /* shared mt */
359414

360-
zone = lmcf->shm_zones->elts;
415+
zone = all_zones.elts;
361416

362-
for (i = 0; i < lmcf->shm_zones->nelts; i++) {
417+
for (i = 0; i < all_zones.nelts; i++) {
363418
ctx = zone[i]->data;
364419

420+
dd("injecting shared dict %.*s",
421+
(int) ctx->name.len, ctx->name.data);
422+
365423
lua_pushlstring(L, (char *) ctx->name.data, ctx->name.len);
366424
/* shared mt key */
367425

@@ -381,6 +439,7 @@ ngx_stream_lua_inject_shdict_api(ngx_stream_lua_main_conf_t *lmcf, lua_State *L)
381439
}
382440

383441
lua_setfield(L, -2, "shared");
442+
ngx_destroy_pool(temp_pool);
384443
}
385444

386445

0 commit comments

Comments
 (0)