Use a timer to try reconnecting to the XMPP server only each 2 seconds

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”.
This commit is contained in:
Florent Le Coz
2015-02-21 06:45:11 +01:00
parent 3032dc3580
commit a50ca30e76
6 changed files with 38 additions and 4 deletions
+21 -2
View File
@@ -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;