@@ -306,14 +306,79 @@ ngx_stream_lua_shdict_expire(ngx_stream_lua_shdict_ctx_t *ctx, ngx_uint_t n)
306
306
307
307
308
308
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 )
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_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 ++ ) {
314
354
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 */ );
317
382
/* ngx.shared */
318
383
319
384
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)
357
422
lua_pushvalue (L , -1 ); /* shared mt mt */
358
423
lua_setfield (L , -2 , "__index" ); /* shared mt */
359
424
360
- zone = lmcf -> shm_zones -> elts ;
425
+ zone = all_zones . elts ;
361
426
362
- for (i = 0 ; i < lmcf -> shm_zones -> nelts ; i ++ ) {
427
+ for (i = 0 ; i < all_zones . nelts ; i ++ ) {
363
428
ctx = zone [i ]-> data ;
364
429
430
+ dd ("injecting shared dict %.*s" ,
431
+ (int ) ctx -> name .len , ctx -> name .data );
432
+
365
433
lua_pushlstring (L , (char * ) ctx -> name .data , ctx -> name .len );
366
434
/* shared mt key */
367
435
@@ -381,6 +449,7 @@ ngx_stream_lua_inject_shdict_api(ngx_stream_lua_main_conf_t *lmcf, lua_State *L)
381
449
}
382
450
383
451
lua_setfield (L , -2 , "shared" );
452
+ ngx_destroy_pool (temp_pool );
384
453
}
385
454
386
455
0 commit comments