From a4640ce9a9332efc0646474f612ad5e0027d4fc6 Mon Sep 17 00:00:00 2001 From: splitice Date: Thu, 29 Dec 2016 14:49:39 +1100 Subject: [PATCH 1/6] incr expire adrgument --- src/ngx_http_lua_shdict.c | 77 +++++++++++++++++++++++++++++----- t/043-shdict.t | 87 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+), 10 deletions(-) diff --git a/src/ngx_http_lua_shdict.c b/src/ngx_http_lua_shdict.c index d6cad7e5e2..4669d82fc7 100644 --- a/src/ngx_http_lua_shdict.c +++ b/src/ngx_http_lua_shdict.c @@ -1257,24 +1257,50 @@ ngx_http_lua_shdict_incr(lua_State *L) ngx_http_lua_shdict_node_t *sd; double num; double init = 0; + int has_init = 0; u_char *p; ngx_shm_zone_t *zone; double value; + lua_Number exptime = -1; ngx_rbtree_node_t *node; /* indicates whether to foricibly override other * valid entries */ int forcible = 0; ngx_queue_t *queue, *q; + ngx_time_t *tp; n = lua_gettop(L); - if (n != 3 && n != 4) { - return luaL_error(L, "expecting 3 or 4 arguments, but only seen %d", n); + if (n < 3) { + return luaL_error(L, "expecting at least 3 arguments, but only seen %d", n); + } + + if (n > 5) { + return luaL_error(L, "expecting no more than 5 arguments, but %d seen", n); } if (lua_type(L, 1) != LUA_TTABLE) { return luaL_error(L, "bad \"zone\" argument"); } + + if (n >= 4) { + if (!lua_isnil(L, 4)) { + init = luaL_checknumber(L, 4); + if (init < 0) { + return luaL_error(L, "bad \"init\" argument"); + } + has_init = 1; + } + + if (n >= 5) { + if (!lua_isnil(L, 5)) { + exptime = luaL_checknumber(L, 5); + if (exptime < 0) { + return luaL_error(L, "bad \"exptime\" argument"); + } + } + } + } zone = ngx_http_lua_shdict_get_zone(L, 1); if (zone == NULL) { @@ -1307,10 +1333,6 @@ ngx_http_lua_shdict_incr(lua_State *L) value = luaL_checknumber(L, 3); - if (n == 4) { - init = luaL_checknumber(L, 4); - } - dd("looking up key %.*s in shared dict %.*s", (int) key.len, key.data, (int) ctx->name.len, ctx->name.data); @@ -1326,7 +1348,7 @@ ngx_http_lua_shdict_incr(lua_State *L) if (rc == NGX_DECLINED || rc == NGX_DONE) { - if (n == 3) { + if (!has_init) { ngx_shmtx_unlock(&ctx->shpool->mutex); lua_pushnil(L); @@ -1376,6 +1398,18 @@ ngx_http_lua_shdict_incr(lua_State *L) ngx_queue_remove(&sd->queue); ngx_queue_insert_head(&ctx->sh->lru_queue, &sd->queue); + if (exptime > 0) { + dd("setting expire time to %d", exptime); + + tp = ngx_timeofday(); + sd->expires = (uint64_t)tp->sec * 1000 + tp->msec + + (uint64_t)(exptime * 1000); + + } else if (exptime == 0) { + dd("setting key to never expire"); + sd->expires = 0; + } + dd("setting value type to %d", (int) sd->value_type); p = sd->data + key.len; @@ -1476,9 +1510,19 @@ ngx_http_lua_shdict_incr(lua_State *L) setvalue: - sd->user_flags = 0; + if (exptime > 0) { + dd("setting expire time to %d", exptime); - sd->expires = 0; + tp = ngx_timeofday(); + sd->expires = (uint64_t)tp->sec * 1000 + tp->msec + + (uint64_t)(exptime * 1000); + + } else if (exptime == 0) { + dd("setting key to never expire"); + sd->expires = 0; + } + + sd->user_flags = 0; dd("setting value type to %d", LUA_TNUMBER); @@ -2622,7 +2666,7 @@ ngx_http_lua_ffi_shdict_get(ngx_shm_zone_t *zone, u_char *key, int ngx_http_lua_ffi_shdict_incr(ngx_shm_zone_t *zone, u_char *key, - size_t key_len, double *value, char **err, int has_init, double init, + size_t key_len, double *value, int exptime, char **err, int has_init, double init, int *forcible) { int i, n; @@ -2633,6 +2677,7 @@ ngx_http_lua_ffi_shdict_incr(ngx_shm_zone_t *zone, u_char *key, double num; ngx_rbtree_node_t *node; u_char *p; + ngx_time_t *tp; ngx_queue_t *queue, *q; if (zone == NULL) { @@ -2712,6 +2757,18 @@ ngx_http_lua_ffi_shdict_incr(ngx_shm_zone_t *zone, u_char *key, ngx_memcpy(p, (double *) &num, sizeof(double)); + if (exptime > 0) { + dd("setting expire time to %d", exptime); + + tp = ngx_timeofday(); + sd->expires = (uint64_t)tp->sec * 1000 + tp->msec + + (uint64_t)(exptime * 1000); + + } else if (exptime == 0) { + dd("setting key to never expire"); + sd->expires = 0; + } + ngx_shmtx_unlock(&ctx->shpool->mutex); *value = num; diff --git a/t/043-shdict.t b/t/043-shdict.t index 9183bf5e2e..a0de642c4a 100644 --- a/t/043-shdict.t +++ b/t/043-shdict.t @@ -2471,3 +2471,90 @@ error lua_shared_dict "dogs" is already defined as "dogs" --- error_log [emerg] + + + +=== TEST 94: incr expire test new key +--- http_config + lua_shared_dict dogs 1m; +--- config + location = /test { + content_by_lua ' + local dogs = ngx.shared.dogs + dogs:incr("foo", 32, 1, 0.5) + ngx.sleep(0.001) + ngx.say(dogs:get("foo")) + '; + } +--- request +GET /test +--- response_body +33 +--- no_error_log +[error] + + + +=== TEST 95: incr expire test new key expire +--- http_config + lua_shared_dict dogs 1m; +--- config + location = /test { + content_by_lua ' + local dogs = ngx.shared.dogs + dogs:incr("foo", 32, 1, 0.01) + ngx.sleep(0.02) + ngx.say(dogs:get("foo")) + '; + } +--- request +GET /test +--- response_body +nil +--- no_error_log +[error] + + + +=== TEST 96: incr expire test existing key expire +--- http_config + lua_shared_dict dogs 1m; +--- config + location = /test { + content_by_lua ' + local dogs = ngx.shared.dogs + dogs:incr("foo", 32, 1) + dogs:incr("foo", 32, nil, 0.01) + ngx.sleep(0.02) + ngx.say(dogs:get("foo")) + '; + } +--- request +GET /test +--- response_body +nil +--- no_error_log +[error] + + + +=== TEST 97: incr expire set no init +--- http_config + lua_shared_dict dogs 1m; +--- config + location = /test { + content_by_lua ' + local dogs = ngx.shared.dogs + dogs:incr("foo", 32, nil, 0.5) + ngx.sleep(0.01) + ngx.say(dogs:get("foo")) + '; + } +--- request +GET /test +--- response_body +nil +--- no_error_log +[error] + + From 7c4514fd83bcca9668af093b53cb6a59859c5e8e Mon Sep 17 00:00:00 2001 From: splitice Date: Thu, 29 Dec 2016 16:50:29 +1100 Subject: [PATCH 2/6] fix init path --- src/ngx_http_lua_shdict.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngx_http_lua_shdict.c b/src/ngx_http_lua_shdict.c index 4669d82fc7..e155c73312 100644 --- a/src/ngx_http_lua_shdict.c +++ b/src/ngx_http_lua_shdict.c @@ -1517,7 +1517,7 @@ ngx_http_lua_shdict_incr(lua_State *L) sd->expires = (uint64_t)tp->sec * 1000 + tp->msec + (uint64_t)(exptime * 1000); - } else if (exptime == 0) { + } else { dd("setting key to never expire"); sd->expires = 0; } From 2e760ece46de50034c408c63f902858573a86552 Mon Sep 17 00:00:00 2001 From: splitice Date: Fri, 30 Dec 2016 12:36:00 +1100 Subject: [PATCH 3/6] fix > 80 lines --- src/ngx_http_lua_shdict.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ngx_http_lua_shdict.c b/src/ngx_http_lua_shdict.c index e155c73312..f29e38fe9c 100644 --- a/src/ngx_http_lua_shdict.c +++ b/src/ngx_http_lua_shdict.c @@ -1272,11 +1272,13 @@ ngx_http_lua_shdict_incr(lua_State *L) n = lua_gettop(L); if (n < 3) { - return luaL_error(L, "expecting at least 3 arguments, but only seen %d", n); + return luaL_error(L, "expecting at least 3 arguments, but only seen %d", + n); } if (n > 5) { - return luaL_error(L, "expecting no more than 5 arguments, but %d seen", n); + return luaL_error(L, "expecting no more than 5 arguments, but %d seen", + n); } if (lua_type(L, 1) != LUA_TTABLE) { @@ -2666,8 +2668,8 @@ ngx_http_lua_ffi_shdict_get(ngx_shm_zone_t *zone, u_char *key, int ngx_http_lua_ffi_shdict_incr(ngx_shm_zone_t *zone, u_char *key, - size_t key_len, double *value, int exptime, char **err, int has_init, double init, - int *forcible) + size_t key_len, double *value, int exptime, char **err, int has_init, + double init, int *forcible) { int i, n; uint32_t hash; From 3aa45d4e0169be9a6b6cb8f15453b3fd530f2866 Mon Sep 17 00:00:00 2001 From: splitice Date: Fri, 30 Dec 2016 13:46:05 +1100 Subject: [PATCH 4/6] style --- src/ngx_http_lua_shdict.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/ngx_http_lua_shdict.c b/src/ngx_http_lua_shdict.c index f29e38fe9c..9eaff5cd1c 100644 --- a/src/ngx_http_lua_shdict.c +++ b/src/ngx_http_lua_shdict.c @@ -1273,18 +1273,18 @@ ngx_http_lua_shdict_incr(lua_State *L) if (n < 3) { return luaL_error(L, "expecting at least 3 arguments, but only seen %d", - n); + n); } - + if (n > 5) { return luaL_error(L, "expecting no more than 5 arguments, but %d seen", - n); + n); } if (lua_type(L, 1) != LUA_TTABLE) { return luaL_error(L, "bad \"zone\" argument"); } - + if (n >= 4) { if (!lua_isnil(L, 4)) { init = luaL_checknumber(L, 4); @@ -1404,8 +1404,8 @@ ngx_http_lua_shdict_incr(lua_State *L) dd("setting expire time to %d", exptime); tp = ngx_timeofday(); - sd->expires = (uint64_t)tp->sec * 1000 + tp->msec - + (uint64_t)(exptime * 1000); + sd->expires = (uint64_t) tp->sec * 1000 + tp->msec + + (uint64_t) (exptime * 1000); } else if (exptime == 0) { dd("setting key to never expire"); @@ -1516,8 +1516,8 @@ ngx_http_lua_shdict_incr(lua_State *L) dd("setting expire time to %d", exptime); tp = ngx_timeofday(); - sd->expires = (uint64_t)tp->sec * 1000 + tp->msec - + (uint64_t)(exptime * 1000); + sd->expires = (uint64_t) tp->sec * 1000 + tp->msec + + (uint64_t) (exptime * 1000); } else { dd("setting key to never expire"); @@ -2668,7 +2668,7 @@ ngx_http_lua_ffi_shdict_get(ngx_shm_zone_t *zone, u_char *key, int ngx_http_lua_ffi_shdict_incr(ngx_shm_zone_t *zone, u_char *key, - size_t key_len, double *value, int exptime, char **err, int has_init, + size_t key_len, double *value, int exptime, char **err, int has_init, double init, int *forcible) { int i, n; From 6207e3657a93ea5d08d145d2f48377793782aea3 Mon Sep 17 00:00:00 2001 From: splitice Date: Fri, 30 Dec 2016 13:47:51 +1100 Subject: [PATCH 5/6] style --- src/ngx_http_lua_shdict.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ngx_http_lua_shdict.c b/src/ngx_http_lua_shdict.c index 9eaff5cd1c..4241aa66eb 100644 --- a/src/ngx_http_lua_shdict.c +++ b/src/ngx_http_lua_shdict.c @@ -1293,7 +1293,7 @@ ngx_http_lua_shdict_incr(lua_State *L) } has_init = 1; } - + if (n >= 5) { if (!lua_isnil(L, 5)) { exptime = luaL_checknumber(L, 5); @@ -2763,8 +2763,8 @@ ngx_http_lua_ffi_shdict_incr(ngx_shm_zone_t *zone, u_char *key, dd("setting expire time to %d", exptime); tp = ngx_timeofday(); - sd->expires = (uint64_t)tp->sec * 1000 + tp->msec - + (uint64_t)(exptime * 1000); + sd->expires = (uint64_t) tp->sec * 1000 + tp->msec + + (uint64_t) (exptime * 1000); } else if (exptime == 0) { dd("setting key to never expire"); From 285c498e4790e8ff429ea346c858bf81b1c8dc79 Mon Sep 17 00:00:00 2001 From: Mathew Heard Date: Tue, 10 Jan 2017 08:53:40 +1100 Subject: [PATCH 6/6] Style: add newline --- src/ngx_http_lua_shdict.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ngx_http_lua_shdict.c b/src/ngx_http_lua_shdict.c index 4241aa66eb..f420a96981 100644 --- a/src/ngx_http_lua_shdict.c +++ b/src/ngx_http_lua_shdict.c @@ -1291,6 +1291,7 @@ ngx_http_lua_shdict_incr(lua_State *L) if (init < 0) { return luaL_error(L, "bad \"init\" argument"); } + has_init = 1; }