retry

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package retry runs functions with bounded retries using exponential backoff with jitter.

By default every error is retried; wrap an error with NonRetryable to stop immediately. Use DefaultConfig or RPCConfig for ready-made settings, or the DoWithLogger variants to log each attempt.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(ctx context.Context, cfg Config, fn func(ctx context.Context) error) error

Do runs the function with retry logic.

Example
package main

import (
	"context"
	"errors"
	"fmt"
	"time"

	"github.com/zloevil/jet/retry"
)

func main() {
	attempts := 0
	err := retry.Do(context.Background(),
		retry.Config{MaxAttempts: 3, InitialDelay: time.Millisecond, Multiplier: 1},
		func(ctx context.Context) error {
			attempts++
			if attempts < 3 {
				return errors.New("temporary failure")
			}
			return nil
		},
	)

	fmt.Println(attempts, err)
}
Output:
3 <nil>

func DoWithLogger

func DoWithLogger(ctx context.Context, cfg Config, logger jet.CLogger, operation string, fn func(ctx context.Context) error) error

DoWithLogger runs the function with retry logic and logging.

func DoWithResult

func DoWithResult[T any](ctx context.Context, cfg Config, fn func(ctx context.Context) (T, error)) (T, error)

DoWithResult runs the function with retry logic and returns a result.

func DoWithResultAndLogger

func DoWithResultAndLogger[T any](ctx context.Context, cfg Config, logger jet.CLogger, operation string, fn func(ctx context.Context) (T, error)) (T, error)

DoWithResultAndLogger runs the function with retry logic and logging, and returns a result.

func IsRetryable

func IsRetryable(err error) bool

IsRetryable reports whether the operation may be retried for the given error. By default all errors are considered retryable, except those explicitly marked.

func NonRetryable

func NonRetryable(err error) error

NonRetryable wraps an error, marking it as non-retryable.

Example
package main

import (
	"context"
	"errors"
	"fmt"

	"github.com/zloevil/jet/retry"
)

func main() {
	attempts := 0
	err := retry.Do(context.Background(), retry.DefaultConfig(), func(ctx context.Context) error {
		attempts++
		return retry.NonRetryable(errors.New("fatal"))
	})

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

Types

type Config

type Config struct {
	// MaxAttempts is the maximum number of attempts (including the first one).
	MaxAttempts int
	// InitialDelay is the initial delay before the first retry.
	InitialDelay time.Duration
	// MaxDelay is the maximum delay between attempts.
	MaxDelay time.Duration
	// Multiplier is the factor for exponential delay growth.
	Multiplier float64
	// Jitter adds randomness to the delay (0.0 - 1.0).
	Jitter float64
}

Config holds the settings for the retry mechanism.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default configuration. Suitable for most operations with moderate delays.

func RPCConfig

func RPCConfig() Config

RPCConfig returns a configuration optimized for RPC calls. More aggressive retries with smaller delays for fast recovery.

type RetryableError

type RetryableError struct {
	Err       error
	Retryable bool
}

RetryableError represents an error and whether it may be retried.

func (*RetryableError) Error

func (e *RetryableError) Error() string

func (*RetryableError) Unwrap

func (e *RetryableError) Unwrap() error

Jump to

Keyboard shortcuts

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