Meter a project nobody set limits on

Unlimited was the default. Creating a project without limits is the easy
path and the common one — the "default" project on this cluster is one, and
the person using it is metered by nothing at all. The first sign of that is
the bill or the outage.

auth.default_rpm and default_tpm now apply to a key no scope gives a limit
to. Zero keeps the old behaviour, which is still a reasonable choice; the
point is that it becomes a choice rather than an omission.

Applied after the chain rather than folded in as its widest scope, because
a default is a fallback and not a ceiling: a project deliberately given 600
rpm must keep it, and tightening would silently cap it at the default. An
explicit limit wins in both directions.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5 committed 2026-09-16 23:28:28 +02:00
1 parent 7e65cdf5c9
commit c115681259
4 files changed
+120

No files matched your search

+38
View File
@@ -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)