retry

package
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package retry provides retry policies with exponential backoff and optional jitter for resilient operation execution.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	MaxAttempts  uint          `env:"MAX_ATTEMPTS"  json:"maxAttempts"`
	InitialDelay time.Duration `env:"INITIAL_DELAY" json:"initialDelay"`
	MaxDelay     time.Duration `env:"MAX_DELAY"     json:"maxDelay"`
	Multiplier   float64       `env:"MULTIPLIER"    json:"multiplier"`
	UseJitter    bool          `env:"USE_JITTER"    json:"useJitter"`
}

Config configures retry behavior.

func (*Config) EnsureDefaults

func (cfg *Config) EnsureDefaults()

EnsureDefaults sets default values for zero fields.

func (*Config) ValidateWithContext

func (cfg *Config) ValidateWithContext(ctx context.Context) error

ValidateWithContext validates the config.

type NoopPolicy

type NoopPolicy struct{}

NoopPolicy executes the operation exactly once with no retries.

func (*NoopPolicy) Execute

func (n *NoopPolicy) Execute(ctx context.Context, operation func(ctx context.Context) error) error

Execute runs the operation once.

type Policy

type Policy interface {
	Execute(ctx context.Context, operation func(ctx context.Context) error) error
}

Policy executes operations with retry logic.

func NewExponentialBackoffPolicy

func NewExponentialBackoffPolicy(cfg Config) Policy

NewExponentialBackoffPolicy returns a Policy that retries with exponential backoff.

Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/verygoodsoftwarenotvirus/platform/v2/retry"
)

func main() {
	policy := retry.NewExponentialBackoffPolicy(retry.Config{
		MaxAttempts:  3,
		InitialDelay: 10 * time.Millisecond,
		MaxDelay:     100 * time.Millisecond,
		Multiplier:   2.0,
	})

	attempts := 0
	err := policy.Execute(context.Background(), func(_ context.Context) error {
		attempts++
		if attempts < 3 {
			return fmt.Errorf("not yet")
		}
		return nil
	})

	fmt.Println(err)
	fmt.Println(attempts)
}
Output:

<nil>
3

func NewNoopPolicy

func NewNoopPolicy() Policy

NewNoopPolicy returns a Policy that never retries.

Jump to

Keyboard shortcuts

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