diff --git a/internal/config/config.go b/internal/config/config.go index aad506f..ac269cf 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -83,6 +83,17 @@ type Auth struct { // client looping with a bad key cannot become a database read per request. NegativeTTL time.Duration `koanf:"negative_ttl"` + // DefaultRPM and DefaultTPM apply to a key that no scope gives a limit to. + // + // Unlimited used to be the default, and a project created without limits is + // the easy path — the "default" project on this cluster still is one. The + // first sign of that is the bill or the outage. Zero keeps the old + // behaviour for anyone who wants it, but it should be a choice rather than + // an omission. A fallback, not a ceiling: an explicit limit at any scope + // wins, above or below. + DefaultRPM int `koanf:"default_rpm"` + DefaultTPM int `koanf:"default_tpm"` + // SignupOpen serves the signup form. Accounts it creates are pending until // an operator approves them, so this is not a way in — but turning it off // means accounts are made with `llmgw users create` instead. diff --git a/internal/router/router.go b/internal/router/router.go index 90a9a4a..9f9a85d 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -173,6 +173,18 @@ func New(cfg config.Server, log *slog.Logger) (*Router, error) { return nil, fmt.Errorf("auth store: %w", err) } r.store = st + // Applied where no scope set one, so a project created without limits + // is still metered. Zero leaves it unlimited, which was the old + // behaviour and is now a choice rather than an omission. + var defRPM, defTPM *int + if cfg.Auth.DefaultRPM > 0 { + defRPM = &cfg.Auth.DefaultRPM + } + if cfg.Auth.DefaultTPM > 0 { + defTPM = &cfg.Auth.DefaultTPM + } + st.SetDefaultLimits(defRPM, defTPM) + r.auth = auth.New(st, auth.Config{ TTL: cfg.Auth.CacheTTL, NegativeTTL: cfg.Auth.NegativeTTL, diff --git a/internal/store/sqlite.go b/internal/store/sqlite.go index f9e97a8..240d450 100644 --- a/internal/store/sqlite.go +++ b/internal/store/sqlite.go @@ -9,6 +9,7 @@ import ( "os" "slices" "strings" + "sync" "time" _ "modernc.org/sqlite" // the cgo-free driver, so the binary stays static @@ -17,6 +18,12 @@ import ( // SQLite is the single-instance store. type SQLite struct { db *sql.DB + + // mu guards the default limits, which are set once at wiring but read on + // every authentication. + mu sync.RWMutex + defaultRPM *int + defaultTPM *int } // OpenSQLite opens (creating if needed) a database and brings it up to date. @@ -301,9 +308,40 @@ func (s *SQLite) Resolve(ctx context.Context, keyID string) (Effective, error) { } e.tighten(l) } + s.applyDefaults(&e) return e, nil } +// SetDefaultLimits sets what applies to a key no scope gives a limit to. +// +// A default is a fallback, not a ceiling, so it is applied after the chain and +// only where the chain left a limit unset — an explicit project limit above the +// default must win, which it would not if this were folded in as the widest +// scope and tightened. +// +// It exists because unlimited was the default. A project created without +// limits — which is the easy path, and what the "default" project on this +// cluster still is — metered nothing at all, and the first sign would have +// been the bill or the outage. +func (s *SQLite) SetDefaultLimits(rpm, tpm *int) { + s.mu.Lock() + defer s.mu.Unlock() + s.defaultRPM, s.defaultTPM = rpm, tpm +} + +func (s *SQLite) applyDefaults(e *Effective) { + s.mu.RLock() + defer s.mu.RUnlock() + if e.RPM == nil && s.defaultRPM != nil { + v := *s.defaultRPM + e.RPM = &v + } + if e.TPM == nil && s.defaultTPM != nil { + v := *s.defaultTPM + e.TPM = &v + } +} + // tighten applies one scope's limits, keeping whichever is stricter. func (e *Effective) tighten(l Limits) { e.RPM = minPtr(e.RPM, l.RPM) diff --git a/internal/store/store_test.go b/internal/store/store_test.go index 4445a5a..87f6dd0 100644 --- a/internal/store/store_test.go +++ b/internal/store/store_test.go @@ -434,3 +434,62 @@ func TestTheStoreIsNotWorldReadable(t *testing.T) { } } } + +func TestDefaultLimitsApplyWhereNoScopeSetsOne(t *testing.T) { + // Unlimited was the default, and a project created without limits is the + // easy path — the one this cluster's "default" project took. The first + // sign of that is the bill or the outage. + s := openDB(t) + proj := tree(t, s) + _, k := mustKey(t, s, "k1", proj) + + s.SetDefaultLimits(intp(60), intp(90_000)) + + e, err := s.Resolve(context.Background(), k.ID) + if err != nil { + t.Fatal(err) + } + if e.RPM == nil || *e.RPM != 60 { + t.Errorf("RPM = %v, want the default", e.RPM) + } + if e.TPM == nil || *e.TPM != 90_000 { + t.Errorf("TPM = %v, want the default", e.TPM) + } +} + +func TestAnExplicitLimitBeatsTheDefaultInBothDirections(t *testing.T) { + // A default is a fallback, not a ceiling. Folding it in as the widest scope + // and tightening would silently cap a project deliberately given more. + s := openDB(t) + proj := tree(t, s) + _, k := mustKey(t, s, "k1", proj) + + ctx := context.Background() + if err := s.PutLimits(ctx, Limits{Scope: ScopeKey, ScopeID: k.ID, RPM: intp(600)}); err != nil { + t.Fatal(err) + } + s.SetDefaultLimits(intp(60), nil) + + e, err := s.Resolve(ctx, k.ID) + if err != nil { + t.Fatal(err) + } + if e.RPM == nil || *e.RPM != 600 { + t.Errorf("RPM = %v; an explicit limit above the default was overridden", e.RPM) + } +} + +func TestNoDefaultsLeavesLimitsUnset(t *testing.T) { + // The old behaviour, still available deliberately. + s := openDB(t) + proj := tree(t, s) + _, k := mustKey(t, s, "k1", proj) + + e, err := s.Resolve(context.Background(), k.ID) + if err != nil { + t.Fatal(err) + } + if e.RPM != nil || e.TPM != nil { + t.Errorf("limits appeared with no defaults set: rpm=%v tpm=%v", e.RPM, e.TPM) + } +}