fix(events): round paced delay in event message

This commit is contained in:
2026-11-25 11:52:37 +00:00
parent 55f1e743fe
commit e0e5989326
2 changed files with 17 additions and 1 deletions
+4 -1
View File
@@ -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
+13
View File
@@ -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)
}