mod_pinger/mod_pinger.lua
author Kim Alvefur <zash@zash.se>
Thu, 28 Dec 2017 22:30:56 +0100
changeset 2858 687b19cad4f5
parent 2678 c971b2cee2cc
child 3107 5bf79bb3cf7e
permissions -rw-r--r--
mod_storage_xmlarchive/README: Add description of how data is stored
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2038
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
local new_watchdog = require "util.watchdog".new;
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
local filters = require "util.filters";
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
local st = require "util.stanza";
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
local idle_timeout = module:get_option_number("c2s_idle_timeout", 300);
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
local ping_timeout = module:get_option_number("c2s_ping_timeout",  30);
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
function update_watchdog(data, session)
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
	session.idle_watchdog:reset();
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
	session.idle_pinged = nil;
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
	return data;
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
end
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
function check_session(watchdog)
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
	local session = watchdog.session;
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
	if not session.idle_pinged then
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
		session.idle_pinged = true;
2678
c971b2cee2cc mod_pinger: Try to solve smacks related issues, see #712
tmolitor <thilo@eightysoft.de>
parents: 2676
diff changeset
    18
		if session.smacks then
c971b2cee2cc mod_pinger: Try to solve smacks related issues, see #712
tmolitor <thilo@eightysoft.de>
parents: 2676
diff changeset
    19
			if not session.awaiting_ack then
2675
80b6c63cb559 mod_pinger: Fix hardcoded smacks namespace, fixes #712
tmolitor <thilo@eightysoft.de>
parents: 2038
diff changeset
    20
				session.send(st.stanza("r", { xmlns = session.smacks }))
2678
c971b2cee2cc mod_pinger: Try to solve smacks related issues, see #712
tmolitor <thilo@eightysoft.de>
parents: 2676
diff changeset
    21
			end
2675
80b6c63cb559 mod_pinger: Fix hardcoded smacks namespace, fixes #712
tmolitor <thilo@eightysoft.de>
parents: 2038
diff changeset
    22
		else
2678
c971b2cee2cc mod_pinger: Try to solve smacks related issues, see #712
tmolitor <thilo@eightysoft.de>
parents: 2676
diff changeset
    23
			session.send(st.iq({ type = "get", from = module.host, id = "idle-check" })
c971b2cee2cc mod_pinger: Try to solve smacks related issues, see #712
tmolitor <thilo@eightysoft.de>
parents: 2676
diff changeset
    24
					:tag("ping", { xmlns = "urn:xmpp:ping" }));
2675
80b6c63cb559 mod_pinger: Fix hardcoded smacks namespace, fixes #712
tmolitor <thilo@eightysoft.de>
parents: 2038
diff changeset
    25
		end
2038
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
		return ping_timeout; -- Call us again after ping_timeout
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
	else
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
		module:log("info", "Client %q silent for too long, closing...", session.full_jid);
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
		session:close("connection-timeout");
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
	end
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
end
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
function watch_session(session)
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
	if not session.idle_watchdog
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
	and not session.requests then -- Don't watch BOSH connections (BOSH already has timeouts)
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
		session.idle_watchdog = new_watchdog(idle_timeout, check_session);
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
		session.idle_watchdog.session = session;
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
		filters.add_filter(session, "bytes/in", update_watchdog);
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
	end
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
end
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
function unwatch_session(session)
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
	if session.idle_watchdog then
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
		session.idle_watchdog:cancel();
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
		session.idle_watchdog = nil;
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
		filters.remove_filter(session, "bytes/in", update_watchdog);
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
	end
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
end
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
module:hook("resource-bind", function (event) watch_session(event.session); end);
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
module:hook("resource-unbind", function (event) unwatch_session(event.session); end);
2676
75ab061069aa mod_pinger: Don't ping when smacks hibernated the session, fixes #712
tmolitor <thilo@eightysoft.de>
parents: 2675
diff changeset
    53
75ab061069aa mod_pinger: Don't ping when smacks hibernated the session, fixes #712
tmolitor <thilo@eightysoft.de>
parents: 2675
diff changeset
    54
-- handle smacks sessions properly (not pinging in hibernated state)
75ab061069aa mod_pinger: Don't ping when smacks hibernated the session, fixes #712
tmolitor <thilo@eightysoft.de>
parents: 2675
diff changeset
    55
module:hook("smacks-hibernation-start", function (event) unwatch_session(event.origin); end);
2678
c971b2cee2cc mod_pinger: Try to solve smacks related issues, see #712
tmolitor <thilo@eightysoft.de>
parents: 2676
diff changeset
    56
module:hook("smacks-hibernation-end", function (event) watch_session(event.resumed); end);