store

package
v0.3.0 Latest Latest
Warning

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

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

Documentation

Overview

Package store provides cross-request state for rate limiting, circuit breaking, sticky sessions, and latency stats. The StateStore interface lives in core (core.StateStore).

Index

Constants

View Source
const (
	RedisKeyConfigModelVersions = "aigw:config:model_versions"
)

Variables

View Source
var (
	// ErrKeyNotFound is returned when a key does not exist.
	ErrKeyNotFound = errors.New("key not found")
)

Functions

func RedisKeyAlias

func RedisKeyAlias(alias string) string

RedisKeyAlias returns the Redis key for an alias mapping.

func RedisKeyApiKeyHash

func RedisKeyApiKeyHash(keyHash string) string

func RedisKeyConfigEndpoints

func RedisKeyConfigEndpoints(modelCode string) string

RedisKeyConfigEndpoints returns the endpoints config key for a model_code.

func RedisKeyModelAliases

func RedisKeyModelAliases(modelCode string) string

RedisKeyModelAliases returns the Redis key for a model's alias set (reverse index).

func RedisKeyTenantEndpoints

func RedisKeyTenantEndpoints(tenantCode, modelCode string) string

func RedisKeyUserModels

func RedisKeyUserModels(userID string) string

Types

type ExpirableCache

type ExpirableCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func NewExpirableCache

func NewExpirableCache[K comparable, V any](validCap int, validTTL time.Duration, invalidCap int, invalidTTL time.Duration) *ExpirableCache[K, V]

func (*ExpirableCache[K, V]) AddInvalid

func (c *ExpirableCache[K, V]) AddInvalid(key K, errMsg string)

func (*ExpirableCache[K, V]) AddValid

func (c *ExpirableCache[K, V]) AddValid(key K, val V)

func (*ExpirableCache[K, V]) Get

func (c *ExpirableCache[K, V]) Get(key K) (V, string, bool)

Get queries valid and invalid caches. Returns (value, errorMsg, exists).

func (*ExpirableCache[K, V]) Remove

func (c *ExpirableCache[K, V]) Remove(key K)

type MemoryStateStore

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

MemoryStateStore is an in-memory StateStore for single-instance and tests.

func NewMemoryStateStore

func NewMemoryStateStore() *MemoryStateStore

NewMemoryStateStore creates a MemoryStateStore.

func (*MemoryStateStore) Close

func (s *MemoryStateStore) Close() error

Close is a no-op for MemoryStateStore.

func (*MemoryStateStore) GetAvgLatency

func (s *MemoryStateStore) GetAvgLatency(ctx context.Context, endpointID string, window time.Duration) (time.Duration, error)

GetAvgLatency returns average latency for endpointID within window, or 0 if none.

func (*MemoryStateStore) GetAvgTTFT

func (s *MemoryStateStore) GetAvgTTFT(ctx context.Context, endpointID string, window time.Duration) (time.Duration, error)

GetAvgTTFT returns average TTFT for endpointID within window, or 0 if none.

func (*MemoryStateStore) GetEMA

func (s *MemoryStateStore) GetEMA(ctx context.Context, key string) (float64, error)

GetEMA returns the cached EMA for key, or 0 if missing.

func (*MemoryStateStore) RateLimitIncr

func (s *MemoryStateStore) RateLimitIncr(ctx context.Context, key string, tokens int64, window time.Duration) (int64, error)

RateLimitIncr adds tokens to the key counter and returns usage in the window. window sets the window size; if the key exists with a different window, it takes effect on the next reset.

func (*MemoryStateStore) RateLimitRefund

func (s *MemoryStateStore) RateLimitRefund(ctx context.Context, key string, tokens int64) error

RateLimitRefund refunds tokens to the key counter.

func (*MemoryStateStore) RateLimitTake

func (s *MemoryStateStore) RateLimitTake(ctx context.Context, key string, tokens int64, rate int64, capacity int64, window time.Duration, now time.Time) (bool, int64, error)

RateLimitTake tries to consume tokens from a token bucket (smooth burst limit).

func (*MemoryStateStore) RecordLatency

func (s *MemoryStateStore) RecordLatency(ctx context.Context, endpointID string, latency time.Duration) error

RecordLatency records a latency sample.

func (*MemoryStateStore) RecordTTFT

func (s *MemoryStateStore) RecordTTFT(ctx context.Context, endpointID string, ttft time.Duration) error

RecordTTFT records a TTFT sample on a series separate from total latency.

func (*MemoryStateStore) StickyGet

func (s *MemoryStateStore) StickyGet(ctx context.Context, sessionKey string) (string, error)

StickyGet returns the endpointID for sessionKey, or ErrKeyNotFound if expired/missing.

func (*MemoryStateStore) StickySet

func (s *MemoryStateStore) StickySet(ctx context.Context, sessionKey string, endpointID string, ttl time.Duration) error

StickySet maps sessionKey to endpointID with the given TTL.

func (*MemoryStateStore) UpdateEMA

func (s *MemoryStateStore) UpdateEMA(ctx context.Context, key string, actual int64, alpha float64) (float64, error)

UpdateEMA updates the EMA for key and returns the new value.

type RedisStateStore

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

RedisStateStore is a Redis-backed StateStore for multi-instance production. Uses redis.Cmdable (works with redis.Client and redis.ClusterClient).

func NewRedisStateStore

func NewRedisStateStore(client redis.Cmdable, cfg *RedisStateStoreConfig) *RedisStateStore

NewRedisStateStore creates a RedisStateStore. client may be *redis.Client or *redis.ClusterClient.

func (*RedisStateStore) Close

func (s *RedisStateStore) Close() error

Close closes the Redis connection if the client is closable.

func (*RedisStateStore) GetAvgLatency

func (s *RedisStateStore) GetAvgLatency(ctx context.Context, endpointID string, window time.Duration) (time.Duration, error)

GetAvgLatency returns average latency for endpointID within window, or 0 if none.

func (*RedisStateStore) GetAvgTTFT

func (s *RedisStateStore) GetAvgTTFT(ctx context.Context, endpointID string, window time.Duration) (time.Duration, error)

GetAvgTTFT returns average TTFT for endpointID within window, or 0 if none.

func (*RedisStateStore) GetEMA

func (s *RedisStateStore) GetEMA(ctx context.Context, key string) (float64, error)

GetEMA returns the EMA estimate from Redis, or 0 if missing.

func (*RedisStateStore) RateLimitIncr

func (s *RedisStateStore) RateLimitIncr(ctx context.Context, key string, tokens int64, window time.Duration) (int64, error)

RateLimitIncr adds tokens to the key counter and returns usage in the window.

func (*RedisStateStore) RateLimitRefund

func (s *RedisStateStore) RateLimitRefund(ctx context.Context, key string, tokens int64) error

RateLimitRefund refunds tokens to the key counter.

func (*RedisStateStore) RateLimitTake

func (s *RedisStateStore) RateLimitTake(ctx context.Context, key string, tokens int64, rate int64, capacity int64, window time.Duration, now time.Time) (bool, int64, error)

RateLimitTake tries to consume tokens from a token bucket (smooth burst limit).

func (*RedisStateStore) RecordLatency

func (s *RedisStateStore) RecordLatency(ctx context.Context, endpointID string, latency time.Duration) error

RecordLatency records a latency sample in a Redis sorted set. Score is timestamp (ms); member is "<latency_ns>:<rand>". Atomically adds the sample, sets TTL, and trims old samples.

func (*RedisStateStore) RecordTTFT

func (s *RedisStateStore) RecordTTFT(ctx context.Context, endpointID string, ttft time.Duration) error

RecordTTFT records a time-to-first-token sample on a key separate from total latency.

func (*RedisStateStore) StickyGet

func (s *RedisStateStore) StickyGet(ctx context.Context, sessionKey string) (string, error)

StickyGet returns the endpointID for sessionKey, or ErrKeyNotFound.

func (*RedisStateStore) StickySet

func (s *RedisStateStore) StickySet(ctx context.Context, sessionKey string, endpointID string, ttl time.Duration) error

StickySet maps sessionKey to endpointID with the given TTL.

func (*RedisStateStore) UpdateEMA

func (s *RedisStateStore) UpdateEMA(ctx context.Context, key string, actual int64, alpha float64) (float64, error)

UpdateEMA updates the EMA estimate in Redis and returns the new value.

type RedisStateStoreConfig

type RedisStateStoreConfig struct {
	// KeyPrefix is the Redis key namespace (default "aigw").
	KeyPrefix string

	// LatencyMaxSamples is the max latency samples retained (default 1000).
	LatencyMaxSamples int
}

RedisStateStoreConfig configures RedisStateStore.

Jump to

Keyboard shortcuts

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