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:
1 parent
7e65cdf5c9
commit
c115681259
4 files changed
+120
No files matched your search
@@ -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.
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user