fallback

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 5 Imported by: 0

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.

func DefaultPolicy

func DefaultPolicy() *Policy

DefaultPolicy returns sensible defaults.

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

func NewService(policy *Policy) Service

NewService creates a new fallback service with the given policy.

type State

type State string

State represents a circuit breaker state.

const (
	StateClosed   State = "closed"    // normal operation
	StateOpen     State = "open"      // all requests fail fast
	StateHalfOpen State = "half-open" // testing recovery
)

Jump to

Keyboard shortcuts

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