commit 4984890265f0f8a83c8bca23be318a02abb7f8b2
parent 6b8ad2d1263124fcc157536bd41c219736d552ae
Author: SomberNight <somber.night@protonmail.com>
Date: Thu, 27 Sep 2018 20:04:36 +0200
follow-up prev: make best_effort_reliable react faster to disconnects
Diffstat:
2 files changed, 11 insertions(+), 8 deletions(-)
diff --git a/electrum/interface.py b/electrum/interface.py
@@ -132,6 +132,7 @@ class Interface(PrintError):
def __init__(self, network, server, config_path, proxy):
self.exception = None
self.ready = asyncio.Future()
+ self.got_disconnected = asyncio.Future()
self.server = server
self.host, self.port, self.protocol = deserialize_server(self.server)
self.port = int(self.port)
@@ -246,6 +247,7 @@ class Interface(PrintError):
self.print_error("disconnecting gracefully. {}".format(e))
finally:
await self.network.connection_down(self.server)
+ self.got_disconnected.set_result(1)
return wrapper_func
@aiosafe
diff --git a/electrum/network.py b/electrum/network.py
@@ -216,7 +216,7 @@ class Network(PrintError):
# kick off the network. interface is the main server we are currently
# communicating with. interfaces is the set of servers we are connecting
# to or have an ongoing connection with
- self.interface = None
+ self.interface = None # type: Interface
self.interfaces = {}
self.auto_connect = self.config.get('auto_connect', True)
self.connecting = set()
@@ -647,13 +647,14 @@ class Network(PrintError):
# no main interface; try again
await asyncio.sleep(0.1)
continue
- try:
- return await func(self, *args, **kwargs)
- except RequestTimedOut:
- if self.interface != iface:
- # main interface changed; try again
- continue
- raise
+ success_fut = asyncio.ensure_future(func(self, *args, **kwargs))
+ disconnected_fut = asyncio.shield(iface.got_disconnected)
+ await asyncio.wait([success_fut, disconnected_fut], return_when=asyncio.FIRST_COMPLETED)
+ if success_fut.done():
+ if success_fut.exception():
+ raise success_fut.exception()
+ return success_fut.result()
+ # otherwise; try again
raise Exception('no interface to do request on... gave up.')
return make_reliable_wrapper