Files
traefik-llmgateway/limit_exempt_test.go
T
lukaszraczyloandGitHub 4c61d19a72 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.
2026-11-25 00:40:30 +00:00

137 lines
5.8 KiB
Go

package traefikllmgateway
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func exemptGateway(t *testing.T, exempt []string, limits *LimitsConfig, kinds map[string]string) (http.Handler, *Gateway) {
t.Helper()
groups := map[string]*GroupConfig{"friends": {}}
users := []*UserConfig{{Name: "a", Group: "friends", APIKey: "sk-a", Limits: limits}}
return memberLimitsGateway(t, groups, users, withRateLimitHeaders, func(c *Config) {
c.LimitExemptKinds = exempt
c.Providers["openai"].Kinds = kinds
})
}
func exemptPost(h http.Handler, path, body string) *httptest.ResponseRecorder {
req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body))
req.Header.Set("Authorization", "Bearer sk-a")
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
return rec
}
const embedBody = `{"model":"gpt-test","input":"hi"}`
func TestLimitExempt_EmbeddingsNeverLimitedWhileChatIs(t *testing.T) {
h, gw := exemptGateway(t, []string{"embedding"}, &LimitsConfig{
RequestsPerMinute: 2, TokensPerMinute: 100, RequestsPerDay: 3,
Pacing: &PacingConfig{StartPct: 1, MaxDelay: "2s"},
}, nil)
start := time.Now()
for i := 0; i < 6; i++ {
rec := exemptPost(h, "/v1/embeddings", embedBody)
require.Equal(t, http.StatusOK, rec.Code, "embedding %d: %s", i+1, rec.Body.String())
assert.Empty(t, rec.Header().Get(pacedHeader), "embedding %d is never paced", i+1)
for k := range rec.Header() {
assert.False(t, isRateLimitHeader(k), "exempt responses carry no rate-limit header, got %s", k)
}
}
assert.Less(t, time.Since(start), time.Second)
x, _ := gw.limiter.getCounter("user", "a", metricExempt, windowMonth, gw.limiter.now())
assert.EqualValues(t, 6, x, "exempt requests are still counted, in the exempt counter")
tok, _ := gw.limiter.getCounter(kindModel, "openai/gpt-test", metricTokIn, windowDay, gw.limiter.now())
assert.EqualValues(t, 6*30, tok, "exempt usage is still in per-model usage")
assert.Equal(t, http.StatusOK, memberLimitsChat(t, h, "sk-a").Code)
assert.Equal(t, http.StatusOK, memberLimitsChat(t, h, "sk-a").Code)
rec := memberLimitsChat(t, h, "sk-a")
assert.Equal(t, http.StatusTooManyRequests, rec.Code, "chat is still limited, by its own requests only")
assert.NotEmpty(t, rec.Header().Get("x-ratelimit-limit-requests"))
}
func TestLimitExempt_UnsetKeepsEmbeddingsLimited(t *testing.T) {
h, _ := exemptGateway(t, nil, &LimitsConfig{RequestsPerMinute: 2}, nil)
assert.Equal(t, http.StatusOK, exemptPost(h, "/v1/embeddings", embedBody).Code)
rec := exemptPost(h, "/v1/embeddings", embedBody)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "0", rec.Header().Get("x-ratelimit-remaining-requests"))
assert.Equal(t, http.StatusTooManyRequests, exemptPost(h, "/v1/embeddings", embedBody).Code)
}
func TestLimitExempt_OtherKindStaysLimited(t *testing.T) {
h, _ := exemptGateway(t, []string{"reranking"}, &LimitsConfig{RequestsPerMinute: 1}, nil)
assert.Equal(t, http.StatusOK, exemptPost(h, "/v1/embeddings", embedBody).Code)
assert.Equal(t, http.StatusTooManyRequests, exemptPost(h, "/v1/embeddings", embedBody).Code, "embedding is not in the exempt list")
}
func TestLimitExempt_RerankPassthroughPath(t *testing.T) {
for name, kinds := range map[string]map[string]string{
"configured path kind": {"/v1/score": "reranking"},
"built-in rerank path": nil,
} {
t.Run(name, func(t *testing.T) {
path := "/openai/v1/rerank"
if kinds != nil {
path = "/openai/v1/score"
}
h, gw := exemptGateway(t, []string{"reranking"}, &LimitsConfig{RequestsPerMinute: 1}, kinds)
for i := 0; i < 4; i++ {
rec := exemptPost(h, path, `{"query":"q","documents":["a"]}`)
require.Equal(t, http.StatusOK, rec.Code, "rerank %d: %s", i+1, rec.Body.String())
}
n, _ := gw.limiter.getCounter("user", "a", metricExempt, windowMonth, gw.limiter.now())
assert.EqualValues(t, 4, n, "still counted, in the exempt counter")
})
}
}
func TestLimitExempt_RerankPassthroughByModelKind(t *testing.T) {
h, _ := exemptGateway(t, []string{"reranking"}, &LimitsConfig{RequestsPerMinute: 1},
map[string]string{"bge-reranker": "reranking"})
for i := 0; i < 4; i++ {
rec := exemptPost(h, "/openai/v1/score", `{"model":"bge-reranker","query":"q"}`)
require.Equal(t, http.StatusOK, rec.Code, "scored %d: %s", i+1, rec.Body.String())
}
}
func TestLimitExempt_UnlistedModelKindStaysLimited(t *testing.T) {
h, _ := exemptGateway(t, []string{"reranking"}, &LimitsConfig{RequestsPerMinute: 1},
map[string]string{"bge-reranker": "reranking"})
assert.Equal(t, http.StatusOK, exemptPost(h, "/openai/v1/score", `{"model":"gpt-test"}`).Code)
assert.Equal(t, http.StatusTooManyRequests, exemptPost(h, "/openai/v1/score", `{"model":"gpt-test"}`).Code)
}
func TestLimitExempt_TokenBudgetsNeverBlockExemptRequests(t *testing.T) {
h, _ := exemptGateway(t, []string{"embedding"}, &LimitsConfig{TokensPerMinute: 10, TokensPerDay: 10, CostPerDayUSD: 0.000001}, nil)
for i := 0; i < 4; i++ {
require.Equal(t, http.StatusOK, exemptPost(h, "/v1/embeddings", embedBody).Code, "embedding %d", i+1)
}
}
func TestLimitExempt_UnsetPassthroughDoesNotPeekBody(t *testing.T) {
h, _ := exemptGateway(t, nil, &LimitsConfig{RequestsPerMinute: 1}, map[string]string{"/v1/rerank": "reranking"})
assert.Equal(t, http.StatusOK, exemptPost(h, "/openai/v1/rerank", `{"q":1}`).Code)
assert.Equal(t, http.StatusTooManyRequests, exemptPost(h, "/openai/v1/rerank", `{"q":1}`).Code)
}
func TestLimitExempt_RejectsBlankKind(t *testing.T) {
cfg := CreateConfig()
cfg.Providers = map[string]*ProviderConfig{"openai": {Type: providerTypeOpenAI, BaseURL: "http://127.0.0.1:1", APIKey: "k"}}
cfg.LimitExemptKinds = []string{"embedding", " "}
_, err := newGateway(context.Background(), nil, cfg, "x")
require.Error(t, err)
assert.Contains(t, err.Error(), "limitExemptKinds")
}