-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry_examples.py
224 lines (177 loc) · 6.9 KB
/
retry_examples.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""Examples demonstrating the use of RetryHandler for resilient operations.
This module provides real-world examples of how to use the RetryHandler
to make your code more resilient against transient failures.
"""
import logging
import random
import time
from typing import Any
from safeguards.core.resilience import RetryableException, RetryHandler, RetryStrategy
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
)
logger = logging.getLogger(__name__)
# Example 1: Simple API call with retry
@RetryHandler(
max_attempts=3,
strategy=RetryStrategy.EXPONENTIAL,
base_delay=1.0,
jitter=0.2,
)
def fetch_user_data(user_id: str) -> dict[str, Any]:
"""Fetch user data from API with retry behavior.
Args:
user_id: User ID to fetch data for
Returns:
User data dictionary
Raises:
MaxRetriesExceeded: If maximum retry attempts are exceeded
"""
url = f"https://api.example.com/users/{user_id}"
# In a real application, this would be an actual API call
# For demonstration, we'll simulate random failures
if random.random() < 0.5: # 50% chance of failure
logger.warning(f"Simulating network error for {url}")
msg = "Connection refused"
raise ConnectionError(msg)
# Simulate successful response
logger.info(f"Successfully fetched data from {url}")
return {"id": user_id, "name": "Test User", "email": "[email protected]"}
# Example 2: Database operation with custom retry behavior
class DatabaseClient:
"""Example database client with retry behavior."""
def __init__(self, connection_string: str):
"""Initialize database client.
Args:
connection_string: Database connection string
"""
self.connection_string = connection_string
self.connected = False
def connect(self) -> bool:
"""Connect to database with retry behavior.
Returns:
True if connection successful
"""
# Custom retry handler for connection attempts
retry_handler = RetryHandler(
max_attempts=5,
strategy=RetryStrategy.LINEAR, # Linear backoff for DB connections
base_delay=2.0,
retryable_exceptions=[ConnectionError, TimeoutError],
)
try:
# Use as context manager
with retry_handler:
logger.info(f"Connecting to database: {self.connection_string}")
# Simulate connection attempt that might fail
if random.random() < 0.3: # 30% chance of failure
logger.warning("Simulating database connection error")
msg = "Database connection failed"
raise ConnectionError(msg)
self.connected = True
logger.info("Database connection established")
return True
except Exception as e:
logger.error(
f"Failed to connect to database after {retry_handler.attempt} attempts: {e!s}",
)
self.connected = False
return False
@RetryHandler(max_attempts=3, strategy=RetryStrategy.EXPONENTIAL)
def query(self, sql: str) -> list:
"""Execute SQL query with retry behavior.
Args:
sql: SQL query to execute
Returns:
Query results
Raises:
ValueError: If not connected to database
MaxRetriesExceeded: If maximum retry attempts are exceeded
"""
if not self.connected:
msg = "Not connected to database"
raise ValueError(msg)
logger.info(f"Executing query: {sql}")
# Simulate random database timeout
if random.random() < 0.4: # 40% chance of timeout
logger.warning("Simulating database timeout")
msg = "Query timeout"
raise TimeoutError(msg)
# Simulate successful query
logger.info("Query executed successfully")
return [{"id": 1, "name": "Test"}]
# Example 3: Custom retry for specific exceptions
class PaymentProcessor:
"""Example payment processor with specialized retry behavior."""
class PaymentGatewayError(RetryableException):
"""Payment gateway error that should be retried."""
pass
class PaymentDeclinedError(Exception):
"""Payment declined error that should not be retried."""
pass
@RetryHandler(
max_attempts=4,
strategy=RetryStrategy.EXPONENTIAL,
base_delay=2.0,
jitter=0.1,
retryable_exceptions=["PaymentProcessor.PaymentGatewayError", ConnectionError],
)
def process_payment(self, amount: float, card_token: str) -> dict[str, Any]:
"""Process payment with specialized retry for gateway errors.
Args:
amount: Payment amount
card_token: Card token for payment
Returns:
Payment receipt
Raises:
PaymentDeclinedError: If payment is declined (won't retry)
MaxRetriesExceeded: If gateway errors persist beyond retries
"""
logger.info(f"Processing payment of ${amount} with token {card_token}")
# Simulate various payment scenarios
rand_val = random.random()
if rand_val < 0.3: # 30% gateway error (will retry)
logger.warning("Simulating payment gateway error")
msg = "Gateway unavailable"
raise self.PaymentGatewayError(msg)
if rand_val < 0.5: # 20% declined (won't retry)
logger.warning("Simulating payment declined")
msg = "Insufficient funds"
raise self.PaymentDeclinedError(msg)
# Successful payment
logger.info("Payment processed successfully")
return {
"transaction_id": f"txn_{random.randint(10000, 99999)}",
"amount": amount,
"status": "completed",
"timestamp": time.time(),
}
def main():
"""Run the examples."""
# Example 1: API call
try:
user_data = fetch_user_data("12345")
print(f"User data: {user_data}")
except Exception as e:
print(f"Failed to fetch user data: {e!s}")
# Example 2: Database operations
db = DatabaseClient("postgres://example:password@localhost:5432/mydb")
if db.connect():
try:
results = db.query("SELECT * FROM users LIMIT 10")
print(f"Query results: {results}")
except Exception as e:
print(f"Query failed: {e!s}")
# Example 3: Payment processing
payment_processor = PaymentProcessor()
try:
receipt = payment_processor.process_payment(99.99, "card_token_12345")
print(f"Payment receipt: {receipt}")
except PaymentProcessor.PaymentDeclinedError as e:
print(f"Payment declined: {e!s}")
except Exception as e:
print(f"Payment processing error: {e!s}")
if __name__ == "__main__":
main()