annotate mod_s2s_idle_timeout/mod_s2s_idle_timeout.lua @ 812:0095b74a3685

mod_smacks: Avoid indexing a nil value
author Kim Alvefur <zash@zash.se>
date Wed, 12 Sep 2012 18:03:55 +0200
parents 6c454d7208ae
children 4e235e565693
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
127
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local now = os.time;
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local s2smanager = require "core.s2smanager";
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local timer = require "util.timer";
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local s2s_sessions = setmetatable({}, { __mode = "kv" });
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local idle_timeout = module:get_option("s2s_idle_timeout") or 300;
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local check_interval = math.ceil(idle_timeout * 0.75);
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local _make_authenticated = s2smanager.make_authenticated;
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 function s2smanager.make_authenticated(session, host)
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 if not session.last_received_time then
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 session.last_received_time = now();
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 if session.direction == "incoming" then
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local _data = session.data;
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 function session.data(conn, data)
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 session.last_received_time = now();
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 return _data(conn, data);
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 else
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local _sends2s = session.sends2s;
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 function session.sends2s(data)
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 session.last_received_time = now();
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return _sends2s(data);
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 s2s_sessions[session] = true;
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 return _make_authenticated(session, host);
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 end
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 function check_idle_sessions(time)
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 time = time or now();
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 for session in pairs(s2s_sessions) do
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 local last_received_time = session.last_received_time;
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 if last_received_time and time - last_received_time > idle_timeout then
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 module:log("debug", "Closing idle connection %s->%s",
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 session.from_host or "(unknown)", session.to_host or "(unknown)");
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 session:close(); -- Close-on-idle isn't an error
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 s2s_sessions[session] = nil;
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 return check_interval;
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 timer.add_task(check_interval, check_idle_sessions);
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 function module.save()
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 return { s2s_sessions = s2s_sessions };
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 function module.restore(data)
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 s2s_sessions = setmetatable(data.s2s_sessions or {}, { __mode = "kv" });
6c454d7208ae mod_s2s_idle_timeout: Close idle connections after s2s_idle_timeout seconds, default 300s
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end