13
13
#include "ngx_stream_lua_shdict.h"
14
14
#include "ngx_stream_lua_util.h"
15
15
16
+ extern ngx_module_t ngx_http_lua_module ;
16
17
17
18
static int ngx_stream_lua_shdict_set (lua_State * L );
18
19
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)
308
309
void
309
310
ngx_stream_lua_inject_shdict_api (ngx_stream_lua_main_conf_t * lmcf , lua_State * L )
310
311
{
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
+ }
314
365
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 */ );
317
372
/* ngx.shared */
318
373
319
374
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)
357
412
lua_pushvalue (L , -1 ); /* shared mt mt */
358
413
lua_setfield (L , -2 , "__index" ); /* shared mt */
359
414
360
- zone = lmcf -> shm_zones -> elts ;
415
+ zone = all_zones . elts ;
361
416
362
- for (i = 0 ; i < lmcf -> shm_zones -> nelts ; i ++ ) {
417
+ for (i = 0 ; i < all_zones . nelts ; i ++ ) {
363
418
ctx = zone [i ]-> data ;
364
419
420
+ dd ("injecting shared dict %.*s" ,
421
+ (int ) ctx -> name .len , ctx -> name .data );
422
+
365
423
lua_pushlstring (L , (char * ) ctx -> name .data , ctx -> name .len );
366
424
/* shared mt key */
367
425
@@ -381,6 +439,7 @@ ngx_stream_lua_inject_shdict_api(ngx_stream_lua_main_conf_t *lmcf, lua_State *L)
381
439
}
382
440
383
441
lua_setfield (L , -2 , "shared" );
442
+ ngx_destroy_pool (temp_pool );
384
443
}
385
444
386
445
0 commit comments