Skip to content

Retry

Defines retry and timeout utilities.

Authors

Henry Wicaksono (henry.wicaksono@gdplabs.id)

References

NONE

RetryConfig

Bases: BaseModel

Configuration for retry behavior.

Attributes:

Name Type Description
max_retries int

Maximum number of retry attempts.

base_delay float

Base delay in seconds between retries.

max_delay float

Maximum delay in seconds between retries.

exponential_base float

Base for exponential backoff.

jitter bool

Whether to add random jitter to delays.

timeout float

Overall timeout in seconds for the entire operation. When set to 0, timeout is disabled.

retry_on_exceptions tuple[type[Exception], ...]

Tuple of exception types to retry on.

validate_delay_constraints()

Validates that max_delay is greater than or equal to base_delay.

Returns:

Name Type Description
RetryConfig RetryConfig

The validated configuration.

Raises:

Type Description
ValueError

If max_delay is less than base_delay.

retry(func, *args, retry_config=None, **kwargs) async

Executes a function with retry logic and exponential backoff.

This function executes the provided function with retry logic. It will first try to execute the function once. If the function raises an exception that matches the retry_on_exceptions, it will retry up to max_retries times with exponential backoff. Therefore, the max number of attempts is max_retries + 1. If provided, the timeout applies to the entire retry operation, including all attempts and delays.

Example

If you set timeout=10s and max_retries=3, the entire retry operation (including all attempts and delays) will timeout after 10 seconds, not 10 seconds per attempt.

Parameters:

Name Type Description Default
func Callable[..., Any]

The function to execute.

required
*args Any

Positional arguments to pass to the function.

()
retry_config RetryConfig | None

Retry configuration. If None, uses default config. Defaults to None.

None
**kwargs Any

Keyword arguments to pass to the function.

{}

Returns:

Name Type Description
T T

The result of the function execution.

Raises:

Type Description
Exception

The last exception raised by the function if all retries are exhausted.

TimeoutError

If the overall timeout is exceeded.