tinterface: throttle messages - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit cad4fb80c188e23061b220e0761db6d977e6c4b6 DIR parent 47a97279af33295a813e7194c4d2d5e771443e51 HTML Author: SomberNight <somber.night@protonmail.com> Date: Wed, 12 Sep 2018 20:17:12 +0200 interface: throttle messages Diffstat: M electrum/interface.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) --- DIR diff --git a/electrum/interface.py b/electrum/interface.py t@@ -49,6 +49,7 @@ class NotificationSession(ClientSession): super(NotificationSession, self).__init__(*args, **kwargs) self.subscriptions = defaultdict(list) self.cache = {} + self.in_flight_requests_semaphore = asyncio.Semaphore(100) async def handle_request(self, request): # note: if server sends malformed request and we raise, the superclass t@@ -64,11 +65,14 @@ class NotificationSession(ClientSession): assert False, request.method async def send_request(self, *args, timeout=-1, **kwargs): + # note: the timeout starts after the request touches the wire! if timeout == -1: timeout = 20 if not self.proxy else 30 - return await asyncio.wait_for( - super().send_request(*args, **kwargs), - timeout) + # note: the semaphore implementation guarantees no starvation + async with self.in_flight_requests_semaphore: + return await asyncio.wait_for( + super().send_request(*args, **kwargs), + timeout) async def subscribe(self, method, params, queue): # note: until the cache is written for the first time,