fix(concurrency): clamp learned limit below the ceiling

An overload seen with many requests in flight set the learned limit above the ceiling. The learner now clamps the value to one below the ceiling.
This commit is contained in:
2026-11-02 23:57:07 +00:00
parent 36ad65c96e
commit cbffcb4061
2 changed files with 39 additions and 0 deletions
+3
View File
@@ -95,6 +95,9 @@ func (c *concurrencyLearner) observe(provider string, resp *http.Response, err e
if next < 1 {
next = 1
}
if next > learnedCeiling-1 {
next = learnedCeiling - 1
}
if s.learned == 0 || next < s.learned {
s.learned = next
}
+36
View File
@@ -0,0 +1,36 @@
package traefikllmgateway
import (
"net/http"
"testing"
)
// TestConcurrencyLearner_OverloadStaysBelowCeiling pins that an overload
// observation on an unknown limit never learns a value at or above the
// ceiling, however many requests were in flight.
func TestConcurrencyLearner_OverloadStaysBelowCeiling(t *testing.T) {
t.Parallel()
tests := []struct {
name string
inFlight int
}{
{name: "one in flight", inFlight: 1},
{name: "typical load", inFlight: 8},
{name: "exactly at ceiling", inFlight: 2 * learnedCeiling},
{name: "far above ceiling", inFlight: 1000},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
c := newConcurrencyLearner(0)
for range tt.inFlight {
_ = c.begin("p")
}
c.observe("p", &http.Response{StatusCode: http.StatusTooManyRequests}, nil)
got := c.limit("p")
if got < 1 || got > learnedCeiling-1 {
t.Errorf("limit = %d, want in [1, %d]", got, learnedCeiling-1)
}
})
}
}