retry

package module
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 10, 2025 License: MIT Imports: 2 Imported by: 0

README

Retry

Go Reference

Very simple Go package to retry a function using exponential backoff algorithm.

Installation

go get github.com/stonear/retry

Usage

import "github.com/stonear/retry"

// Define your function
func yourFunction() error {
    // Your code here
    return nil
}

// Call the Retry function
err := retry.Retry(yourFunction, time.Second, 5)
if err != nil {
    // Handle the error
}

Contributing

Contributions are welcome. Please open a pull request with your changes.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Retry

func Retry(f func() error, expRate time.Duration, maxRetries int) error

Retry retries the given function until no error is returned or max retries are reached. f is the function to be retried; it must return an error on failure or nil on success. expRate defines the exponential backoff base duration between retry attempts. maxRetries specifies the maximum number of retry attempts allowed.

The function implements exponential backoff algorithm for retry delays: t = b^c where:

  • t is the time delay between retry attempts (in seconds)
  • b is the base duration (expRate in seconds)
  • c is the retry attempt number (0-based)

Source: https://en.wikipedia.org/wiki/Exponential_backoff

For example, with expRate=2s and maxRetries=4, the delays will be:

  • 1st retry: 2^0 = 1s
  • 2nd retry: 2^1 = 2s
  • 3rd retry: 2^2 = 4s
  • 4th retry: 2^3 = 8s

Returns the last encountered error if all retries fail, or nil if the function eventually succeeds.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL