From 1ff2dea44d0281a16c7f6c911a1f7fa147ed5d60 Mon Sep 17 00:00:00 2001 From: splitice Date: Mon, 28 Dec 2015 22:51:10 +1100 Subject: [PATCH 1/4] shared dictionary incr method exptime added to FFI api --- lib/resty/core/shdict.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/resty/core/shdict.lua b/lib/resty/core/shdict.lua index de811ae47..e7ab5cadd 100644 --- a/lib/resty/core/shdict.lua +++ b/lib/resty/core/shdict.lua @@ -26,7 +26,7 @@ ffi.cdef[[ int get_stale, int *is_stale); int ngx_http_lua_ffi_shdict_incr(void *zone, const unsigned char *key, - size_t key_len, double *value, char **err); + size_t key_len, double *value, int exptime, char **err); int ngx_http_lua_ffi_shdict_store(void *zone, int op, const unsigned char *key, size_t key_len, int value_type, @@ -311,7 +311,7 @@ local function shdict_get_stale(zone, key) end -local function shdict_incr(zone, key, value) +local function shdict_incr(zone, key, value, exptime) zone = check_zone(zone) if key == nil then @@ -334,9 +334,11 @@ local function shdict_incr(zone, key, value) value = tonumber(value) end num_value[0] = value + + exptime = exptime or -1 local rc = C.ngx_http_lua_ffi_shdict_incr(zone, key, key_len, num_value, - errmsg) + exptime, errmsg) if rc ~= 0 then -- ~= NGX_OK return nil, ffi_str(errmsg[0]) end From ff6287f9677126ba9b8dabf76804abccc3671a27 Mon Sep 17 00:00:00 2001 From: splitice Date: Mon, 28 Dec 2015 22:52:40 +1100 Subject: [PATCH 2/4] add simple shared dictionary test --- t/shdict.t | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/t/shdict.t b/t/shdict.t index c62e25eaf..3be23f687 100644 --- a/t/shdict.t +++ b/t/shdict.t @@ -915,3 +915,27 @@ qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):7 loop\]/ -- NYI: stitch + + + +=== TEST 27: incr key expire +--- http_config eval: $::HttpConfig +--- config + location = /t { + content_by_lua ' + local val, flags + local dogs = ngx.shared.dogs + local value, err = dogs:incr(nil, 32, 10) + if not ok then + ngx.say("failed to incr: ", err) + end + '; + } +--- request +GET /t +--- response_body +failed to incr: nil key +--- no_error_log +[error] +[alert] +[crit] \ No newline at end of file From d78c8a3d7030c17c9cc595a6d80bc067552ade6b Mon Sep 17 00:00:00 2001 From: splitice Date: Thu, 4 Feb 2016 19:26:21 +1100 Subject: [PATCH 3/4] initial creation of two balancer modules --- lib/ngx/{balancer.lua => balancer_http.lua} | 0 lib/ngx/balancer_stream.lua | 82 +++++++++++++++++++++ 2 files changed, 82 insertions(+) rename lib/ngx/{balancer.lua => balancer_http.lua} (100%) create mode 100644 lib/ngx/balancer_stream.lua diff --git a/lib/ngx/balancer.lua b/lib/ngx/balancer_http.lua similarity index 100% rename from lib/ngx/balancer.lua rename to lib/ngx/balancer_http.lua diff --git a/lib/ngx/balancer_stream.lua b/lib/ngx/balancer_stream.lua new file mode 100644 index 000000000..fffa39a04 --- /dev/null +++ b/lib/ngx/balancer_stream.lua @@ -0,0 +1,82 @@ +-- Copyright (C) Yichun Zhang (agentzh) + + +local ffi = require "ffi" +local base = require "resty.core.base" + + +local C = ffi.C +local ffi_str = ffi.string +local errmsg = base.get_errmsg_ptr() +local FFI_OK = base.FFI_OK +local FFI_ERROR = base.FFI_ERROR +local int_out = ffi.new("int[1]") +local getfenv = getfenv +local error = error +local type = type +local tonumber = tonumber + + +ffi.cdef[[ +int ngx_stream_lua_ffi_balancer_set_current_peer(ngx_stream_session_t *r, + const unsigned char *addr, size_t addr_len, int port, char **err); + +int ngx_stream_lua_ffi_balancer_set_more_tries(ngx_stream_session_t *r, + int count, char **err); + +int ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_session_t *r, + int *status, char **err); +]] + + +local peer_state_names = { + [1] = "keepalive", + [2] = "next", + [4] = "failed", +} + + +local _M = { version = base.version } + + +function _M.set_current_peer(addr, port) + local r = getfenv(0).__ngx_sess + if not r then + return error("no request found") + end + + if not port then + port = 0 + elseif type(port) ~= "number" then + port = tonumber(port) + end + + local rc = C.ngx_stream_lua_ffi_balancer_set_current_peer(r, addr, #addr, + port, errmsg) + if rc == FFI_OK then + return true + end + + return nil, ffi_str(errmsg[0]) +end + + +function _M.set_more_tries(count) + local r = getfenv(0).__ngx_sess + if not r then + return error("no request found") + end + + local rc = C.ngx_stream_lua_ffi_balancer_set_more_tries(r, count, errmsg) + if rc == FFI_OK then + if errmsg[0] == nil then + return true + end + return true, ffi_str(errmsg[0]) -- return the warning + end + + return nil, ffi_str(errmsg[0]) +end + + +return _M From ce47583ef7a3eb798195f544e91161dcecf81c16 Mon Sep 17 00:00:00 2001 From: splitice Date: Fri, 5 Feb 2016 10:19:34 +1100 Subject: [PATCH 4/4] add missing typedef --- lib/resty/core/base.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/resty/core/base.lua b/lib/resty/core/base.lua index 452708d8e..6de5fa9e3 100644 --- a/lib/resty/core/base.lua +++ b/lib/resty/core/base.lua @@ -81,6 +81,14 @@ if not pcall(ffi.typeof, "ngx_http_request_t") then end +if not pcall(ffi.typeof, "ngx_stream_session_t") then + ffi.cdef[[ + struct ngx_stream_session_s; + typedef struct ngx_stream_session_s ngx_stream_session_t; + ]] +end + + if not pcall(ffi.typeof, "ngx_http_lua_ffi_str_t") then ffi.cdef[[ typedef struct {