fiber/addon/retry
Juan Calderon-Perez 9463a8f626
v3: Add support for consistent documentation using markdownlint (#3064)
* Add support for consistent documentation using markdownlint

* Only run workflow during changes to markdown files

* Fix more inconsistencies

* Fixes to markdown under .github/

* More fixes

* Apply suggestions from code review

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Fix typo in limiter docs

* Add missing space before code-block

* Add check for dead-links

* Add write-good

* Remove legacy README files

* Fix glob for skipping .md files

* Use paths-ignore instead

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2024-07-11 15:21:56 +02:00
..
README.md v3: Add support for consistent documentation using markdownlint (#3064) 2024-07-11 15:21:56 +02:00
config.go 👷 v3 (ci): fix some linter warnings 2023-03-06 17:35:39 +03:00
exponential_backoff.go v3 (feature): add retry mechanism (#1972) 2022-08-19 08:20:14 +02:00
exponential_backoff_test.go chore: Enabling shuffling, cleanup and consistency across tests (#2931) 2024-03-24 20:54:56 +01:00

README.md

Retry Addon

Retry addon for Fiber designed to apply retry mechanism for unsuccessful network operations. This addon uses an exponential backoff algorithm with jitter. It calls the function multiple times and tries to make it successful. If all calls are failed, then, it returns an error. It adds a jitter at each retry step because adding a jitter is a way to break synchronization across the client and avoid collision.

Table of Contents

Signatures

func NewExponentialBackoff(config ...Config) *ExponentialBackoff

Examples

Firstly, import the addon from Fiber,

import (
    "github.com/gofiber/fiber/v3/addon/retry"
)

Default Config

retry.NewExponentialBackoff()

Custom Config

retry.NewExponentialBackoff(retry.Config{
    InitialInterval: 2 * time.Second,
    MaxBackoffTime:  64 * time.Second,
    Multiplier:      2.0,
    MaxRetryCount:   15,
})

Config

// Config defines the config for addon.
type Config struct {
    // InitialInterval defines the initial time interval for backoff algorithm.
    //
    // Optional. Default: 1 * time.Second
    InitialInterval time.Duration
    
    // MaxBackoffTime defines maximum time duration for backoff algorithm. When
    // the algorithm is reached this time, rest of the retries will be maximum
    // 32 seconds.
    //
    // Optional. Default: 32 * time.Second
    MaxBackoffTime time.Duration
    
    // Multiplier defines multiplier number of the backoff algorithm.
    //
    // Optional. Default: 2.0
    Multiplier float64
    
    // MaxRetryCount defines maximum retry count for the backoff algorithm.
    //
    // Optional. Default: 10
    MaxRetryCount int
    
    // currentInterval tracks the current waiting time.
    //
    // Optional. Default: 1 * time.Second
    currentInterval time.Duration
}

Default Config Example

// DefaultConfig is the default config for retry.
var DefaultConfig = Config{
    InitialInterval: 1 * time.Second,
    MaxBackoffTime:  32 * time.Second,
    Multiplier:      2.0,
    MaxRetryCount:   10,
    currentInterval: 1 * time.Second,
}