Software /
code /
prosody-modules
Comparison
mod_s2s_idle_timeout/mod_s2s_idle_timeout.lua @ 127:6c454d7208ae
mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 28 Jan 2010 17:40:29 +0000 |
child | 932:4e235e565693 |
comparison
equal
deleted
inserted
replaced
126:e190c1643a8f | 127:6c454d7208ae |
---|---|
1 local now = os.time; | |
2 | |
3 local s2smanager = require "core.s2smanager"; | |
4 local timer = require "util.timer"; | |
5 | |
6 local s2s_sessions = setmetatable({}, { __mode = "kv" }); | |
7 | |
8 local idle_timeout = module:get_option("s2s_idle_timeout") or 300; | |
9 local check_interval = math.ceil(idle_timeout * 0.75); | |
10 local _make_authenticated = s2smanager.make_authenticated; | |
11 function s2smanager.make_authenticated(session, host) | |
12 if not session.last_received_time then | |
13 session.last_received_time = now(); | |
14 if session.direction == "incoming" then | |
15 local _data = session.data; | |
16 function session.data(conn, data) | |
17 session.last_received_time = now(); | |
18 return _data(conn, data); | |
19 end | |
20 else | |
21 local _sends2s = session.sends2s; | |
22 function session.sends2s(data) | |
23 session.last_received_time = now(); | |
24 return _sends2s(data); | |
25 end | |
26 end | |
27 s2s_sessions[session] = true; | |
28 end | |
29 return _make_authenticated(session, host); | |
30 end | |
31 | |
32 function check_idle_sessions(time) | |
33 time = time or now(); | |
34 for session in pairs(s2s_sessions) do | |
35 local last_received_time = session.last_received_time; | |
36 if last_received_time and time - last_received_time > idle_timeout then | |
37 module:log("debug", "Closing idle connection %s->%s", | |
38 session.from_host or "(unknown)", session.to_host or "(unknown)"); | |
39 session:close(); -- Close-on-idle isn't an error | |
40 s2s_sessions[session] = nil; | |
41 end | |
42 end | |
43 return check_interval; | |
44 end | |
45 timer.add_task(check_interval, check_idle_sessions); | |
46 | |
47 function module.save() | |
48 return { s2s_sessions = s2s_sessions }; | |
49 end | |
50 | |
51 function module.restore(data) | |
52 s2s_sessions = setmetatable(data.s2s_sessions or {}, { __mode = "kv" }); | |
53 end |