From 9353c13d65408ac89aac175244faf02fbb47206c Mon Sep 17 00:00:00 2001 From: junglecow Date: Fri, 8 Dec 2006 21:19:01 +0000 Subject: [PATCH] - Trying to connect to server every 20 seconds forever is extremely rude to its owner. Let's be polite and do exponential back-off. (See #2411) - [PyOpenSSL] Ported [7443] to pyopenssl branch. --- src/common/connection.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/common/connection.py b/src/common/connection.py index d8cd978a5..f66c896bc 100644 --- a/src/common/connection.py +++ b/src/common/connection.py @@ -20,7 +20,12 @@ import os import random -random.seed() + +try: + randomsource = random.SystemRandom() +except: + randomsource = random.Random() + randomsource.seed() import signal if os.name != 'nt': @@ -56,6 +61,7 @@ class Connection(ConnectionHandlers): # holds the actual hostname to which we are connected self.connected_hostname = None self.time_to_reconnect = None + self.last_time_to_reconnect = None self.new_account_info = None self.bookmarks = [] self.annotations = {} @@ -140,10 +146,15 @@ class Connection(ConnectionHandlers): self.connected = 1 self.dispatch('STATUS', 'connecting') # this check has moved from _reconnect method - if self.retrycount > 5: - self.time_to_reconnect = random.randint(15, 25) - else: - self.time_to_reconnect = random.randint(5, 15) + # do exponential backoff until 15 minutes, + # then small linear increase + if self.retrycount < 2 or self.last_time_to_reconnect is None: + self.last_time_to_reconnect = 5 + if self.last_time_to_reconnect < 800: + self.last_time_to_reconnect *= 1.5 + self.last_time_to_reconnect += randomsource.randint(0, 5) + self.time_to_reconnect = int(self.last_time_to_reconnect) + gajim.log.debug("Reconnect to %s in %ss", self.name, self.time_to_reconnect) gajim.idlequeue.set_alarm(self._reconnect_alarm, self.time_to_reconnect) elif self.on_connect_failure: