retry_handler
¶
Database retry handler with exponential backoff and transient error detection.
This module provides sophisticated retry mechanisms for database operations with intelligent error classification and adaptive delay strategies.
RetryMetadata
¶
Bases: TypedDict
Type definition for retry operation metadata.
Defines the structure of metadata that can be stored in RetryOperationContext for tracking and debugging.
RetryPolicy
dataclass
¶
RetryPolicy(
max_retries=3,
base_delay_seconds=1.0,
max_delay_seconds=60.0,
exponential_base=2.0,
jitter_range=0.1,
operation_timeout_seconds=300.0,
)
Configuration for retry behavior with exponential backoff.
Defines retry parameters including maximum attempts, delay settings, and jitter for avoiding thundering herd problems.
RetryOperationContext
dataclass
¶
RetryOperationContext(
operation_id,
policy,
start_time=(lambda: now(UTC))(),
attempt_count=0,
last_error=None,
metadata=_create_empty_metadata(),
)
Context information for retry operations.
Tracks operation progress, attempt history, and metadata for comprehensive retry operation management.
DatabaseRetryHandler
¶
Advanced retry handler for database operations with intelligent error detection.
Provides exponential backoff with jitter, transient error classification, and comprehensive retry context management for reliable database operations.
Initialize retry handler with logging and default policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger
|
Logger instance for retry operation tracking |
required |
default_policy
|
RetryPolicy | None
|
Default retry policy for operations |
None
|
Source code in src/core/retry_handler.py
is_transient_error
¶
Determine if an error is transient and worth retrying.
Analyzes error type, message content, and errno codes to classify errors as transient (temporary) or permanent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
Exception
|
Exception to analyze |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if error appears transient and operation should be retried |
Source code in src/core/retry_handler.py
calculate_delay_seconds
staticmethod
¶
Calculate delay for retry attempt with exponential backoff and jitter.
Implements exponential backoff with configurable jitter to prevent thundering herd problems and distribute retry attempts over time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attempt_number
|
int
|
Current attempt number (0-based) |
required |
policy
|
RetryPolicy
|
Retry policy configuration |
required |
Returns:
| Type | Description |
|---|---|
float
|
Delay in seconds before next retry attempt |
Source code in src/core/retry_handler.py
async_retry_operation
async
¶
Async context manager for retry operations with comprehensive tracking.
Provides retry context with operation tracking, error handling, and automatic retry logic for database operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation_id
|
str
|
Unique identifier for the operation |
required |
policy
|
RetryPolicy | None
|
Retry policy to use (defaults to database_policy) |
None
|
Yields:
| Name | Type | Description |
|---|---|---|
RetryOperationContext |
AsyncGenerator[RetryOperationContext]
|
Context for tracking retry progress |
Example
async with retry_handler.async_retry_operation("db_read") as ctx: ctx.metadata["table"] = "tracks" result = await database_operation()
Source code in src/core/retry_handler.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | |
execute_with_retry
async
¶
Execute operation with retry logic.
Implements retry loop directly for reliable async operation retry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
Callable[[], Awaitable[RetryResult]]
|
Async callable to execute with retry |
required |
operation_id
|
str
|
Unique identifier for the operation |
required |
policy
|
RetryPolicy | None
|
Retry policy to use |
None
|
Returns:
| Type | Description |
|---|---|
RetryResult
|
Result from successful operation execution |
Raises:
| Type | Description |
|---|---|
(OSError, ValueError, RuntimeError)
|
If all retries exhausted |
Source code in src/core/retry_handler.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |