# HG changeset patch # User Matthew Wild # Date 1264700429 0 # Node ID 6c454d7208ae92ec486b2f987d1a95a7c7351f35 # Parent e190c1643a8ff07539820c0c78a3764dead64ea1 mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s diff -r e190c1643a8f -r 6c454d7208ae mod_s2s_idle_timeout/mod_s2s_idle_timeout.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_s2s_idle_timeout/mod_s2s_idle_timeout.lua Thu Jan 28 17:40:29 2010 +0000 @@ -0,0 +1,53 @@ +local now = os.time; + +local s2smanager = require "core.s2smanager"; +local timer = require "util.timer"; + +local s2s_sessions = setmetatable({}, { __mode = "kv" }); + +local idle_timeout = module:get_option("s2s_idle_timeout") or 300; +local check_interval = math.ceil(idle_timeout * 0.75); +local _make_authenticated = s2smanager.make_authenticated; +function s2smanager.make_authenticated(session, host) + if not session.last_received_time then + session.last_received_time = now(); + if session.direction == "incoming" then + local _data = session.data; + function session.data(conn, data) + session.last_received_time = now(); + return _data(conn, data); + end + else + local _sends2s = session.sends2s; + function session.sends2s(data) + session.last_received_time = now(); + return _sends2s(data); + end + end + s2s_sessions[session] = true; + end + return _make_authenticated(session, host); +end + +function check_idle_sessions(time) + time = time or now(); + for session in pairs(s2s_sessions) do + local last_received_time = session.last_received_time; + if last_received_time and time - last_received_time > idle_timeout then + module:log("debug", "Closing idle connection %s->%s", + session.from_host or "(unknown)", session.to_host or "(unknown)"); + session:close(); -- Close-on-idle isn't an error + s2s_sessions[session] = nil; + end + end + return check_interval; +end +timer.add_task(check_interval, check_idle_sessions); + +function module.save() + return { s2s_sessions = s2s_sessions }; +end + +function module.restore(data) + s2s_sessions = setmetatable(data.s2s_sessions or {}, { __mode = "kv" }); +end