From 85503cf92bc0641ba8332d116d3c7f924d38d5e4 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Fri, 19 Oct 2018 11:45:05 -0300 Subject: [PATCH] retry with a delay increasing from 0 up to 10 seconds --- torba/basenetwork.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/torba/basenetwork.py b/torba/basenetwork.py index 50727ea92..ed0bd0dc6 100644 --- a/torba/basenetwork.py +++ b/torba/basenetwork.py @@ -1,6 +1,7 @@ import logging -from itertools import cycle +import asyncio from asyncio import CancelledError +from itertools import cycle from aiorpcx import ClientSession as BaseClientSession @@ -51,6 +52,7 @@ class BaseNetwork: async def start(self): self.running = True + delay = 0.0 for server in cycle(self.config['default_servers']): connection_string = '{}:{}'.format(*server) self.client = ClientSession(*server, network=self) @@ -59,6 +61,7 @@ class BaseNetwork: await self.ensure_server_version() log.info("Successfully connected to SPV wallet server: %s", connection_string) self._on_connected_controller.add(True) + delay = 0.0 await self.client.on_disconnected.first except (Exception, CancelledError): # pylint: disable=broad-except log.exception("Connecting to %s raised an exception:", connection_string) @@ -67,6 +70,8 @@ class BaseNetwork: elif self.client: await self.client.close() self.client.connection.cancel_pending_requests() + await asyncio.sleep(delay) + delay = min(delay + 1.0, 10.0) async def stop(self): self.running = False