Documentation
¶
Index ¶
- Constants
- func CalculateHTTPBackoff(config types.RetryConfig, attempt int) time.Duration
- func Is529Error(resp *http.Response, err error) bool
- func IsModelDeprecatedError(err error, resp *http.Response) bool
- func IsQuotaExhaustedError(err error, resp *http.Response) bool
- func IsRateLimitError(resp *http.Response) bool
- func IsRegionallyUnavailableError(err error, resp *http.Response) bool
- func IsRetryableStatus(statusCode int) bool
- func ParseContextOverflowError(err error) (inputTokens, maxTokens, contextLimit int, ok bool)
- func ParseRetryAfterHeader(resp *http.Response) time.Duration
- func RecoveryHint(classification RetryClassification, modelID string) string
- func RetryHTTP(ctx context.Context, config types.RetryConfig, send HTTPRequestFunc, ...) (*http.Response, error)
- func RetryHTTPNotify(ctx context.Context, config types.RetryConfig, send HTTPRequestFunc, ...) (*http.Response, error)
- func RetryWithStrategy(ctx context.Context, strategy *RetryStrategy, model types.ModelIdentifier, ...) (*types.APIResponse, error)
- func ShouldRetryHTTP(config types.RetryConfig, classification RetryClassification, attempt int) bool
- type CircuitOpenChecker
- type HTTPRequestFunc
- type HTTPResponseErrorFunc
- type RequestFunc
- type RetryClassification
- type RetryNotice
- type RetryNotifier
- type RetryState
- type RetryStrategy
- type RetryableResponseInspector
Constants ¶
const MaxRetryAfter = 2 * time.Minute
MaxRetryAfter is the longest wait a provider can ask for in Retry-After and still be waited for. A longer one fails the request with the provider's own message, so that the user reads it instead of watching a spinner.
Variables ¶
This section is empty.
Functions ¶
func CalculateHTTPBackoff ¶
func CalculateHTTPBackoff(config types.RetryConfig, attempt int) time.Duration
func IsModelDeprecatedError ¶
IsModelDeprecatedError returns true when the provider signals that the requested model is no longer available (retired, renamed, or removed).
func IsRateLimitError ¶
func IsRegionallyUnavailableError ¶
IsRegionallyUnavailableError returns true when the service is reachable but not available in the current region or endpoint configuration.
func IsRetryableStatus ¶
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)
RetryHTTP sends a request and retries it as RetryHTTPNotify does, without telling anyone about the waits.
func RetryHTTPNotify ¶ added in v1.8.0
func RetryHTTPNotify( ctx context.Context, config types.RetryConfig, send HTTPRequestFunc, buildResponseError HTTPResponseErrorFunc, isCircuitOpen CircuitOpenChecker, notify RetryNotifier, ) (*http.Response, error)
RetryHTTPNotify sends a request and retries it on a refused or reset connection, a 429 and a 5xx, with the wait the provider asks for in Retry-After or an exponential one. notify, when set, is called before each wait.
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 ¶
CircuitOpenChecker reports whether an error comes from an open circuit breaker.
type HTTPRequestFunc ¶
HTTPRequestFunc is the HTTP-level request function executed under retry control.
type HTTPResponseErrorFunc ¶
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" // 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 RetryNotice ¶ added in v1.8.0
type RetryNotice struct {
// Attempt is the number of the attempt about to be made (2 for the first retry).
Attempt int
// MaxAttempts is the number of attempts made in all.
MaxAttempts int
// Wait is how long the retry waits before that attempt.
Wait time.Duration
// Reason says why the request is retried.
Reason string
}
RetryNotice describes a wait before a retry, for a client that wants to say what it is waiting for.
type RetryNotifier ¶ added in v1.8.0
type RetryNotifier func(RetryNotice)
RetryNotifier is called before each wait.
type RetryState ¶
type RetryState struct {
// contains filtered or unexported fields
}
RetryState contains retry and circuit breaker 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.