view mod_pinger/mod_pinger.lua @ 2491:5fbca7de2088

mod_smacks: Send out more ack requests where needed Under some circumstances it was possible that more than "max_unacked_stanzas" where left in the outgoing stanza queue without forcing an ack. This could happen, when more stanzas entered the queue while the last ack request was still unanswered. Now the test "#queue > max_unacked_stanzas" is done upon receiving an ack as well as when sending out stanzas, which fixes this bug.
author tmolitor <thilo@eightysoft.de>
date Sun, 12 Feb 2017 19:27:50 +0100
parents 256a5e3591db
children 80b6c63cb559
line wrap: on
line source

local new_watchdog = require "util.watchdog".new;
local filters = require "util.filters";
local st = require "util.stanza";

local idle_timeout = module:get_option_number("c2s_idle_timeout", 300);
local ping_timeout = module:get_option_number("c2s_ping_timeout",  30);

function update_watchdog(data, session)
	session.idle_watchdog:reset();
	session.idle_pinged = nil;
	return data;
end

function check_session(watchdog)
	local session = watchdog.session;
	if not session.idle_pinged then
		session.idle_pinged = true;
               if session.smacks and not session.awaiting_ack then
                       session.send(st.stanza("r", { xmlns = "urn:xmpp:sm:2" })) -- TODO: hardcoded sm:2
               else
                       session.send(st.iq({ type = "get", from = module.host, id = "idle-check" })
                               :tag("ping", { xmlns = "urn:xmpp:ping" }));
               end
		return ping_timeout; -- Call us again after ping_timeout
	else
		module:log("info", "Client %q silent for too long, closing...", session.full_jid);
		session:close("connection-timeout");
	end
end


function watch_session(session)
	if not session.idle_watchdog
	and not session.requests then -- Don't watch BOSH connections (BOSH already has timeouts)
		session.idle_watchdog = new_watchdog(idle_timeout, check_session);
		session.idle_watchdog.session = session;
		filters.add_filter(session, "bytes/in", update_watchdog);
	end
end

function unwatch_session(session)
	if session.idle_watchdog then
		session.idle_watchdog:cancel();
		session.idle_watchdog = nil;
		filters.remove_filter(session, "bytes/in", update_watchdog);
	end
end

module:hook("resource-bind", function (event) watch_session(event.session); end);
module:hook("resource-unbind", function (event) unwatch_session(event.session); end);