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 ¶
- func Do(ctx context.Context, cfg Config, fn func(ctx context.Context) error) error
- func DoWithLogger(ctx context.Context, cfg Config, logger jet.CLogger, operation string, ...) error
- func DoWithResult[T any](ctx context.Context, cfg Config, fn func(ctx context.Context) (T, error)) (T, error)
- func DoWithResultAndLogger[T any](ctx context.Context, cfg Config, logger jet.CLogger, operation string, ...) (T, error)
- func IsRetryable(err error) bool
- func NonRetryable(err error) error
- type Config
- type RetryableError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
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 ¶
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 ¶
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.
type RetryableError ¶
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