retry

package
v0.166.2 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

Package retry

The retry package provides tooling to do retries easily in your application code.

retry strategies

Using a retry strategy easy as iterating with a for loop, but instead of making a condition based on a max value, we check it with retry.Strategy#ShouldTry.

Example:

package mypkg

import (
	"context"
	"fmt"
	"github.com/adamluzsi/frameless/pkg/retry"
)

func (ms MyStruct) MyFunc(ctx context.Context) error {
	var rs retry.ExponentialBackoff

	for i := 0; rs.ShouldTry(ctx, i); i++ {
		err := ms.DoAction(ctx)
		if err != nil {
			if ms.isErrTemporary(err) {
				continue
			}
			return err
		}
		return nil
	}
	return fmt.Errorf("failed to DoAction")
}

The package contains strategies for retrying operations.

ExponentialBackoff

This strategy will retry an operation up to a specified maximum number of times, with an increasing delay between each retry. The delay doubles with each failed attempt. This gives the system more time to recover from any issues.

Jitter

This strategy will also retry an operation up to a specified maximum number of times, but adds a random variation to the backoff time. This helps distribute retry attempts evenly over time and reduces the risk of overwhelming the system.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExponentialBackoff

type ExponentialBackoff struct {
	// MaxRetries is the amount of retry which is allowed before giving up the application.
	//
	// Default: 5
	MaxRetries int
	// BackoffDuration is the time duration which will be used to calculate the exponential backoff wait time.
	//
	// Default: 1/2 time.Second
	BackoffDuration time.Duration
}

ExponentialBackoff will answer if retry can be made. It waits as well the amount of time based on the failure count. The waiting time before returning is doubled for each failed attempts This ensures that the system gets progressively more time to recover from any issues.

Example
package main

import (
	"context"
	"github.com/adamluzsi/frameless/pkg/retry"
)

func main() {
	ctx := context.Background()
	rs := retry.ExponentialBackoff{}

	for i := 0; rs.ShouldTry(ctx, i); i++ {
		// do an action
		// return on success
	}
	// return failure
}
Output:

func (ExponentialBackoff) ShouldTry

func (rs ExponentialBackoff) ShouldTry(ctx context.Context, failureCount int) bool

type Jitter

type Jitter struct {
	// MaxRetries is the amount of retry which is allowed before giving up the application.
	//
	// Default: 5
	MaxRetries int
	// MaxWaitDuration is the time duration which will be used to calculate the exponential backoff wait time.
	//
	// Default: 5 * time.Second
	MaxWaitDuration time.Duration
}

Jitter is a random variation added to the backoff time. This helps to distribute the retry attempts evenly over time, reducing the risk of overwhelming the system and avoiding synchronization between multiple clients that might be retrying simultaneously.

Example
package main

import (
	"context"
	"github.com/adamluzsi/frameless/pkg/retry"
)

func main() {
	ctx := context.Background()
	rs := retry.Jitter{}

	for i := 0; rs.ShouldTry(ctx, i); i++ {
		// do an action
		// return on success
	}
	// return failure
}
Output:

func (Jitter) ShouldTry

func (rs Jitter) ShouldTry(ctx context.Context, failureCount int) bool

type Strategy

type Strategy interface {
	// ShouldTry will tell if retry should be attempted after a given number of failed attempts.
	ShouldTry(ctx context.Context, failureCount int) bool
}

Jump to

Keyboard shortcuts

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