mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
feat(router): smart routers and per-member rate limits (#9)
Add smart routers: virtual models that score each request and route it to admin-defined model tiers. A retryable failure moves the request to a higher tier. The classifier v2 extracts prose, finds the original ask in long tool loops, adds an uncertainty margin and reads tool-output failures. Sessions, outcome signals, bounded calibration, adaptive selection within a tier, gated saturation downgrade, a first-byte timeout and an opt-in timeout fallback are included. Every new feature is off by default. Add per-member group limits, a tokensPerMinute window that counts uncached input and output, rate-limit headers in three dialects, soft pacing before the hard limit, and a limitExemptKinds list so embedding and reranking traffic never counts toward or hits a limit. Fix yaegi named-result state that made smartContentSize grow between calls.
This commit is contained in:
@@ -16,7 +16,11 @@ import (
|
||||
|
||||
// user is an authenticated API-key holder resolved by authStore.identify.
|
||||
type user struct {
|
||||
limits *LimitsConfig
|
||||
// limits is the EFFECTIVE limit set: the user's own limits merged with
|
||||
// member groups' memberLimits (effectiveUserLimits, member_limits.go).
|
||||
limits *LimitsConfig
|
||||
// limitSources maps each non-zero limits field to its origin.
|
||||
limitSources map[string]string
|
||||
name, groupName string
|
||||
// groupNames is this user's full, de-duplicated, order-preserving
|
||||
// member group list (effectiveGroupNames, below) — groupName above
|
||||
@@ -35,6 +39,9 @@ type user struct {
|
||||
// providers, models, mcpServers, or agents means all are allowed.
|
||||
type group struct {
|
||||
limits *LimitsConfig
|
||||
// memberLimits is GroupConfig.MemberLimits: per-member defaults folded
|
||||
// into each member user's effective limits at entry build time.
|
||||
memberLimits *LimitsConfig
|
||||
// cache is GroupConfig.Cache carried through unchanged: nil inherits
|
||||
// the global cache.enabled setting, non-nil overrides it for this
|
||||
// group's requests. Resolved by groupCacheEnabled (cache.go).
|
||||
@@ -188,6 +195,20 @@ func (grp *group) allowsProviderModel(provider string, ids ...string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// allowsModelName reports whether SOME grant's model globs match id, ignoring
|
||||
// providers: a smart router has no provider of its own.
|
||||
func (grp *group) allowsModelName(id string) bool {
|
||||
if grp.grants == nil {
|
||||
return matchesGlob(grp.models, id)
|
||||
}
|
||||
for _, g := range grp.grants {
|
||||
if matchesGlob(g.models, id) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// memberScopeGroups returns the *group values buildLimitScopes (routes_
|
||||
// unified.go) builds one "group" limit scope per: grp.memberGroups for a
|
||||
// synthetic multi-membership/personal-grant principal (effectiveGroup,
|
||||
@@ -739,6 +760,12 @@ func newAuthStore(cfg *Config) (*authStore, error) {
|
||||
if err := gc.Limits.validate(); err != nil {
|
||||
return nil, fmt.Errorf("llmgateway: group %q: %w", name, err)
|
||||
}
|
||||
if gc.Limits != nil && gc.Limits.Pacing != nil {
|
||||
return nil, fmt.Errorf("llmgateway: group %q: pacing is not supported on a group's aggregate limits; set it in memberLimits or on a user", name)
|
||||
}
|
||||
if err := gc.MemberLimits.validate(); err != nil {
|
||||
return nil, fmt.Errorf("llmgateway: group %q memberLimits: %w", name, err)
|
||||
}
|
||||
// A group cannot opt into caching (Cache: true) when the global
|
||||
// cache block itself is not configured (cfg.Cache.Enabled false)
|
||||
// — there is no ttl/maxBodyBytes to inherit, and nothing for
|
||||
@@ -762,6 +789,7 @@ func newAuthStore(cfg *Config) (*authStore, error) {
|
||||
}
|
||||
a.groups[name] = &group{
|
||||
limits: gc.Limits,
|
||||
memberLimits: gc.MemberLimits,
|
||||
cache: gc.Cache,
|
||||
cacheTTL: cacheTTL,
|
||||
name: name,
|
||||
@@ -1020,15 +1048,17 @@ func (a *authStore) buildEntry(uc *UserConfig) (*authEntry, error) {
|
||||
if key == "" {
|
||||
return nil, fmt.Errorf("llmgateway: user %q has an empty API key", uc.Name)
|
||||
}
|
||||
effLimits, limitSources := effectiveUserLimits(uc.Limits, members)
|
||||
digest := sha256.Sum256([]byte(key))
|
||||
return &authEntry{
|
||||
digest: digest,
|
||||
user: &user{
|
||||
limits: uc.Limits,
|
||||
groupNames: groupNames,
|
||||
name: uc.Name,
|
||||
groupName: groupNames[0],
|
||||
admin: uc.Admin,
|
||||
limits: effLimits,
|
||||
limitSources: limitSources,
|
||||
groupNames: groupNames,
|
||||
name: uc.Name,
|
||||
groupName: groupNames[0],
|
||||
admin: uc.Admin,
|
||||
},
|
||||
group: effective,
|
||||
}, nil
|
||||
@@ -1231,14 +1261,19 @@ func (a *authStore) identify(r *http.Request) (*user, *group, bool) {
|
||||
// presentedKey extracts the API key from r's Authorization Bearer header or
|
||||
// its x-api-key header. Bearer takes priority when both are set.
|
||||
func presentedKey(r *http.Request) (string, bool) {
|
||||
if authz := r.Header.Get("Authorization"); authz != "" {
|
||||
return presentedKeyFromHeader(r.Header)
|
||||
}
|
||||
|
||||
// presentedKeyFromHeader is presentedKey over a bare header set.
|
||||
func presentedKeyFromHeader(h http.Header) (string, bool) {
|
||||
if authz := h.Get("Authorization"); authz != "" {
|
||||
if scheme, rest, found := strings.Cut(authz, " "); found && strings.EqualFold(scheme, "bearer") {
|
||||
if key := strings.TrimSpace(rest); key != "" {
|
||||
return key, true
|
||||
}
|
||||
}
|
||||
}
|
||||
if key := strings.TrimSpace(r.Header.Get("x-api-key")); key != "" {
|
||||
if key := strings.TrimSpace(h.Get("x-api-key")); key != "" {
|
||||
return key, true
|
||||
}
|
||||
return "", false
|
||||
@@ -1252,6 +1287,7 @@ func presentedKey(r *http.Request) (string, bool) {
|
||||
// byDigest itself.
|
||||
type userSummary struct {
|
||||
limits *LimitsConfig
|
||||
limitSources map[string]string
|
||||
name string
|
||||
groupName string
|
||||
source string
|
||||
@@ -1281,6 +1317,7 @@ type userSummary struct {
|
||||
// on.
|
||||
type groupSummary struct {
|
||||
limits *LimitsConfig
|
||||
memberLimits *LimitsConfig
|
||||
name string
|
||||
providers []string
|
||||
models []string
|
||||
@@ -1311,6 +1348,7 @@ func (a *authStore) snapshot() ([]userSummary, []groupSummary) {
|
||||
}
|
||||
users = append(users, userSummary{
|
||||
limits: entry.user.limits,
|
||||
limitSources: entry.user.limitSources,
|
||||
name: entry.user.name,
|
||||
groupName: entry.user.groupName,
|
||||
groups: cloneStringSlice(entry.user.groupNames),
|
||||
@@ -1336,6 +1374,7 @@ func (a *authStore) snapshot() ([]userSummary, []groupSummary) {
|
||||
for name, grp := range a.groups {
|
||||
groups = append(groups, groupSummary{
|
||||
limits: grp.limits,
|
||||
memberLimits: grp.memberLimits,
|
||||
name: name,
|
||||
providers: cloneStringSlice(grp.providers),
|
||||
models: cloneStringSlice(grp.models),
|
||||
|
||||
Reference in New Issue
Block a user