-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
38 lines (34 loc) · 1.32 KB
/
Copy pathhandler.py
File metadata and controls
38 lines (34 loc) · 1.32 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
import time
import functools
import logging
# Configure basic logger for utility output
logger = logging.getLogger(__name__)
def retry_network_operation(max_attempts=3, delay=1.0, backoff=2):
"""
Decorator to implement exponential backoff retry logic.
Catches generic exceptions for network-related tasks.
"""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
attempts = 0
current_delay = delay
while attempts < max_attempts:
try:
return func(*args, **kwargs)
except Exception as e:
attempts += 1
if attempts == max_attempts:
logger.error(f"Final attempt {attempts} failed: {e}")
raise
logger.warning(f"Attempt {attempts} failed, retrying in {current_delay}s...")
time.sleep(current_delay)
current_delay *= backoff
return wrapper
return decorator
@retry_network_operation(max_attempts=3, delay=2)
def fetch_data_from_source(url):
"""Example usage of the retry decorator."""
# Simulating actual network call logic here
logger.info(f"Fetching data from {url}")
return {"status": "success", "url": url}