retry

Contributions Welcome Release

πŸ”„ Retry Package

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.

Features

  • 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

Installation

go get github.com/kittipat1413/go-common/framework/retry

Documentation

Go Reference

For detailed API documentation, examples, and usage patterns, visit the Go Package Documentation.

Usage

🧩 Interface

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.

Example: Basic Retry

You can find a complete working example in the repository under framework/retry/example.

Backoff Strategies

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

Last updated