libRetry

package
v0.28.1 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package libRetry provides retry policies with exponential backoff for function execution.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeriveStatusCode added in v0.28.0

func DeriveStatusCode[T any](resp *T, err error) int

DeriveStatusCode returns the appropriate HTTP status code for a completed call. Returns http.StatusInternalServerError (500) if err != nil or resp is nil. Otherwise returns ExtractStatusCode(resp) if non-zero, or http.StatusOK (200) if the response's status is zero.

func ExtractStatusCode added in v0.28.0

func ExtractStatusCode[T any](resp *T) int

ExtractStatusCode returns the status code from a response pointer whose concrete type implements StatusProvider. Returns 0 if resp is nil or the type does not implement StatusProvider.

The type assertion is performed on the pointer (not the dereferenced value) so that both pointer-receiver and value-receiver GetStatus methods are recognized.

func FormatAttemptTitle

func FormatAttemptTitle(base string, attempt int) string

FormatAttemptTitle returns a log key suffix for the given attempt number. Attempt 1 has no suffix; attempts 2+ get "-retry-N".

Types

type AttemptFunc

type AttemptFunc[Resp any] func(attempt int) (*Resp, int, error)

AttemptFunc is the function executed for each retry attempt. The attempt number (1-based) is passed as the title argument. It returns the response, the HTTP status code observed, and an error.

type ErrorCodeProvider

type ErrorCodeProvider interface {
	GetErrorCode() int
}

ErrorCodeProvider is an optional interface that response types can implement to expose an application-level error code for retry eligibility checks.

type ErrorKeyProvider

type ErrorKeyProvider interface {
	GetErrorKey() string
}

ErrorKeyProvider is an optional interface that response types can implement to expose an application-level error key (string) for retry eligibility checks. This complements ErrorCodeProvider (int-based) for APIs that use string error keys (e.g. Galaxy's ExceptionDetail.Key field).

It is evaluated only in shouldRetryResponse, which runs for nil-error attempts — i.e., responses successfully decoded from HTTP 2xx. HTTP failure retries (non-2xx → RemoteCallError) remain governed by shouldRetryError's timeout/status predicates and do NOT consult ErrorKeyProvider.

type RetryPolicy

type RetryPolicy struct {
	// MaxRetries is the maximum number of retry attempts (0 = no retries,
	// total attempts = MaxRetries + 1).
	MaxRetries int

	// RetryOnTimeout enables retrying when a timeout error is detected.
	RetryOnTimeout bool

	// RetryOnStatus is a set of HTTP status codes that should trigger a retry.
	RetryOnStatus map[int]bool

	// RetryOnErrorCodes is a set of application-level error codes (from
	// ErrorCodeProvider) that should trigger a retry.
	RetryOnErrorCodes map[int]bool

	// RetryOnErrorKeys is a set of application-level error keys (strings from
	// ErrorKeyProvider) that should trigger a retry. This complements
	// RetryOnErrorCodes (int-based) for APIs that use string error keys
	// (e.g. "SERVICE_UNAVAILABLE", "RATE_LIMITED").
	RetryOnErrorKeys map[string]bool

	// Backoff is the duration to wait between retry attempts.
	// If zero, no delay is applied between attempts.
	Backoff time.Duration

	// IsTimeoutError is an optional predicate to determine if an error is a
	// timeout error. If nil, the default predicate is used, which recognizes
	// API_CONNECT_TIMED_OUT and API_CALL_TIME_OUT error descriptions.
	IsTimeoutError func(err error) bool

	// Context for cancellation. If nil, context.Background() is used.
	Context context.Context

	// Sleep is an optional function used for backoff delays. If nil,
	// a timer-based implementation is used that exits early on context
	// cancellation. Useful for deterministic testing.
	Sleep func(ctx context.Context, d time.Duration) bool
}

RetryPolicy configures the retry behavior for a sequence of attempts.

type RetryResult

type RetryResult[Resp any] struct {
	Response   *Resp
	Error      error
	Elapsed    time.Duration
	Attempts   int
	LastStatus int
}

RetryResult holds the outcome of a retry sequence.

func WithRetry

func WithRetry[Resp any](policy *RetryPolicy, attempt AttemptFunc[Resp]) RetryResult[Resp]

WithRetry executes the given attempt function up to MaxRetries + 1 times, retrying based on the policy configuration. It returns the final result with aggregate metadata.

type StatusProvider

type StatusProvider interface {
	GetStatus() int
}

StatusProvider is an optional interface that response types can implement to expose their HTTP status code for retry eligibility checks.

Jump to

Keyboard shortcuts

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