-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
35 lines (31 loc) · 1.1 KB
/
Copy pathutils.py
File metadata and controls
35 lines (31 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import time
import functools
import random
def retry_network_call(max_retries=3, base_delay=1.0, jitter=True):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
attempt = 0
while attempt < max_retries:
try:
return func(*args, **kwargs)
except (ConnectionError, TimeoutError) as e:
attempt += 1
if attempt == max_retries:
raise e
sleep_time = base_delay * (2 ** (attempt - 1))
if jitter:
sleep_time *= (0.5 + random.random())
time.sleep(sleep_time)
return None
return wrapper
return decorator
class NetworkHandler:
@staticmethod
@retry_network_call(max_retries=5)
def sync_game_state(payload):
print(f"Syncing: {payload}")
# Simulation of unstable network
if random.random() < 0.7:
raise ConnectionError("Server lag detected")
return True