From a50ca30e769a628f609f8cc0eedf5bc10b3f1b5a Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sat, 21 Feb 2015 06:39:20 +0100 Subject: Use a timer to try reconnecting to the XMPP server only each 2 seconds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the connection is lost, immediately try to reconnect, then try to reconnect every 2 seconds. This is much better than the previous “Try to re-connect as fast as possible”. --- src/main.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 393bf05..a67baf9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -108,6 +108,8 @@ int main(int ac, char** av) exiting = true; stop.store(false); xmpp_component->shutdown(); + // Cancel the timer for an potential reconnection + TimedEventsManager::instance().cancel("XMPP reconnection"); } if (reload) { @@ -127,17 +129,34 @@ int main(int ac, char** av) if (!exiting && xmpp_component->ever_auth && !xmpp_component->is_connected() && !xmpp_component->is_connecting()) - { + { + if (xmpp_component->first_connection_try == true) + { // immediately re-try to connect xmpp_component->reset(); xmpp_component->start(); } + else + { // Re-connecting failed, we now try only each few seconds + auto reconnect_later = [xmpp_component]() + { + xmpp_component->reset(); + xmpp_component->start(); + }; + TimedEvent event(std::chrono::steady_clock::now() + 2s, + reconnect_later, "XMPP reconnection"); + TimedEventsManager::instance().add_event(std::move(event)); + } + } // If the only existing connection is the one to the XMPP component: // close the XMPP stream. if (exiting && xmpp_component->is_connecting()) xmpp_component->close(); if (exiting && p->size() == 1 && xmpp_component->is_document_open()) xmpp_component->close_document(); - timeout = TimedEventsManager::instance().get_timeout(); + if (exiting) // If we are exiting, do not wait for any timed event + timeout = utils::no_timeout; + else + timeout = TimedEventsManager::instance().get_timeout(); } log_info("All connections cleanly closed, have a nice day."); return 0; -- cgit v1.2.3