diff --git a/src/ngx_http_lua_api.c b/src/ngx_http_lua_api.c index 7b590e7a9f..b8fbfc684b 100644 --- a/src/ngx_http_lua_api.c +++ b/src/ngx_http_lua_api.c @@ -91,7 +91,6 @@ ngx_http_lua_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name, size_t size, ngx_shm_zone_t **zp; ngx_shm_zone_t *zone; ngx_http_lua_shm_zone_ctx_t *ctx; - ngx_int_t n; lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module); if (lmcf == NULL) { @@ -122,9 +121,7 @@ ngx_http_lua_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name, size_t size, return &ctx->zone; } - n = sizeof(ngx_http_lua_shm_zone_ctx_t); - - ctx = ngx_pcalloc(cf->pool, n); + ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_lua_shm_zone_ctx_t)); if (ctx == NULL) { return NULL; } diff --git a/src/ngx_http_lua_configureby.c b/src/ngx_http_lua_configureby.c index d798c27200..d7bb880143 100644 --- a/src/ngx_http_lua_configureby.c +++ b/src/ngx_http_lua_configureby.c @@ -143,88 +143,49 @@ int ngx_http_lua_ffi_configure_shared_dict(ngx_str_t *name, ngx_str_t *size, u_char *errstr, size_t *err_len) { - ssize_t ssize; - ngx_shm_zone_t **zp; - ngx_shm_zone_t *zone; - ngx_http_lua_shdict_ctx_t *ctx; - ngx_http_lua_main_conf_t *lmcf; - lua_State *L; - ngx_conf_t *cf = cfp; + ngx_int_t rc; + ssize_t ssize; + lua_State *L; + ngx_http_lua_main_conf_t *lmcf; + ngx_conf_t *cf = cfp; + ngx_shm_zone_t **zone; lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module); ssize = ngx_parse_size(size); if (ssize <= NGX_HTTP_LUA_SHDICT_MINSIZE) { *err_len = ngx_snprintf(errstr, *err_len, - "invalid lua shared dict size \"%s\"", - size->data) - - errstr; - return NGX_ERROR; - } - - ctx = ngx_pcalloc(cf->cycle->pool, sizeof(ngx_http_lua_shdict_ctx_t)); - if (ctx == NULL) { - *err_len = ngx_snprintf(errstr, *err_len, "no memory") + "invalid lua shared dict size \"%s\"", + size->data) - errstr; - return NGX_ERROR; - } - - ctx->name = *name; - ctx->main_conf = lmcf; - ctx->log = &cf->cycle->new_log; - - zone = ngx_http_lua_shared_memory_add(cf, name, (size_t) ssize, - &ngx_http_lua_module); - if (zone == NULL) { - *err_len = ngx_snprintf(errstr, *err_len, "no memory") - - errstr; - return NGX_ERROR; - } - - if (zone->data) { return NGX_DECLINED; } - zone->init = ngx_http_lua_shdict_init_zone; - zone->data = ctx; - - if (lmcf->shdict_zones == NULL) { - lmcf->shdict_zones = ngx_palloc(cf->pool, sizeof(ngx_array_t)); - if (lmcf->shdict_zones == NULL) { - *err_len = ngx_snprintf(errstr, *err_len, "no memory") - - errstr; - return NGX_ERROR; - } - - if (ngx_array_init(lmcf->shdict_zones, cf->pool, 2, - sizeof(ngx_shm_zone_t *)) - != NGX_OK) - { - *err_len = ngx_snprintf(errstr, *err_len, "no memory") + rc = ngx_http_lua_shared_dict_add(cf, name, ssize); + if (rc != NGX_OK) { + if (rc == NGX_DECLINED) { + *err_len = ngx_snprintf(errstr, *err_len, + "lua_shared_dict \"%V\" is already defined" + " as \"%V\"", name, name) - errstr; - return NGX_ERROR; } - } - zp = ngx_array_push(lmcf->shdict_zones); - if (zp == NULL) { - *err_len = ngx_snprintf(errstr, *err_len, "no memory") - - errstr; - return NGX_ERROR; + return rc; } - *zp = zone; + zone = lmcf->shdict_zones->elts; L = lmcf->lua; lua_getglobal(L, "ngx"); lua_getfield(L, -1, "shared"); - if (!lua_getmetatable(L, -1)) { - ngx_http_lua_create_shdict_mt(L); /* ngx.shared shmt */ - } + ngx_http_lua_create_shdict_mt(L); + + /* ngx ngx.shared shmt */ + + ngx_http_lua_attach_shdict(L, name, zone[lmcf->shdict_zones->nelts - 1]); - ngx_http_lua_attach_shdict(L, name, zone); - lua_pop(L, 2); /* pop: ngx.shared + shmt */ + lua_pop(L, 3); /* pop: ngx ngx.shared shmt */ return NGX_OK; } diff --git a/src/ngx_http_lua_directive.c b/src/ngx_http_lua_directive.c index 8ff6760962..0b533c90c5 100644 --- a/src/ngx_http_lua_directive.c +++ b/src/ngx_http_lua_directive.c @@ -73,40 +73,19 @@ enum { char * ngx_http_lua_shared_dict(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { - ngx_http_lua_main_conf_t *lmcf = conf; - ngx_str_t *value, name; - ngx_shm_zone_t *zone; - ngx_shm_zone_t **zp; - ngx_http_lua_shdict_ctx_t *ctx; ssize_t size; - - if (lmcf->shdict_zones == NULL) { - lmcf->shdict_zones = ngx_palloc(cf->pool, sizeof(ngx_array_t)); - if (lmcf->shdict_zones == NULL) { - return NGX_CONF_ERROR; - } - - if (ngx_array_init(lmcf->shdict_zones, cf->pool, 2, - sizeof(ngx_shm_zone_t *)) - != NGX_OK) - { - return NGX_CONF_ERROR; - } - } + ngx_int_t rc; value = cf->args->elts; + name = value[1]; - ctx = NULL; - - if (value[1].len == 0) { + if (name.len == 0) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, - "invalid lua shared dict name \"%V\"", &value[1]); + "invalid lua shared dict name \"%V\"", &name); return NGX_CONF_ERROR; } - name = value[1]; - size = ngx_parse_size(&value[2]); 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) return NGX_CONF_ERROR; } - ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_lua_shdict_ctx_t)); - if (ctx == NULL) { - return NGX_CONF_ERROR; - } - - ctx->name = name; - ctx->main_conf = lmcf; - ctx->log = &cf->cycle->new_log; - - zone = ngx_http_lua_shared_memory_add(cf, &name, (size_t) size, - &ngx_http_lua_module); - if (zone == NULL) { - return NGX_CONF_ERROR; - } - - if (zone->data) { - ctx = zone->data; - - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, - "lua_shared_dict \"%V\" is already defined as " - "\"%V\"", &name, &ctx->name); - return NGX_CONF_ERROR; - } - - zone->init = ngx_http_lua_shdict_init_zone; - zone->data = ctx; + rc = ngx_http_lua_shared_dict_add(cf, &name, size); + if (rc != NGX_OK) { + if (rc == NGX_DECLINED) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "lua_shared_dict \"%V\" is already defined as " + "\"%V\"", &name, &name); + } - zp = ngx_array_push(lmcf->shdict_zones); - if (zp == NULL) { return NGX_CONF_ERROR; } - *zp = zone; - - lmcf->requires_shm = 1; - return NGX_CONF_OK; } diff --git a/src/ngx_http_lua_shdict.c b/src/ngx_http_lua_shdict.c index a05f291b72..df1dbc51cf 100644 --- a/src/ngx_http_lua_shdict.c +++ b/src/ngx_http_lua_shdict.c @@ -79,6 +79,63 @@ ngx_http_lua_shdict_get_list_head(ngx_http_lua_shdict_node_t *sd, size_t len) } +ngx_int_t +ngx_http_lua_shared_dict_add(ngx_conf_t *cf, ngx_str_t *name, ssize_t size) +{ + ngx_http_lua_main_conf_t *lmcf; + ngx_http_lua_shdict_ctx_t *ctx = NULL; + ngx_shm_zone_t *zone; + ngx_shm_zone_t **zp; + + lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module); + + if (lmcf->shdict_zones == NULL) { + lmcf->shdict_zones = ngx_palloc(cf->pool, sizeof(ngx_array_t)); + if (lmcf->shdict_zones == NULL) { + return NGX_ERROR; + } + + if (ngx_array_init(lmcf->shdict_zones, cf->pool, 2, + sizeof(ngx_shm_zone_t *)) + != NGX_OK) + { + return NGX_ERROR; + } + } + + ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_lua_shdict_ctx_t)); + if (ctx == NULL) { + return NGX_ERROR; + } + + ctx->name = *name; + ctx->main_conf = lmcf; + ctx->log = &cf->cycle->new_log; + + zone = ngx_http_lua_shared_memory_add(cf, name, (size_t) size, + &ngx_http_lua_module); + if (zone == NULL) { + return NGX_ERROR; + } + + if (zone->data) { + return NGX_DECLINED; + } + + zone->init = ngx_http_lua_shdict_init_zone; + zone->data = ctx; + + zp = ngx_array_push(lmcf->shdict_zones); + if (zp == NULL) { + return NGX_ERROR; + } + + *zp = zone; + + return NGX_OK; +} + + ngx_int_t ngx_http_lua_shdict_init_zone(ngx_shm_zone_t *shm_zone, void *data) { @@ -323,6 +380,15 @@ ngx_http_lua_shdict_expire(ngx_http_lua_shdict_ctx_t *ctx, ngx_uint_t n) void ngx_http_lua_create_shdict_mt(lua_State *L) { + /* ngx ngx.shared */ + + if (lua_getmetatable(L, -1)) { + /* when no lua_shared_dict directives have been set, but we + * we add a dict via the configure phase, we lazily create the + * shdict mt. This avoids creating it multiple times. */ + return; + } + lua_createtable(L, 0 /* narr */, 18 /* nrec */); /* ngx ngx.shared shmt */ @@ -383,7 +449,8 @@ ngx_http_lua_create_shdict_mt(lua_State *L) void -ngx_http_lua_attach_shdict(lua_State *L, ngx_str_t *name, ngx_shm_zone_t *zone) +ngx_http_lua_attach_shdict(lua_State *L, ngx_str_t *name, + ngx_shm_zone_t *zone) { lua_pushlstring(L, (char *) name->data, name->len); /* 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) /* ngx ngx.shared */ ngx_http_lua_create_shdict_mt(L); - /* ngx ngx.shared shmt */ zone = lmcf->shdict_zones->elts; diff --git a/src/ngx_http_lua_shdict.h b/src/ngx_http_lua_shdict.h index 9d83c07ef5..1b40b8a1bb 100644 --- a/src/ngx_http_lua_shdict.h +++ b/src/ngx_http_lua_shdict.h @@ -58,6 +58,8 @@ typedef struct { } ngx_http_lua_shm_zone_ctx_t; +ngx_int_t ngx_http_lua_shared_dict_add(ngx_conf_t *cf, ngx_str_t *name, + ssize_t size); ngx_int_t ngx_http_lua_shdict_init_zone(ngx_shm_zone_t *shm_zone, void *data); void ngx_http_lua_shdict_rbtree_insert_value(ngx_rbtree_node_t *temp, ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);