Skip to content

Commit 89795c9

Browse files
committed
feature: added the 'ngx.shared.get_info()' API when compiled with debug.
This replaces lua-nginx-module's ngx_http_lua_fake_shm_module for the `t/148-fake-shm-zone.t` and `t/149-hup-fake-shm-zone.t` test suites.
1 parent a60bfc8 commit 89795c9

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/meta/ngx_meta_lua_shdict.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ static ngx_inline ngx_queue_t *
2828
size_t len);
2929
static int ngx_meta_lua_shdict_expire(ngx_meta_lua_shdict_ctx_t *ctx,
3030
ngx_uint_t n);
31+
#if (NGX_DEBUG)
32+
static int ngx_meta_lua_shdict_get_info(lua_State *L);
33+
#endif
3134
static int ngx_meta_lua_shdict_push_helper(lua_State *L, int flags);
3235
static int ngx_meta_lua_shdict_pop_helper(lua_State *L, int flags);
3336
static int ngx_meta_lua_shdict_flush_expired(lua_State *L);
@@ -210,9 +213,16 @@ ngx_meta_lua_shdict_init_zone(ngx_shm_zone_t *shm_zone, void *data)
210213

211214
ctx = shm_zone->data;
212215

216+
#if (NGX_DEBUG)
217+
ctx->isinit = 1;
218+
#endif
219+
213220
if (octx) {
214221
ctx->sh = octx->sh;
215222
ctx->shpool = octx->shpool;
223+
#if (NGX_DEBUG)
224+
ctx->isold = 1;
225+
#endif
216226

217227
return NGX_OK;
218228
}
@@ -340,6 +350,11 @@ ngx_meta_lua_inject_shdict_api(lua_State *L, ngx_cycle_t *cycle, void *tag)
340350
lua_pushcfunction(L, ngx_meta_lua_shdict_get_keys);
341351
lua_setfield(L, -2, "get_keys");
342352

353+
#if (NGX_DEBUG)
354+
lua_pushcfunction(L, ngx_meta_lua_shdict_get_info);
355+
lua_setfield(L, -2, "get_info");
356+
#endif
357+
343358
lua_pushvalue(L, -1); /* shared mt mt */
344359
lua_setfield(L, -2, "__index"); /* shared mt */
345360

@@ -1282,6 +1297,39 @@ ngx_meta_lua_shdict_llen(lua_State *L)
12821297
}
12831298

12841299

1300+
#if (NGX_DEBUG)
1301+
static int
1302+
ngx_meta_lua_shdict_get_info(lua_State *L)
1303+
{
1304+
ngx_int_t n;
1305+
ngx_shm_zone_t *zone;
1306+
ngx_meta_lua_shdict_ctx_t *ctx;
1307+
1308+
n = lua_gettop(L);
1309+
1310+
if (n != 1) {
1311+
return luaL_error(L, "expecting exactly one argument, but seen %d", n);
1312+
}
1313+
1314+
luaL_checktype(L, 1, LUA_TTABLE);
1315+
1316+
zone = ngx_meta_lua_shdict_get_zone(L, 1);
1317+
if (zone == NULL) {
1318+
return luaL_error(L, "bad \"zone\" argument");
1319+
}
1320+
1321+
ctx = (ngx_meta_lua_shdict_ctx_t *) zone->data;
1322+
1323+
lua_pushlstring(L, (char *) zone->shm.name.data, zone->shm.name.len);
1324+
lua_pushnumber(L, zone->shm.size);
1325+
lua_pushboolean(L, ctx->isinit);
1326+
lua_pushboolean(L, ctx->isold);
1327+
1328+
return 4;
1329+
}
1330+
#endif
1331+
1332+
12851333
ngx_shm_zone_t *
12861334
ngx_meta_lua_ffi_shdict_udata_to_zone(void *zone_udata)
12871335
{

src/meta/ngx_meta_lua_shdict.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ typedef struct {
3838

3939

4040
typedef struct {
41+
#if (NGX_DEBUG)
42+
ngx_int_t isold;
43+
ngx_int_t isinit;
44+
#endif
4145
ngx_str_t name;
4246
ngx_meta_lua_shdict_shctx_t *sh;
4347
ngx_slab_pool_t *shpool;

0 commit comments

Comments
 (0)