retry

package
v4.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 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 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/v4/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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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