Add retry time logic (exponential backoff)
This commit is contained in:
parent
0d7728665f
commit
08368bbf36
|
@ -23,6 +23,21 @@ class NioErrorResponse(Exception):
|
||||||
super().__init__(str(response))
|
super().__init__(str(response))
|
||||||
|
|
||||||
|
|
||||||
|
class RetrySleeper:
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.current_time: float = 0
|
||||||
|
self.tries: int = 0
|
||||||
|
|
||||||
|
|
||||||
|
def sleep(self, max_time: float) -> None:
|
||||||
|
self.current_time = max(
|
||||||
|
0, min((max_time / 10) * (2 ^ (self.tries - 1)), max_time)
|
||||||
|
)
|
||||||
|
print("sleeping", self.current_time)
|
||||||
|
time.sleep(self.current_time)
|
||||||
|
self.tries += 1
|
||||||
|
|
||||||
|
|
||||||
class NetworkManager:
|
class NetworkManager:
|
||||||
http_retry_codes = {408, 429, 500, 502, 503, 504, 507}
|
http_retry_codes = {408, 429, 500, 502, 503, 504, 507}
|
||||||
|
|
||||||
|
@ -102,10 +117,13 @@ class NetworkManager:
|
||||||
*args,
|
*args,
|
||||||
**kwargs) -> nr.Response:
|
**kwargs) -> nr.Response:
|
||||||
with self._lock:
|
with self._lock:
|
||||||
|
retry = RetrySleeper()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
sock = None
|
sock = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
print("Try for", nio_func.__name__)
|
||||||
sock = self._get_socket()
|
sock = self._get_socket()
|
||||||
|
|
||||||
if not self.nio.connection:
|
if not self.nio.connection:
|
||||||
|
@ -120,14 +138,14 @@ class NetworkManager:
|
||||||
logging.error("Socket error for %s: %s",
|
logging.error("Socket error for %s: %s",
|
||||||
nio_func.__name__, err.strerror)
|
nio_func.__name__, err.strerror)
|
||||||
self._close_socket(sock)
|
self._close_socket(sock)
|
||||||
time.sleep(2)
|
retry.sleep(max_time=2)
|
||||||
|
|
||||||
except RemoteTransportError as err:
|
except RemoteTransportError as err:
|
||||||
logging.error("HTTP transport error for %s: %s",
|
logging.error("HTTP transport error for %s: %s",
|
||||||
nio_func.__name__, err)
|
nio_func.__name__, err)
|
||||||
self._close_socket(sock)
|
self._close_socket(sock)
|
||||||
self.http_disconnect()
|
self.http_disconnect()
|
||||||
time.sleep(2)
|
retry.sleep(max_time=2)
|
||||||
|
|
||||||
except NioErrorResponse as err:
|
except NioErrorResponse as err:
|
||||||
logging.error("Nio response error for %s: %s",
|
logging.error("Nio response error for %s: %s",
|
||||||
|
@ -137,7 +155,7 @@ class NetworkManager:
|
||||||
if err.response.status_code in self.http_retry_codes:
|
if err.response.status_code in self.http_retry_codes:
|
||||||
return response
|
return response
|
||||||
|
|
||||||
time.sleep(2)
|
retry.sleep(max_time=10)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return response
|
return response
|
||||||
|
|
Loading…
Reference in New Issue
Block a user