mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
Two callers that sent different anthropic-beta values shared one cache entry, although the gateway forwards that header upstream. The key now includes the normalised beta set when the header is present. Requests without the header keep their existing key.
123 lines
4.0 KiB
Go
123 lines
4.0 KiB
Go
package traefikllmgateway
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
const betaCacheResp = `{"id":"msg_1","type":"message","role":"assistant","content":[{"type":"text","text":"hi"}],"model":"claude-real","stop_reason":"end_turn","usage":{"input_tokens":1,"output_tokens":1}}`
|
|
|
|
type betaCacheFixture struct {
|
|
gw *Gateway
|
|
betas []string
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func newBetaCacheFixture(t *testing.T, strip bool) *betaCacheFixture {
|
|
t.Helper()
|
|
f := &betaCacheFixture{}
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
f.mu.Lock()
|
|
f.betas = append(f.betas, r.Header.Get("anthropic-beta"))
|
|
f.mu.Unlock()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(betaCacheResp))
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
redisLn := newBehavioralRedisServer(t)
|
|
|
|
cfg := CreateConfig()
|
|
cfg.Providers = map[string]*ProviderConfig{
|
|
"anthropic": {Type: providerTypeAnthropic, BaseURL: srv.URL, APIKey: "k", Models: []string{"claude-test"}},
|
|
}
|
|
cfg.Groups = map[string]*GroupConfig{"default": {}}
|
|
cfg.Users = &UsersConfig{Inline: []*UserConfig{{Name: "alice", Group: "default", APIKey: "sk-alice", Limits: &LimitsConfig{}}}}
|
|
cfg.Redis = &RedisConfig{Address: redisLn.Addr().String()}
|
|
cfg.Cache = CacheConfig{Enabled: true, TTL: "1m"}
|
|
cfg.StripClientAnthropicBeta = strip
|
|
h, err := New(context.Background(), http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}), cfg, "llmgw")
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
gw, ok := h.(*Gateway)
|
|
if !ok {
|
|
t.Fatal("handler is not *Gateway")
|
|
}
|
|
f.gw = gw
|
|
return f
|
|
}
|
|
|
|
// send posts the fixed messages body with the given beta header values and returns X-Llmgw-Cache.
|
|
func (f *betaCacheFixture) send(t *testing.T, betas ...string) string {
|
|
t.Helper()
|
|
body := map[string]any{"model": "claude-test", "max_tokens": 10, "messages": []any{map[string]any{"role": "user", "content": "hello"}}}
|
|
req := newMessagesRequest(t, body, "x-api-key", "sk-alice")
|
|
for _, b := range betas {
|
|
req.Header.Add("anthropic-beta", b)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
f.gw.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
return rec.Header().Get("X-Llmgw-Cache")
|
|
}
|
|
|
|
func (f *betaCacheFixture) upstreamCalls() int {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return len(f.betas)
|
|
}
|
|
|
|
func TestMessagesCache_BetaHeaderDoesNotServeHeaderlessCaller(t *testing.T) {
|
|
f := newBetaCacheFixture(t, false)
|
|
f.send(t, "context-1m")
|
|
if got := f.send(t); got == "hit" {
|
|
t.Fatal("a caller without anthropic-beta was served the entry cached for a beta caller")
|
|
}
|
|
if f.upstreamCalls() != 2 || f.betas[1] != "" {
|
|
t.Fatalf("upstream betas = %q, want the headerless request to reach upstream with no beta", f.betas)
|
|
}
|
|
}
|
|
|
|
func TestMessagesCache_DifferentBetaValuesDoNotShare(t *testing.T) {
|
|
f := newBetaCacheFixture(t, false)
|
|
f.send(t, "a")
|
|
if got := f.send(t, "b"); got == "hit" {
|
|
t.Fatal("different beta sets shared a cache entry")
|
|
}
|
|
}
|
|
|
|
func TestMessagesCache_SameBetaSetSharesRegardlessOfOrder(t *testing.T) {
|
|
f := newBetaCacheFixture(t, false)
|
|
f.send(t, "a,b")
|
|
if got := f.send(t, "b, A"); got != "hit" {
|
|
t.Fatalf("X-Llmgw-Cache = %q, want hit for the same beta set in another order", got)
|
|
}
|
|
}
|
|
|
|
func TestMessagesCache_HeaderlessKeyUnchanged(t *testing.T) {
|
|
if got := normalizeBetaSet(nil); got != "" {
|
|
t.Fatalf("normalizeBetaSet(nil) = %q, want empty so the key stays cacheKey(...)", got)
|
|
}
|
|
if got := normalizeBetaSet([]string{" , "}); got != "" {
|
|
t.Fatalf("normalizeBetaSet(blank) = %q, want empty", got)
|
|
}
|
|
f := newBetaCacheFixture(t, false)
|
|
f.send(t)
|
|
if got := f.send(t); got != "hit" {
|
|
t.Fatalf("X-Llmgw-Cache = %q, want hit for repeated headerless requests", got)
|
|
}
|
|
}
|
|
|
|
func TestMessagesCache_StripClientBetaIgnoresHeader(t *testing.T) {
|
|
f := newBetaCacheFixture(t, true)
|
|
f.send(t, "context-1m")
|
|
if got := f.send(t); got != "hit" {
|
|
t.Fatalf("X-Llmgw-Cache = %q, want hit: with stripClientAnthropicBeta the header is not forwarded", got)
|
|
}
|
|
}
|