Skip to content

Commit 545deb0

Browse files
committed
refactor: configure: avoid duplicated code for shm creation.
1 parent 69f7817 commit 545deb0

5 files changed

+104
-124
lines changed

src/ngx_http_lua_api.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ ngx_http_lua_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name, size_t size,
9191
ngx_shm_zone_t **zp;
9292
ngx_shm_zone_t *zone;
9393
ngx_http_lua_shm_zone_ctx_t *ctx;
94-
ngx_int_t n;
9594

9695
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
9796
if (lmcf == NULL) {
@@ -122,9 +121,7 @@ ngx_http_lua_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name, size_t size,
122121
return &ctx->zone;
123122
}
124123

125-
n = sizeof(ngx_http_lua_shm_zone_ctx_t);
126-
127-
ctx = ngx_pcalloc(cf->pool, n);
124+
ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_lua_shm_zone_ctx_t));
128125
if (ctx == NULL) {
129126
return NULL;
130127
}

src/ngx_http_lua_configureby.c

+22-61
Original file line numberDiff line numberDiff line change
@@ -143,88 +143,49 @@ int
143143
ngx_http_lua_ffi_configure_shared_dict(ngx_str_t *name, ngx_str_t *size,
144144
u_char *errstr, size_t *err_len)
145145
{
146-
ssize_t ssize;
147-
ngx_shm_zone_t **zp;
148-
ngx_shm_zone_t *zone;
149-
ngx_http_lua_shdict_ctx_t *ctx;
150-
ngx_http_lua_main_conf_t *lmcf;
151-
lua_State *L;
152-
ngx_conf_t *cf = cfp;
146+
ngx_int_t rc;
147+
ssize_t ssize;
148+
lua_State *L;
149+
ngx_http_lua_main_conf_t *lmcf;
150+
ngx_conf_t *cf = cfp;
151+
ngx_shm_zone_t **zone;
153152

154153
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
155154

156155
ssize = ngx_parse_size(size);
157156
if (ssize <= NGX_HTTP_LUA_SHDICT_MINSIZE) {
158157
*err_len = ngx_snprintf(errstr, *err_len,
159-
"invalid lua shared dict size \"%s\"",
160-
size->data)
161-
- errstr;
162-
return NGX_ERROR;
163-
}
164-
165-
ctx = ngx_pcalloc(cf->cycle->pool, sizeof(ngx_http_lua_shdict_ctx_t));
166-
if (ctx == NULL) {
167-
*err_len = ngx_snprintf(errstr, *err_len, "no memory")
158+
"invalid lua shared dict size \"%s\"",
159+
size->data)
168160
- errstr;
169-
return NGX_ERROR;
170-
}
171-
172-
ctx->name = *name;
173-
ctx->main_conf = lmcf;
174-
ctx->log = &cf->cycle->new_log;
175-
176-
zone = ngx_http_lua_shared_memory_add(cf, name, (size_t) ssize,
177-
&ngx_http_lua_module);
178-
if (zone == NULL) {
179-
*err_len = ngx_snprintf(errstr, *err_len, "no memory")
180-
- errstr;
181-
return NGX_ERROR;
182-
}
183-
184-
if (zone->data) {
185161
return NGX_DECLINED;
186162
}
187163

