Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
42 lines (36 loc) · 1.41 KB
/
Copy pathutils.py
File metadata and controls
42 lines (36 loc) · 1.41 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
36
37
38
39
40
41
42
import time
import functools
import random
def retry_network(max_retries=3, base_delay=1.0, exceptions=(ConnectionError, TimeoutError)):
"""Decorator implementing exponential backoff for network operations."""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
attempts = 0
while attempts < max_retries:
try:
return func(*args, **kwargs)
except exceptions as e:
attempts += 1
if attempts >= max_retries:
raise e
# Jittered exponential backoff
delay = (base_delay * (2 ** (attempts - 1))) + (random.uniform(0, 1))
time.sleep(delay)
return func(*args, **kwargs)
return wrapper
return decorator
class NetworkSession:
"""Context manager for simulating network session lifecycle."""
def __init__(self, target):
self.target = target
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
pass
@retry_network(max_retries=5, base_delay=0.5)
def request(self, endpoint):
# Simulate intermittent network instability
if random.random() < 0.7:
raise ConnectionError(f"Failed to reach {self.target}/{endpoint}")
return {"status": 200, "data": "success"}