fix(events): keep sub-100ms paced delays readable

This commit is contained in:
2026-11-25 11:52:38 +00:00
parent dd11ada41f
commit 32a6d0c51f
2 changed files with 29 additions and 1 deletions
+13 -1
View File
@@ -348,10 +348,22 @@ func (g *Gateway) admitCounted(ctx context.Context, w http.ResponseWriter, scope
// pacedEventRound keeps the event message readable (8.3s, not 8.333333333s).
const pacedEventRound = 100 * time.Millisecond
// pacedDelayText renders a delay for the event message: tenths of a second
// from 100ms up, whole milliseconds below, never "0s" for a positive delay.
func pacedDelayText(d time.Duration) string {
if d >= pacedEventRound {
return d.Round(pacedEventRound).String()
}
if d > 0 && d < time.Millisecond {
return time.Millisecond.String()
}
return d.Round(time.Millisecond).String()
}
// 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.Round(pacedEventRound).String()}
ev := gatewayEvent{Route: "pacing", Kind: eventKindPaced, Message: "request paced " + pacedDelayText(d)}
applyScopeAttribution(&ev, scopes)
if ev.User == "" || !g.limiter.lastSeen.due(pacedEventScope, ev.User, g.limiter.now()) {
return
+16
View File
@@ -427,3 +427,19 @@ func TestPacing_EventMessageRoundsDelay(t *testing.T) {
}
assert.Equal(t, []string{"request paced 8.3s"}, msgs)
}
func TestPacedDelayText(t *testing.T) {
cases := []struct {
want string
d time.Duration
}{
{"8.3s", 8333333333 * time.Nanosecond},
{"100ms", 120 * time.Millisecond},
{"37ms", 37 * time.Millisecond},
{"1ms", 400 * time.Microsecond},
{"100ms", 99600 * time.Microsecond},
}
for _, c := range cases {
assert.Equal(t, c.want, pacedDelayText(c.d), c.d.String())
}
}