188-
zone->init = ngx_http_lua_shdict_init_zone;
189-
zone->data = ctx;
190-
191-
if (lmcf->shdict_zones == NULL) {
192-
lmcf->shdict_zones = ngx_palloc(cf->pool, sizeof(ngx_array_t));
193-
if (lmcf->shdict_zones == NULL) {
194-
*err_len = ngx_snprintf(errstr, *err_len, "no memory")
195-
- errstr;
196-
return NGX_ERROR;
197-
}
198-
199-
if (ngx_array_init(lmcf->shdict_zones, cf->pool, 2,
200-
sizeof(ngx_shm_zone_t *))
201-
!= NGX_OK)
202-
{
203-
*err_len = ngx_snprintf(errstr, *err_len, "no memory")
164+
rc = ngx_http_lua_shared_dict_add(cf, name, ssize);
165+
if (rc != NGX_OK) {
166+
if (rc == NGX_DECLINED) {
167+
*err_len = ngx_snprintf(errstr, *err_len,
168+
"lua_shared_dict \"%V\" is already defined"
169+
" as \"%V\"", name, name)
204170
- errstr;
205-
return NGX_ERROR;
206171
}
207-
}
208172

209-
zp = ngx_array_push(lmcf->shdict_zones);
210-
if (zp == NULL) {
211-
*err_len = ngx_snprintf(errstr, *err_len, "no memory")
212-
- errstr;
213-
return NGX_ERROR;
173+
return rc;
214174
}
215175

216-
*zp = zone;
176+
zone = lmcf->shdict_zones->elts;
217177

218178
L = lmcf->lua;
219179

220180
lua_getglobal(L, "ngx");
221181
lua_getfield(L, -1, "shared");
222-
if (!lua_getmetatable(L, -1)) {
223-
ngx_http_lua_create_shdict_mt(L); /* ngx.shared shmt */
224-
}
182+
ngx_http_lua_create_shdict_mt(L);
183+
184+
/* ngx ngx.shared shmt */
185+
186+
ngx_http_lua_attach_shdict(L, name, zone[lmcf->shdict_zones->nelts - 1]);
225187

226-
ngx_http_lua_attach_shdict(L, name, zone);
227-
lua_pop(L, 2); /* pop: ngx.shared + shmt */
188+
lua_pop(L, 3); /* pop: ngx ngx.shared shmt */
228189

229190
return NGX_OK;
230191
}

src/ngx_http_lua_directive.c

+11-57
Original file line numberDiff line numberDiff line change
@@ -73,40 +73,19 @@ enum {
7373
char *
7474
ngx_http_lua_shared_dict(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
7575
{
76-
ngx_http_lua_main_conf_t *lmcf = conf;
77-
7876
ngx_str_t *value, name;
79-
ngx_shm_zone_t *zone;
80-
ngx_shm_zone_t **zp;
81-
ngx_http_lua_shdict_ctx_t *ctx;
8277
ssize_t size;
83-
84-
if (lmcf->shdict_zones == NULL) {
85-
lmcf->shdict_zones = ngx_palloc(cf->pool, sizeof(ngx_array_t));
86-
if (lmcf->shdict_zones == NULL) {
87-
return NGX_CONF_ERROR;
88-
}
89-
90-
if (ngx_array_init(lmcf->shdict_zones, cf->pool, 2,
91-
sizeof(ngx_shm_zone_t *))
92-
!= NGX_OK)
93-
{
94-
return NGX_CONF_ERROR;
95-
}
96-
}
78+
ngx_int_t rc;
9779

9880
value = cf->args->elts;
81+
name = value[1];
9982

100-
ctx = NULL;
101-
102-
if (value[1].len == 0) {
83+
if (name.len == 0) {
10384
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
104-
"invalid lua shared dict name \"%V\"", &value[1]);
85+
"invalid lua shared dict name \"%V\"", &name);
10586
return NGX_CONF_ERROR;
10687
}
10788

108-
name = value[1];
109-
11089
size = ngx_parse_size(&value[2]);
11190

11291
if (size <= NGX_HTTP_LUA_SHDICT_MINSIZE) {
@@ -115,42 +94,17 @@ ngx_http_lua_shared_dict(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
11594
return NGX_CONF_ERROR;
11695
}
11796

118-
ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_lua_shdict_ctx_t));
119-
if (ctx == NULL) {
120-
return NGX_CONF_ERROR;
121-
}
122-
123-
ctx->name = name;
124-
ctx->main_conf = lmcf;
125-
ctx->log = &cf->cycle->new_log;
126-
127-
zone = ngx_http_lua_shared_memory_add(cf, &name, (size_t) size,
128-
&ngx_http_lua_module);
129-
if (zone == NULL) {
130-
return NGX_CONF_ERROR;
131-
}
132-
133-
if (zone->data) {
134-
ctx = zone->data;
135-
136-
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
137-
"lua_shared_dict \"%V\" is already defined as "
138-
"\"%V\"", &name, &ctx->name);
139-
return NGX_CONF_ERROR;
140-
}
141-
142-
zone->init = ngx_http_lua_shdict_init_zone;
143-
zone->data = ctx;
97+
rc = ngx_http_lua_shared_dict_add(cf, &name, size);
98+
if (rc != NGX_OK) {
99+
if (rc == NGX_DECLINED) {
100+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
101+
"lua_shared_dict \"%V\" is already defined as "
102+
"\"%V\"", &name, &name);
103+
}
144104

145-
zp = ngx_array_push(lmcf->shdict_zones);
146-
if (zp == NULL) {
147105
return NGX_CONF_ERROR;
148106
}
149107

150-
*zp = zone;
151-
152-
lmcf->requires_shm = 1;
153-
154108
return NGX_CONF_OK;
155109
}
156110

