mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
fix(events): round paced delay in event message
This commit is contained in:
@@ -345,10 +345,13 @@ func (g *Gateway) admitCounted(ctx context.Context, w http.ResponseWriter, scope
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// pacedEventRound keeps the event message readable (8.3s, not 8.333333333s).
|
||||
const pacedEventRound = 100 * time.Millisecond
|
||||
|
||||
// recordPacedEvent records at most one pacing event per user per throttle
|
||||
// window.
|
||||
func (g *Gateway) recordPacedEvent(scopes []limitScope, d time.Duration) {
|
||||
ev := gatewayEvent{Route: "pacing", Kind: eventKindPaced, Message: "request paced " + d.String()}
|
||||
ev := gatewayEvent{Route: "pacing", Kind: eventKindPaced, Message: "request paced " + d.Round(pacedEventRound).String()}
|
||||
applyScopeAttribution(&ev, scopes)
|
||||
if ev.User == "" || !g.limiter.lastSeen.due(pacedEventScope, ev.User, g.limiter.now()) {
|
||||
return
|
||||
|
||||
@@ -414,3 +414,16 @@ func TestPacing_ConcurrentPacedRequestsCappedPerUser(t *testing.T) {
|
||||
assert.LessOrEqual(t, paced.Load(), int64(maxConcurrentPaced), "requests above the cap proceed undelayed")
|
||||
assert.Greater(t, paced.Load(), int64(0))
|
||||
}
|
||||
|
||||
func TestPacing_EventMessageRoundsDelay(t *testing.T) {
|
||||
_, gw := pacingGateway(t, nil, &LimitsConfig{RequestsPerMinute: 10})
|
||||
scopes := []limitScope{{kind: "user", id: "a"}}
|
||||
gw.recordPacedEvent(scopes, 8333333333*time.Nanosecond)
|
||||
var msgs []string
|
||||
for _, ev := range gw.events.ring.snapshot(eventRingCap) {
|
||||
if ev.Kind == eventKindPaced {
|
||||
msgs = append(msgs, ev.Message)
|
||||
}
|
||||
}
|
||||
assert.Equal(t, []string{"request paced 8.3s"}, msgs)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user