retry

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateHTTPBackoff

func CalculateHTTPBackoff(config types.RetryConfig, attempt int) time.Duration

func Is529Error

func Is529Error(resp *http.Response, err error) bool

func IsModelDeprecatedError

func IsModelDeprecatedError(err error, resp *http.Response) bool

IsModelDeprecatedError returns true when the provider signals that the requested model is no longer available (retired, renamed, or removed).

func IsQuotaExhaustedError

func IsQuotaExhaustedError(err error, resp *http.Response) bool

func IsRateLimitError

func IsRateLimitError(resp *http.Response) bool

func IsRegionallyUnavailableError

func IsRegionallyUnavailableError(err error, resp *http.Response) bool

IsRegionallyUnavailableError returns true when the service is reachable but not available in the current region or endpoint configuration.

func IsRetryableStatus

func IsRetryableStatus(statusCode int) bool

func ParseContextOverflowError

func ParseContextOverflowError(err error) (inputTokens, maxTokens, contextLimit int, ok bool)

func ParseRetryAfterHeader

func ParseRetryAfterHeader(resp *http.Response) time.Duration

func RecoveryHint

func RecoveryHint(classification RetryClassification, modelID string) string

RecoveryHint returns a human-readable suggestion for the operator based on the classification. Returns empty string for classifications that have no actionable hint beyond retrying.

func RetryHTTP

func RetryHTTP(
	ctx context.Context,
	config types.RetryConfig,
	send HTTPRequestFunc,
	buildResponseError HTTPResponseErrorFunc,
	isCircuitOpen CircuitOpenChecker,
) (*http.Response, error)

func RetryWithStrategy

func RetryWithStrategy(
	ctx context.Context,
	strategy *RetryStrategy,
	model types.ModelIdentifier,
	maxTokens int,
	fn RequestFunc,
) (*types.APIResponse, error)

func ShouldRetryHTTP

func ShouldRetryHTTP(config types.RetryConfig, classification RetryClassification, attempt int) bool

Types

type CircuitOpenChecker

type CircuitOpenChecker func(err error) bool

CircuitOpenChecker reports whether an error comes from an open circuit breaker.

type HTTPRequestFunc

type HTTPRequestFunc func(ctx context.Context) (*http.Response, error)

HTTPRequestFunc is the HTTP-level request function executed under retry control.

type HTTPResponseErrorFunc

type HTTPResponseErrorFunc func(resp *http.Response) error

HTTPResponseErrorFunc translates a non-2xx HTTP response into an error. Implementations may consume and restore the response body as needed.

type RequestFunc

type RequestFunc func(ctx context.Context, attempt int, maxTokens int, model types.ModelIdentifier) (*types.APIResponse, error)

RequestFunc is the function executed under advanced retry control.

type RetryClassification

type RetryClassification string

RetryClassification represents the type of error for retry logic.

const (
	RetryClassificationNetwork        RetryClassification = "network"
	RetryClassificationRateLimit      RetryClassification = "rate_limit"
	RetryClassificationServerOverload RetryClassification = "server_overload"
	RetryClassificationTimeout        RetryClassification = "timeout"
	RetryClassificationServerError    RetryClassification = "server_error"
	RetryClassificationClientError    RetryClassification = "client_error"
	RetryClassificationAuthError      RetryClassification = "auth_error"
	RetryClassificationUnknown        RetryClassification = "unknown"

	// RetryClassificationModelDeprecated is returned when the provider rejects
	// the request because the model identifier is no longer available. The
	// caller should switch to an active fallback model and not retry with the
	// same model name.
	RetryClassificationModelDeprecated RetryClassification = "model_deprecated"

	// RetryClassificationRegionallyUnavailable is returned when the service is
	// reachable but not available in the configured region or endpoint. The
	// caller may retry with a different region or endpoint.
	RetryClassificationRegionallyUnavailable RetryClassification = "regionally_unavailable"

	// RetryClassificationQuotaExhausted is returned when the account has
	// exhausted its quota (distinct from a transient rate-limit). Retrying
	// immediately will not help; the caller should either wait until the
	// quota resets or switch to a different provider.
	RetryClassificationQuotaExhausted RetryClassification = "quota_exhausted"
)

func ClassifyHTTPError

func ClassifyHTTPError(err error, statusCode int) RetryClassification

type RetryState

type RetryState struct {
	// contains filtered or unexported fields
}

RetryState contains retry and circuit breaker state.

func NewRetryState

func NewRetryState() *RetryState

NewRetryState creates a new retry state.

func (*RetryState) IncrementFailure

func (s *RetryState) IncrementFailure()

func (*RetryState) IsCircuitOpen

func (s *RetryState) IsCircuitOpen(maxFailures int) bool

func (*RetryState) ResetConsecutiveFailures

func (s *RetryState) ResetConsecutiveFailures()

type RetryStrategy

type RetryStrategy struct {
	MaxRetries                 int
	MaxConsecutiveFailures     int
	FallbackModel              *types.ModelIdentifier
	Max529Errors               int
	EnableContextOverflowRetry bool
	BaseDelayMs                int64
	MaxBackoffMs               int64
}

RetryStrategy defines advanced retry options.

func DefaultRetryStrategy

func DefaultRetryStrategy() *RetryStrategy

DefaultRetryStrategy returns the default retry strategy.

type RetryableResponseInspector

type RetryableResponseInspector func(resp *http.Response, err error) bool

RetryableResponseInspector allows packages to inject HTTP-specific retry logic.

Jump to

Keyboard shortcuts

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