src/ngx_http_lua_shdict.c

+68-2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,63 @@ ngx_http_lua_shdict_get_list_head(ngx_http_lua_shdict_node_t *sd, size_t len)
7979
}
8080

8181

82+
ngx_int_t
83+
ngx_http_lua_shared_dict_add(ngx_conf_t *cf, ngx_str_t *name, ssize_t size)
84+
{
85+
ngx_http_lua_main_conf_t *lmcf;
86+
ngx_http_lua_shdict_ctx_t *ctx = NULL;
87+
ngx_shm_zone_t *zone;
88+
ngx_shm_zone_t **zp;
89+
90+
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
91+
92+
if (lmcf->shdict_zones == NULL) {
93+
lmcf->shdict_zones = ngx_palloc(cf->pool, sizeof(ngx_array_t));
94+
if (lmcf->shdict_zones == NULL) {
95+
return NGX_ERROR;
96+
}
97+
98+
if (ngx_array_init(lmcf->shdict_zones, cf->pool, 2,
99+
sizeof(ngx_shm_zone_t *))
100+
!= NGX_OK)
101+
{
102+
return NGX_ERROR;
103+
}
104+
}
105+
106+
ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_lua_shdict_ctx_t));
107+
if (ctx == NULL) {
108+
return NGX_ERROR;
109+
}
110+
111+
ctx->name = *name;
112+
ctx->main_conf = lmcf;
113+
ctx->log = &cf->cycle->new_log;
114+
115+
zone = ngx_http_lua_shared_memory_add(cf, name, (size_t) size,
116+
&ngx_http_lua_module);
117+
if (zone == NULL) {
118+
return NGX_ERROR;
119+
}
120+
121+
if (zone->data) {
122+
return NGX_DECLINED;
123+
}
124+
125+
zone->init = ngx_http_lua_shdict_init_zone;
126+
zone->data = ctx;
127+
128+
zp = ngx_array_push(lmcf->shdict_zones);
129+
if (zp == NULL) {
130+
return NGX_ERROR;
131+
}
132+
133+
*zp = zone;
134+
135+
return NGX_OK;
136+
}
137+
138+
82139
ngx_int_t
83140
ngx_http_lua_shdict_init_zone(ngx_shm_zone_t *shm_zone, void *data)
84141
{
@@ -323,6 +380,15 @@ ngx_http_lua_shdict_expire(ngx_http_lua_shdict_ctx_t *ctx, ngx_uint_t n)
323380
void
324381
ngx_http_lua_create_shdict_mt(lua_State *L)
325382
{
383+
/* ngx ngx.shared */
384+
385+
if (lua_getmetatable(L, -1)) {
386+
/* when no lua_shared_dict directives have been set, but we
387+
* we add a dict via the configure phase, we lazily create the
388+
* shdict mt. This avoids creating it multiple times. */
389+
return;
390+
}
391+
326392
lua_createtable(L, 0 /* narr */, 18 /* nrec */);
327393
/* ngx ngx.shared shmt */
328394

@@ -383,7 +449,8 @@ ngx_http_lua_create_shdict_mt(lua_State *L)
383449

384450

385451
void
386-
ngx_http_lua_attach_shdict(lua_State *L, ngx_str_t *name, ngx_shm_zone_t *zone)
452+
ngx_http_lua_attach_shdict(lua_State *L, ngx_str_t *name,
453+
ngx_shm_zone_t *zone)
387454
{
388455
lua_pushlstring(L, (char *) name->data, name->len);
389456
/* ngx ngx.shared shmt name */
@@ -414,7 +481,6 @@ ngx_http_lua_inject_shdict_api(ngx_http_lua_main_conf_t *lmcf, lua_State *L)
414481
/* ngx ngx.shared */
415482

416483
ngx_http_lua_create_shdict_mt(L);
417-
/* ngx ngx.shared shmt */
418484

419485
zone = lmcf->shdict_zones->elts;
420486

src/ngx_http_lua_shdict.h

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ typedef struct {
5858
} ngx_http_lua_shm_zone_ctx_t;
5959

6060

61+
ngx_int_t ngx_http_lua_shared_dict_add(ngx_conf_t *cf, ngx_str_t *name,
62+
ssize_t size);
6163
ngx_int_t ngx_http_lua_shdict_init_zone(ngx_shm_zone_t *shm_zone, void *data);
6264
void ngx_http_lua_shdict_rbtree_insert_value(ngx_rbtree_node_t *temp,
6365
ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);

0 commit comments

Comments
 (0)