Add a waiting time before reconnecting automatically

Punishing a server for being down by sending more traffic is not a nice
thing to do. Taking 100% of the CPU is not nice either.
This commit is contained in:
mathieui 2015-07-21 00:57:22 +02:00
parent 84eddd2ed2
commit 2ce931cb7a
No known key found for this signature in database
GPG key ID: C59F84CEEFD616E3

View file

@ -72,6 +72,8 @@ class XMLStream(asyncio.BaseProtocol):
# The socket the is used internally by the transport object # The socket the is used internally by the transport object
self.socket = None self.socket = None
self.connect_loop_wait = 0
self.parser = None self.parser = None
self.xml_depth = 0 self.xml_depth = 0
self.xml_root = None self.xml_root = None
@ -301,6 +303,7 @@ class XMLStream(asyncio.BaseProtocol):
# and try (host, port) as a last resort # and try (host, port) as a last resort
self.dns_answers = None self.dns_answers = None
yield from asyncio.sleep(self.connect_loop_wait)
try: try:
yield from self.loop.create_connection(lambda: self, yield from self.loop.create_connection(lambda: self,
self.address[0], self.address[0],
@ -312,7 +315,10 @@ class XMLStream(asyncio.BaseProtocol):
except OSError as e: except OSError as e:
log.debug('Connection failed: %s', e) log.debug('Connection failed: %s', e)
self.event("connection_failed", e) self.event("connection_failed", e)
self.connect_loop_wait = self.connect_loop_wait * 2 + 1
asyncio.async(self._connect_routine()) asyncio.async(self._connect_routine())
else:
self.connect_loop_wait = 0
def process(self, *, forever=True, timeout=None): def process(self, *, forever=True, timeout=None):
"""Process all the available XMPP events (receiving or sending data on the """Process all the available XMPP events (receiving or sending data on the