mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-28 16:01:30 +00:00
util.NetworkRetryManager: fix potential overflow
e.g. consider: >>> 1.5 * 2 ** 2000 Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: int too large to convert to float
This commit is contained in:
parent
fdaf6e775c
commit
5d723401f8
1 changed files with 17 additions and 6 deletions
|
@ -1380,20 +1380,31 @@ class NetworkRetryManager(Generic[_NetAddrType]):
|
||||||
def _on_connection_successfully_established(self, addr: _NetAddrType) -> None:
|
def _on_connection_successfully_established(self, addr: _NetAddrType) -> None:
|
||||||
self._last_tried_addr[addr] = time.time(), 0
|
self._last_tried_addr[addr] = time.time(), 0
|
||||||
|
|
||||||
def _can_retry_addr(self, peer: _NetAddrType, *,
|
def _can_retry_addr(self, addr: _NetAddrType, *,
|
||||||
now: float = None, urgent: bool = False) -> bool:
|
now: float = None, urgent: bool = False) -> bool:
|
||||||
if now is None:
|
if now is None:
|
||||||
now = time.time()
|
now = time.time()
|
||||||
last_time, num_attempts = self._last_tried_addr.get(peer, (0, 0))
|
last_time, num_attempts = self._last_tried_addr.get(addr, (0, 0))
|
||||||
if urgent:
|
if urgent:
|
||||||
delay = min(self._max_retry_delay_urgent,
|
max_delay = self._max_retry_delay_urgent
|
||||||
self._init_retry_delay_urgent * 2 ** num_attempts)
|
init_delay = self._init_retry_delay_urgent
|
||||||
else:
|
else:
|
||||||
delay = min(self._max_retry_delay_normal,
|
max_delay = self._max_retry_delay_normal
|
||||||
self._init_retry_delay_normal * 2 ** num_attempts)
|
init_delay = self._init_retry_delay_normal
|
||||||
|
delay = self.__calc_delay(multiplier=init_delay, max_delay=max_delay, num_attempts=num_attempts)
|
||||||
next_time = last_time + delay
|
next_time = last_time + delay
|
||||||
return next_time < now
|
return next_time < now
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def __calc_delay(cls, *, multiplier: float, max_delay: float,
|
||||||
|
num_attempts: int) -> float:
|
||||||
|
num_attempts = min(num_attempts, 100_000)
|
||||||
|
try:
|
||||||
|
res = multiplier * 2 ** num_attempts
|
||||||
|
except OverflowError:
|
||||||
|
return max_delay
|
||||||
|
return max(0, min(max_delay, res))
|
||||||
|
|
||||||
def _clear_addr_retry_times(self) -> None:
|
def _clear_addr_retry_times(self) -> None:
|
||||||
self._last_tried_addr.clear()
|
self._last_tried_addr.clear()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue