retry

package
v0.1.32 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RetryStrategyCtxKey = "retry_strategy"

	// TODO_IMPROVE: Make these configurable via flags, configs or env vars.
	DefaultMaxRetryCount  = 25
	DefaultInitialDelayMs = 500
	DefaultMaxDelayMs     = 30000
)

Variables

View Source
var (
	// Prepare the default exponential backoff strategy
	DefaultExponentialDelay = WithExponentialBackoffFn(DefaultMaxDelayMs, DefaultInitialDelayMs, DefaultMaxDelayMs)
)
View Source
var (

	// ErrNonRetryable signals the retry strategy to stop retrying due to a non-retryable error.
	ErrNonRetryable = sdkerrors.Register(codespace, 1, "non-retryable error")
)

Functions

func Call added in v0.1.2

func Call[T any](
	ctx context.Context,
	work func() (T, error),
	retryStrategy RetryStrategyFunc,
	logger polylog.Logger,
) (T, error)

Call executes a function repeatedly according to the retry strategy until either: - It succeeds - It returns an `ErrNonRetryable` (indicating no more retries should be attempted)

If no retry strategy is provided, it defaults to an exponential backoff strategy defined at the top of this file.

Returns: - The result from the work function - Any error that occurred if retries are exhausted

func OnError

func OnError(
	ctx context.Context,
	retryLimit int,
	retryDelay time.Duration,
	retryResetTimeout time.Duration,
	workName string,
	workFn RetryFunc,
) error

OnError continuously invokes the provided work function (workFn) until either the context (ctx) is canceled or the error channel returned by workFn is closed. If workFn encounters an error, OnError will retry invoking workFn based on the provided retry parameters.

Parameters:

  • ctx: the context to monitor for cancellation. If canceled, OnError will exit without error.
  • retryLimit: the maximum number of retries for workFn upon encountering an error.
  • retryDelay: the duration to wait before retrying workFn after an error.
  • retryResetCount: Specifies the duration of continuous error-free operation required before the retry count is reset. If the work function operates without errors for this duration, any subsequent error will restart the retry count from the beginning.
  • workName: a name or descriptor for the work function, used for logging purposes.
  • workFn: a function that performs some work and returns an error channel. This channel emits errors encountered during the work.

Returns: - If the context is canceled, the function returns nil. - If the error channel is closed, a warning is logged, and the function returns nil. - If the retry limit is reached, the function returns the error from the channel.

Note: After each error, a delay specified by retryDelay is introduced before retrying workFn.func OnError(

Types

type RetryFunc

type RetryFunc func() chan error

type RetryStrategyFunc added in v0.1.2

type RetryStrategyFunc func(context.Context, int) bool

func GetStrategy added in v0.1.2

func GetStrategy(ctx context.Context) RetryStrategyFunc

GetStrategy retrieves the retry strategy from the context. - Returns the default exponential delay strategy if no strategy is found - Useful for setting a custom retry strategy in the context and retrieving it later

func WithExponentialBackoffFn added in v0.1.2

func WithExponentialBackoffFn(
	maxRetryCount int,
	initialDelayMs int,
	maxDelayMs int,
) RetryStrategyFunc

WithExponentialBackoffFn creates a retry strategy with exponential backoff.

This function returns a RetryStrategyFunc that implements exponential backoff behavior: - Retries stop after reaching maxRetryCount - Initial delay starts at initialDelayMs - Delay doubles with each retry attempt (2^retryCount) - Delay is capped at maxDelayMs - Respects context cancellation

Parameters:

  • maxRetryCount: Maximum number of retry attempts allowed
  • initialDelayMs: Starting delay in milliseconds
  • maxDelayMs: Upper limit for delay in milliseconds

Returns a RetryStrategyFunc that can be used with retry mechanisms.

Jump to

Keyboard shortcuts

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