From f3c1a77d7a4814f3dd3be5dae6d530471a8d58a5 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 18 Oct 2014 00:32:21 +0200 Subject: moved to resty-lock for fetch --- src/daq/nginx-lua/s5-nginx-fetch.lua | 41 +++++++++++++----------------------- src/daq/nginx-lua/s5-nginx-init.lua | 31 ++++++++++++++++++++------- src/daq/nginx-lua/s5-nginx-log.lua | 4 ++-- 3 files changed, 40 insertions(+), 36 deletions(-) (limited to 'src/daq/nginx-lua') diff --git a/src/daq/nginx-lua/s5-nginx-fetch.lua b/src/daq/nginx-lua/s5-nginx-fetch.lua index 8bfcf07..8821094 100644 --- a/src/daq/nginx-lua/s5-nginx-fetch.lua +++ b/src/daq/nginx-lua/s5-nginx-fetch.lua @@ -33,52 +33,41 @@ -- Install this by adding the following to your nginx.conf -- -- location /sfive { --- allow localhost; +-- allow 127.0.0.1; +-- allow ::1; -- deny all; -- --- lua_check_client_abort on; -- content_by_lua_file '/path/to/s5-nginx-fetch.lua'; -- } -- -local sfive = ngx.shared.sfive - -local function fetch_cleanup() - ngx.log(ngx.INFO, "SFive(fetch): cleanup fetch") - sfive:delete("fetch:connected") -end - -local ok, err, force = sfive:add("fetch:connected", 1) --- TODO: race condition: when client disconnects here we won't free up the singleton... +local lock = locks:new("sfive_locks", { exptime = nil, timeout = 0.001 }) +local ok, err = lock:lock("fetch") if not ok then - if err == "exists" then + if err == "timeout" then ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE) else - ngx.log(ngx.ERR, "SFive(fetch): failed to set connected flag: " .. err) + ngx.log(ngx.ERR, "SFive(fetch): failed to acquire fetch lock: " .. err) ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) end else - local ok, err = ngx.on_abort(fetch_cleanup) - if not ok then - ngx.log(ngx.ERR, "SFive(fetch): failed to register the on_abort callback: ", err) - ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) - end - ngx.log(ngx.INFO, "SFive(fetch): client connected") - - local sfive_log = ngx.shared.sfive_log + local sfive_data = ngx.shared.sfive_data while true do - logs = sfive_log:get_keys() + logs = sfive_data:get_keys() for i, k in ipairs(logs) do - local ok, err = ngx.say(sfive_log:get(k)) + local ok, err = ngx.say(sfive_data:get(k)) if not ok then - ngx.log(ngx.ERR, "SFive(fetch): failed to register the on_abort callback: ", err) + ngx.log(ngx.ERR, "SFive(fetch): failed to send data set: ", err) break end - sfive_log:delete(k) + sfive_data:delete(k) end ngx.flush() ngx.sleep(0.1) end - fetch_cleenup() + ok, err = lock:unlock("fetch") + if not ok then + ngx.log(ngx.ERR, "SFive(fetch): unlock error: " .. err) + end end diff --git a/src/daq/nginx-lua/s5-nginx-init.lua b/src/daq/nginx-lua/s5-nginx-init.lua index 9d73603..7270e23 100644 --- a/src/daq/nginx-lua/s5-nginx-init.lua +++ b/src/daq/nginx-lua/s5-nginx-init.lua @@ -34,22 +34,37 @@ -- -- http { -- lua_shared_dict sfive 64k; --- lua_shared_dict sfive_log 64m; +-- lua_shared_dict sfive_locks 64k; +-- lua_shared_dict sfive_data 64m; -- init_by_lua_file '/path/to/s5-nginx-init.lua'; -- } -- ngx.log(ngx.DEBUG, "SFive: loaded") + +locks = require "resty.lock" + local sfive = ngx.shared.sfive sfive:flush_all() sfive:flush_expired() -local sfive_log = ngx.shared.sfive_log -sfive_log:flush_all() -sfive_log:flush_expired() +local sfive_data = ngx.shared.sfive_data +sfive_data:flush_all() +sfive_data:flush_expired() -local ok, err, force = sfive:set("log:idx", 0) -if not ok then - ngx.log(ngx.ERR, "SFive(init): creating log index counter failed: " .. err) +-- try to create locks so we get an error on init and not on first usage +local lock = locks:new("sfive_locks") +local elapsed, err = lock:lock("test") +if not elapsed then + ngx.log(ngx.ERR, "SFive(init): lock error: " .. err) else - ngx.log(ngx.INFO, "SFive(init): initialized successfully!") + local ok, err, force = sfive:set("log:idx", 0) + if not ok then + ngx.log(ngx.ERR, "SFive(init): creating log index counter failed: " .. err) + else + ngx.log(ngx.INFO, "SFive(init): initialized successfully!") + end + ok, err = lock:unlock("test") + if not ok then + ngx.log(ngx.ERR, "SFive(init): unlock error: " .. err) + end end diff --git a/src/daq/nginx-lua/s5-nginx-log.lua b/src/daq/nginx-lua/s5-nginx-log.lua index daad0f4..43d2394 100644 --- a/src/daq/nginx-lua/s5-nginx-log.lua +++ b/src/daq/nginx-lua/s5-nginx-log.lua @@ -88,8 +88,8 @@ if status == '200' or status == '206' then json = json .. '"bytes_sent": ' .. ngx.var.bytes_sent json = json .. '}' - local sfive_log = ngx.shared.sfive_log - local ok, err, force = sfive_log:add(idx, json, log_exptime) + local sfive_data = ngx.shared.sfive_data + local ok, err, force = sfive_data:add(idx, json, log_exptime) if not ok then ngx.log(ngx.ERR, "SFive(log): adding log line (".. idx .. ") to log store failed: " .. err) elseif force then -- cgit v1.2.3