Documentation
¶
Overview ¶
Package fallback defines the resilience and fallback subsystem.
The service calls providers directly, outside the gateway pipeline, so it bypasses credential resolution (nexus.WithCredentialProvider): every call it makes uses each provider's configured key and base URL, whatever tenant the request belongs to. Route tenant traffic through the gateway's Engine instead.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker implements the circuit breaker pattern per provider.
func NewCircuitBreaker ¶
func NewCircuitBreaker(threshold int, timeout time.Duration) *CircuitBreaker
NewCircuitBreaker creates a new circuit breaker.
func (*CircuitBreaker) Allow ¶
func (cb *CircuitBreaker) Allow() bool
Allow returns true if the request should proceed.
func (*CircuitBreaker) RecordFailure ¶
func (cb *CircuitBreaker) RecordFailure() bool
RecordFailure marks a failed call and may trip the circuit.
func (*CircuitBreaker) RecordSuccess ¶
func (cb *CircuitBreaker) RecordSuccess()
RecordSuccess marks a successful call.
func (*CircuitBreaker) Reset ¶
func (cb *CircuitBreaker) Reset()
Reset resets the circuit breaker to closed state.
func (*CircuitBreaker) State ¶
func (cb *CircuitBreaker) State() State
State returns the current state.
type Policy ¶
type Policy struct {
MaxRetries int `json:"max_retries"` // per provider
RetryDelay time.Duration `json:"retry_delay"` // initial delay
RetryBackoff float64 `json:"retry_backoff"` // multiplier (e.g., 2.0)
Timeout time.Duration `json:"timeout"` // per-request timeout
CircuitThreshold int `json:"circuit_threshold"` // failures before open
CircuitTimeout time.Duration `json:"circuit_timeout"` // open duration
}
Policy defines retry and fallback behavior.
type Service ¶
type Service interface {
// Execute calls the provider with retry + circuit breaker + fallback.
Execute(ctx context.Context, primary provider.Provider, fallbacks []provider.Provider, req *provider.CompletionRequest) (*provider.CompletionResponse, error)
// ExecuteStream is the streaming counterpart to Execute. Primary and
// each fallback are tried in turn; the first one whose CompleteStream
// returns successfully wins. Mid-stream failures (errors surfaced from
// Stream.Next after a chunk has been delivered) are NOT retried — the
// consumer has already begun receiving data, and replaying upstream
// would duplicate content.
ExecuteStream(ctx context.Context, primary provider.Provider, fallbacks []provider.Provider, req *provider.CompletionRequest) (provider.Stream, error)
// CircuitState returns the circuit breaker state for a provider.
CircuitState(providerName string) State
// Reset resets the circuit breaker for a provider.
Reset(providerName string)
}
Service manages provider-level resilience.
func NewService ¶
NewService creates a new fallback service with the given policy.