retry
Last updated
Last updated
The Retry Package provides a robust and extensible interface for automatically retrying operations in Go. It supports configurable retry strategies like fixed delays, jitter, and exponential backoff, ensuring reliability in API calls, database queries, and distributed systems.
Customizable Backoff Strategies – Supports Fixed, Jitter, and Exponential backoff
Context-Aware – Automatically stops retries when the context is canceled
Configurable Retry Conditions – Choose which errors should trigger retries
ExecuteWithRetry
: Executes a function with automatic retry logic.
Params:
ctx
: Context for request tracing and cancellation
fn
: The function to retry (must return an error if it fails)
retryOn
: Custom function to determine retry conditions
Returns:
error
: The final result after retries.
1. Fixed Backoff
Constant delay between retries
Simple and predictable retry behavior
2. Jitter Backoff
Adds randomness to prevent synchronized retries (thundering herd problem)
3. Exponential Backoff
Delays grow exponentially (BaseDelay
* Factor
^attempt
)
Prevents excessive load on failing services
You can find a complete working example in the repository under .