|
| 1 | +--- |
| 2 | +id: retry |
| 3 | +--- |
| 4 | + |
| 5 | +# Retry Addon |
| 6 | + |
| 7 | +Retry addon for [Fiber](https://github.com/gofiber/fiber) designed to apply retry mechanism for unsuccessful network |
| 8 | +operations. This addon uses an exponential backoff algorithm with jitter. It calls the function multiple times and tries |
| 9 | +to make it successful. If all calls are failed, then, it returns an error. It adds a jitter at each retry step because adding |
| 10 | +a jitter is a way to break synchronization across the client and avoid collision. |
| 11 | + |
| 12 | +## Table of Contents |
| 13 | + |
| 14 | +- [Retry Addon](#retry-addon) |
| 15 | +- [Table of Contents](#table-of-contents) |
| 16 | +- [Signatures](#signatures) |
| 17 | +- [Examples](#examples) |
| 18 | +- [Default Config](#default-config) |
| 19 | +- [Custom Config](#custom-config) |
| 20 | +- [Config](#config) |
| 21 | +- [Default Config Example](#default-config-example) |
| 22 | + |
| 23 | +## Signatures |
| 24 | + |
| 25 | +```go |
| 26 | +func NewExponentialBackoff(config ...retry.Config) *retry.ExponentialBackoff |
| 27 | +``` |
| 28 | + |
| 29 | +## Examples |
| 30 | + |
| 31 | +```go |
| 32 | +package main |
| 33 | + |
| 34 | +import ( |
| 35 | + "fmt" |
| 36 | + |
| 37 | + "github.com/gofiber/fiber/v3/addon/retry" |
| 38 | + "github.com/gofiber/fiber/v3/client" |
| 39 | +) |
| 40 | + |
| 41 | +func main() { |
| 42 | + expBackoff := retry.NewExponentialBackoff(retry.Config{}) |
| 43 | + |
| 44 | + // Local variables that will be used inside of Retry |
| 45 | + var resp *client.Response |
| 46 | + var err error |
| 47 | + |
| 48 | + // Retry a network request and return an error to signify to try again |
| 49 | + err = expBackoff.Retry(func() error { |
| 50 | + client := client.New() |
| 51 | + resp, err = client.Get("https://gofiber.io") |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("GET gofiber.io failed: %w", err) |
| 54 | + } |
| 55 | + if resp.StatusCode() != 200 { |
| 56 | + return fmt.Errorf("GET gofiber.io did not return OK 200") |
| 57 | + } |
| 58 | + return nil |
| 59 | + }) |
| 60 | + |
| 61 | + // If all retries failed, panic |
| 62 | + if err != nil { |
| 63 | + panic(err) |
| 64 | + } |
| 65 | + fmt.Printf("GET gofiber.io succeeded with status code %d\n", resp.StatusCode()) |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +## Default Config |
| 70 | + |
| 71 | +```go |
| 72 | +retry.NewExponentialBackoff() |
| 73 | +``` |
| 74 | + |
| 75 | +## Custom Config |
| 76 | + |
| 77 | +```go |
| 78 | +retry.NewExponentialBackoff(retry.Config{ |
| 79 | + InitialInterval: 2 * time.Second, |
| 80 | + MaxBackoffTime: 64 * time.Second, |
| 81 | + Multiplier: 2.0, |
| 82 | + MaxRetryCount: 15, |
| 83 | +}) |
| 84 | +``` |
| 85 | + |
| 86 | +## Config |
| 87 | + |
| 88 | +```go |
| 89 | +// Config defines the config for addon. |
| 90 | +type Config struct { |
| 91 | + // InitialInterval defines the initial time interval for backoff algorithm. |
| 92 | + // |
| 93 | + // Optional. Default: 1 * time.Second |
| 94 | + InitialInterval time.Duration |
| 95 | + |
| 96 | + // MaxBackoffTime defines maximum time duration for backoff algorithm. When |
| 97 | + // the algorithm is reached this time, rest of the retries will be maximum |
| 98 | + // 32 seconds. |
| 99 | + // |
| 100 | + // Optional. Default: 32 * time.Second |
| 101 | + MaxBackoffTime time.Duration |
| 102 | + |
| 103 | + // Multiplier defines multiplier number of the backoff algorithm. |
| 104 | + // |
| 105 | + // Optional. Default: 2.0 |
| 106 | + Multiplier float64 |
| 107 | + |
| 108 | + // MaxRetryCount defines maximum retry count for the backoff algorithm. |
| 109 | + // |
| 110 | + // Optional. Default: 10 |
| 111 | + MaxRetryCount int |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +## Default Config Example |
| 116 | + |
| 117 | +```go |
| 118 | +// DefaultConfig is the default config for retry. |
| 119 | +var DefaultConfig = Config{ |
| 120 | + InitialInterval: 1 * time.Second, |
| 121 | + MaxBackoffTime: 32 * time.Second, |
| 122 | + Multiplier: 2.0, |
| 123 | + MaxRetryCount: 10, |
| 124 | + currentInterval: 1 * time.Second, |
| 125 | +} |
| 126 | +``` |
0 commit comments