summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2014-10-18 00:32:21 +0200
committerChristian Pointner <equinox@spreadspace.org>2014-10-18 00:32:21 +0200
commitf3c1a77d7a4814f3dd3be5dae6d530471a8d58a5 (patch)
treed3551ea4333ab65820a6e96685649cfb76283d99
parentadded proposal for protocol upgrade (diff)
moved to resty-lock for fetchold-lua-ngx
-rw-r--r--src/daq/nginx-lua/s5-nginx-fetch.lua41
-rw-r--r--src/daq/nginx-lua/s5-nginx-init.lua31
-rw-r--r--src/daq/nginx-lua/s5-nginx-log.lua4
3 files changed, 40 insertions, 36 deletions
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