routing

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StrategyOrdered  = "ordered"
	StrategyWeighted = "weighted"
	StrategyLatency  = "latency"
	StrategyCost     = "cost"
)

Strategy values mirrored from gatewayconfig / snapshot.

Variables

This section is empty.

Functions

This section is empty.

Types

type Candidate

type Candidate struct {
	ProviderID   string
	ProviderType string
	TargetModel  string
	Weight       int
}

Candidate is a provider+model target considered for an attempt list.

type CostSelector

type CostSelector struct{}

CostSelector orders by ascending catalog unit price; unknown prices last.

func (CostSelector) Order

func (CostSelector) Order(candidates []Candidate, _ *rand.Rand) []Candidate

type LatencySelector

type LatencySelector struct {
	Store SignalStore
}

LatencySelector orders by ascending EWMA latency; unknowns get the median of known.

func (LatencySelector) Order

func (s LatencySelector) Order(candidates []Candidate, _ *rand.Rand) []Candidate

type MemorySignalStore

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

MemorySignalStore keeps gateway-local EWMA latency and error rates in process memory. Multi-instance skew is accepted; no Redis/Postgres.

func NewMemorySignalStore

func NewMemorySignalStore() *MemorySignalStore

NewMemorySignalStore returns an empty in-process signal store.

func (*MemorySignalStore) ErrorRate

func (s *MemorySignalStore) ErrorRate(providerID, targetModel string) (rate float64, ok bool)

func (*MemorySignalStore) LatencyEWMA

func (s *MemorySignalStore) LatencyEWMA(providerID, targetModel string) (ms float64, ok bool)

func (*MemorySignalStore) Observe

func (s *MemorySignalStore) Observe(providerID, targetModel string, latencyMs int64, failed bool)

type OrderedSelector

type OrderedSelector struct{}

OrderedSelector keeps config order (primary then fallbacks).

func (OrderedSelector) Order

func (OrderedSelector) Order(candidates []Candidate, _ *rand.Rand) []Candidate

type Selector

type Selector interface {
	Order(candidates []Candidate, rng *rand.Rand) []Candidate
}

Selector orders candidates for the first attempt and subsequent failovers.

func ForStrategy

func ForStrategy(strategy string, signals SignalStore) Selector

ForStrategy returns the selector for a routing strategy (unknown → ordered). signals is used by latency; nil signals fall back to ordered for latency.

type SignalStore

type SignalStore interface {
	Observe(providerID, targetModel string, latencyMs int64, failed bool)
	LatencyEWMA(providerID, targetModel string) (ms float64, ok bool)
	ErrorRate(providerID, targetModel string) (rate float64, ok bool)
}

SignalStore records and queries gateway-local upstream signals for adaptive routing.

type WeightedSelector

type WeightedSelector struct{}

WeightedSelector picks the first attempt by weight; remainder keep config order.

func (WeightedSelector) Order

func (WeightedSelector) Order(candidates []Candidate, rng *rand.Rand) []Candidate

Jump to

Keyboard shortcuts

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