@@ -531,13 +531,21 @@ def __init__(
531
531
self ._open_subscriptions = 0
532
532
self ._options = options if options else {}
533
533
self .last_received = time .time ()
534
+ self .last_sent = time .time ()
534
535
535
536
async def __aenter__ (self ):
536
537
async with self ._lock :
537
538
self ._in_use += 1
538
539
await self .connect ()
540
+ now = asyncio .get_running_loop ().time ()
541
+ self .last_received = now
542
+ self .last_sent = now
539
543
return self
540
544
545
+ @staticmethod
546
+ async def loop_time () -> float :
547
+ return asyncio .get_running_loop ().time ()
548
+
541
549
async def connect (self , force = False ):
542
550
if self ._exit_task :
543
551
self ._exit_task .cancel ()
@@ -594,7 +602,7 @@ async def _recv(self) -> None:
594
602
try :
595
603
# TODO consider wrapping this in asyncio.wait_for and use that for the timeout logic
596
604
response = json .loads (await self .ws .recv (decode = False ))
597
- self .last_received = time . time ()
605
+ self .last_received = await self . loop_time ()
598
606
async with self ._lock :
599
607
# note that these 'subscriptions' are all waiting sent messages which have not received
600
608
# responses, and thus are not the same as RPC 'subscriptions', which are unique
@@ -630,12 +638,12 @@ async def send(self, payload: dict) -> int:
630
638
Returns:
631
639
id: the internal ID of the request (incremented int)
632
640
"""
633
- # async with self._lock:
634
641
original_id = get_next_id ()
635
642
# self._open_subscriptions += 1
636
643
await self .max_subscriptions .acquire ()
637
644
try :
638
645
await self .ws .send (json .dumps ({** payload , ** {"id" : original_id }}))
646
+ self .last_sent = await self .loop_time ()
639
647
return original_id
640
648
except (ConnectionClosed , ssl .SSLError , EOFError ):
641
649
async with self ._lock :
@@ -2120,7 +2128,11 @@ async def _make_rpc_request(
2120
2128
2121
2129
if request_manager .is_complete :
2122
2130
break
2123
- if time .time () - self .ws .last_received >= self .retry_timeout :
2131
+ if (
2132
+ (current_time := await self .ws .loop_time ()) - self .ws .last_received
2133
+ >= self .retry_timeout
2134
+ and current_time - self .ws .last_sent >= self .retry_timeout
2135
+ ):
2124
2136
if attempt >= self .max_retries :
2125
2137
logger .warning (
2126
2138
f"Timed out waiting for RPC requests { attempt } times. Exiting."
0 commit comments