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.
type NoopPolicy ¶
type NoopPolicy struct{}
NoopPolicy executes the operation exactly once with no retries.
type Policy ¶
type Policy interface {
Execute(ctx context.Context, operation func(ctx context.Context) error) error
}
Policy executes operations with retry logic.
func NewExponentialBackoffPolicy ¶
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
Click to show internal directories.
Click to hide internal directories.