retryx

package
v0.0.153 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	DefaultInterval       = 500 * time.Millisecond
	DefaultMaxInterval    = 2 * time.Second
	DefaultMaxElapsedTime = 5 * time.Second
	DefaultMaxRetries     = 3
)

Variables

This section is empty.

Functions

func ConstantRetry

func ConstantRetry(fn func() error, opts ...RetryOption) error

ConstantRetry executes the provided function `fn` with a constant retry interval. This function is designed for simple retry scenarios where the interval between retries and the maximum number of retries are the only customizable options.

Parameters: - fn: The function to be executed and retried upon failure. - opts: Optional retry configurations, such as initial interval and maximum retries.

The retry interval defaults to `DefaultInterval` unless overridden by the `WithInterval` option. If more advanced control over the retry behavior is required, consider using the `backoff` package directly.

Example
package main

import (
	"time"

	"github.com/clinia/x/retryx"
)

func main() {
	// Define a function to be retried.
	fn := func() error {
		// Perform some operation that may fail.
		return nil
	}

	// Retry the function with default options.
	err := retryx.ConstantRetry(fn)
	if err != nil {
		// Handle the error.
	}

	// Retry the function with custom options.
	err = retryx.ConstantRetry(fn, retryx.WithRetryCount(5), retryx.WithInterval(1*time.Second))
	if err != nil {
		// Handle the error.
	}
}

func ExponentialRetry

func ExponentialRetry(fn func() error, opts ...RetryOption) error

ExponentialRetry executes the provided function `fn` with an exponential backoff retry strategy. This function is suitable for scenarios where the retry interval increases exponentially with each attempt, up to a maximum interval and elapsed time.

Parameters: - fn: The function to be executed and retried upon failure. - opts: Optional retry configurations, such as initial interval, maximum retries, and other settings.

The retry interval starts at `DefaultInterval` unless overridden by the `WithInterval` option. The maximum interval between retries starts at `DefaultMaxInterval` unless overridden by the `WithMaxInterval` option. The maximum elapsed time defaults to `DefaultMaxElapsedTime` unless overridden by the `WithMaxElapsedTime`option. If more advanced control over the retry behavior is required, consider using the `backoff` package directly.

Example
package main

import (
	"time"

	"github.com/clinia/x/retryx"
)

func main() {
	// Define a function to be retried.
	fn := func() error {
		// Perform some operation that may fail.
		return nil
	}

	// Retry the function with default options.
	err := retryx.ExponentialRetry(fn)
	if err != nil {
		// Handle the error.
	}

	// Retry the function with custom options.
	err = retryx.ExponentialRetry(fn,
		retryx.WithMaxElapsedTime(10*time.Second),
		retryx.WithMaxInterval(2*time.Second),
		retryx.WithInterval(50*time.Millisecond),
		retryx.WithRetryCount(5),
	)
	if err != nil {
		// Handle the error.
	}
}

Types

type RetryOption

type RetryOption func(*retryOptions)

func WithInterval

func WithInterval(interval time.Duration) RetryOption

WithInterval sets the initial interval between retries. Default interval is 500ms. When used with ExponentialRetry, this sets the initial interval. When used with ConstantRetry, this sets the constant interval.

func WithMaxElapsedTime

func WithMaxElapsedTime(maxElapsedTime time.Duration) RetryOption

WithMaxElapsedTime sets the maximum elapsed time for retries. Default max elapsed time is 5s. When used with ExponentialRetry, this sets the maximum elapsed time. When used with ConstantRetry, this option is ignored.

func WithMaxInterval

func WithMaxInterval(maxInterval time.Duration) RetryOption

WithMaxInterval sets the maximum interval between retries. Default max interval is 2s. When used with ExponentialRetry, this sets the maximum interval. When used with ConstantRetry, this option is ignored.

func WithRetryCount

func WithRetryCount(count int) RetryOption

WithRetryCount sets the maximum number of retries. Default retry count is 3. A retry count of <= 0 will not be taken into account (default will be used).

Note: a retry count of 1 will result in 2 calls (the first one and 1 retry).

Jump to

Keyboard shortcuts

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