memory

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package memory implements the zero-dependency reference store for the open-source core: request logging, the budget ledger, settings, and caller resolution all live in process memory. It is the offline runtime behind `cmd/proxy` when no SQLite path is configured, and the reference implementation for tests. Nothing here imports anything beyond stdlib and the core pkg/proxy seams.

Index

Constants

View Source
const DefaultLoopThreshold = 2

DefaultLoopThreshold mirrors the detector's repeat-threshold default (first sighting passes, the second repeat blocks). It seeds SettingsRow until an UpdateSettings call.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyStore

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

KeyStore is an in-memory proxy.CallerStore: a SHA-256 key-hash -> Caller map for offline per-key budgets, seeded from a keys file or by tests. It is safe for concurrent use.

func NewKeyStore

func NewKeyStore() *KeyStore

NewKeyStore returns an empty KeyStore.

func (*KeyStore) CallerByKeyHash

func (k *KeyStore) CallerByKeyHash(_ context.Context, hash string) (proxy.Caller, error)

CallerByKeyHash implements proxy.CallerStore. An unknown hash surfaces as proxy.ErrInvalidKey so the middleware answers 401 (fail-closed).

func (*KeyStore) Set

func (k *KeyStore) Set(hash string, c proxy.Caller)

Set binds (or replaces) the caller for a key hash. hash is the lowercase hex SHA-256 of the raw key (proxy.HashAPIKey).

type Store

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

Store is the in-memory proxy.Store. It is safe for concurrent use: a single mutex serializes every read and write.

func New

func New() *Store

New returns an empty in-memory Store. It satisfies proxy.Store and, through its embedded caller map, proxy.CallerStore.

func (*Store) CallerByKeyHash

func (s *Store) CallerByKeyHash(ctx context.Context, hash string) (proxy.Caller, error)

CallerByKeyHash implements proxy.CallerStore via the embedded KeyStore.

func (*Store) GetSettings

func (s *Store) GetSettings(_ context.Context) (proxy.SettingsRow, error)

GetSettings implements proxy.SettingsStore, returning the current snapshot or the defaults before the first update.

func (*Store) LogRequest

func (s *Store) LogRequest(_ context.Context, r proxy.RequestRecord) error

LogRequest implements proxy.RequestLogStore by appending one record.

func (*Store) Logs

func (s *Store) Logs() []proxy.RequestRecord

Logs returns a copy of every recorded request, oldest first. It is a test and diagnostics helper, not part of any seam.

func (*Store) Set

func (s *Store) Set(hash string, c proxy.Caller)

Set binds (or replaces) the caller for a key hash. It matches KeyStore.Set, so cmd/proxy's loadKeys can seed the in-memory store (the -keys flag) through the same seam as KeyStore. hash is the lowercase hex SHA-256 of the raw key.

func (*Store) SetCaller

func (s *Store) SetCaller(hash string, c proxy.Caller)

SetCaller binds (or replaces) the caller for a key hash, for offline per-key budgets. It is an alias of Set kept for existing callers.

func (*Store) SumActualCostForKeySince

func (s *Store) SumActualCostForKeySince(_ context.Context, keyID string, since int64) (int64, error)

SumActualCostForKeySince implements proxy.BudgetLedger: the sum of actual cost for one caller's requests at or after since. The empty keyID selects the anonymous/legacy bucket.

func (*Store) SumActualCostSince

func (s *Store) SumActualCostSince(_ context.Context, since int64) (int64, error)

SumActualCostSince implements proxy.BudgetLedger: the sum of actual (non-loop-blocked) cost at or after since, across every caller.

func (*Store) UpdateSettings

func (s *Store) UpdateSettings(_ context.Context, row proxy.SettingsRow) error

UpdateSettings implements proxy.SettingsStore by replacing the snapshot.

Jump to

Keyboard shortcuts

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