From cbffcb40618d4d9406f726e37f4ec5992cb54dc3 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Mon, 2 Nov 2026 23:57:07 +0000 Subject: [PATCH] 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. --- concurrency.go | 3 +++ concurrency_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 concurrency_test.go diff --git a/concurrency.go b/concurrency.go index d6c5090..eecf116 100644 --- a/concurrency.go +++ b/concurrency.go @@ -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 } diff --git a/concurrency_test.go b/concurrency_test.go new file mode 100644 index 0000000..a79ae7f --- /dev/null +++ b/concurrency_test.go @@ -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) + } + }) + } +}