perf: roll the warning limit over any minute and deep-copy samples at the ring boundary
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+127
−18
internal/perf/perf.go+55 −18internal/perf/perf_test.go+72 −0
CI failed — run details
diff --git a/internal/perf/perf.go b/internal/perf/perf.go| index eeeca55ac..d9e578cdc 100644 |
| --- a/internal/perf/perf.go |
| +++ b/internal/perf/perf.go |
| @@ -67,13 +67,15 @@ type Recorder struct { |
| // request owns, since the process started. |
| background map[string]int64 |
| - // Over-budget logging is throttled twice: once a minute per screen, |
| - // and 120 lines a minute in all. A slow dependency makes every page |
| - // slow at once, and the log is where someone goes to find out why, |
| - // so it must not be the thing that drowns. |
| - logWindow time.Time |
| - logCount int |
| - lastLog map[string]time.Time |
| + // Over-budget logging is throttled twice, both on a rolling minute: |
| + // a route logs at most once in any 60 seconds (lastLog holds when it |
| + // last did), and no 60 seconds carry more than 120 lines in all |
| + // (logged holds when the last 120 were written, oldest first). A slow |
| + // dependency makes every page slow at once, and the log is where |
| + // someone goes to find out why, so it must not be the thing that |
| + // drowns. |
| + logged []time.Time |
| + lastLog map[string]time.Time |
| } |
| // Snapshot returns the samples held, oldest first. |
| @@ -83,9 +85,35 @@ func (c *Recorder) Snapshot() []Sample { |
| out := make([]Sample, 0, len(c.samples)) |
| if len(c.samples) == Capacity { |
| out = append(out, c.samples[c.next:]...) |
| - return append(out, c.samples[:c.next]...) |
| + out = append(out, c.samples[:c.next]...) |
| + } else { |
| + out = append(out, c.samples...) |
| + } |
| + // The maps are copied so a caller cannot alter later snapshots or |
| + // the groups, and so a reader never races a writer of the ring. |
| + for i := range out { |
| + out[i] = cloneSample(out[i]) |
| + } |
| + return out |
| +} |
| + |
| +// cloneSample returns s with its own Spans and Calls maps. |
| +func cloneSample(s Sample) Sample { |
| + if s.Spans != nil { |
| + spans := make(map[string]float64, len(s.Spans)) |
| + for k, v := range s.Spans { |
| + spans[k] = v |
| + } |
| + s.Spans = spans |
| + } |
| + if s.Calls != nil { |
| + calls := make(map[string]int, len(s.Calls)) |
| + for k, v := range s.Calls { |
| + calls[k] = v |
| + } |
| + s.Calls = calls |
| } |
| - return append(out, c.samples...) |
| + return s |
| } |
| // Groups summarises the samples held; see the package function. |
| @@ -170,25 +198,34 @@ func percentile(values []float64, p int) float64 { |
| return values[(len(values)*p+99)/100-1] |
| } |
| +// maxLogsPerMinute is the most over-budget warnings any 60 seconds carry. |
| +const maxLogsPerMinute = 120 |
| + |
| // add records s in the ring and, if it is over budget and the throttle |
| -// allows, logs it to log (nil logs nothing). |
| +// allows, logs it to log (nil logs nothing). The ring keeps its own copy |
| +// of the sample's maps, so the caller may keep writing to them. |
| func (c *Recorder) add(s Sample, log *slog.Logger) { |
| + stored := cloneSample(s) |
| c.mu.Lock() |
| if len(c.samples) < Capacity { |
| - c.samples = append(c.samples, s) |
| + c.samples = append(c.samples, stored) |
| } else { |
| - c.samples[c.next] = s |
| + c.samples[c.next] = stored |
| } |
| c.next = (c.next + 1) % Capacity |
| key := s.Method + "\x00" + s.Screen |
| - emit := s.Over && (c.lastLog == nil || s.At.Sub(c.lastLog[key]) >= time.Minute) |
| - if s.At.Sub(c.logWindow) >= time.Minute { |
| - c.logWindow = s.At |
| - c.logCount = 0 |
| + emit := s.Over |
| + if last, ok := c.lastLog[key]; ok && s.At.Sub(last) < time.Minute { |
| + emit = false |
| + } |
| + if emit && len(c.logged) >= maxLogsPerMinute && s.At.Sub(c.logged[len(c.logged)-maxLogsPerMinute]) < time.Minute { |
| + emit = false |
| } |
| - emit = emit && c.logCount < 120 |
| if emit { |
| - c.logCount++ |
| + c.logged = append(c.logged, s.At) |
| + if len(c.logged) >= 2*maxLogsPerMinute { |
| + c.logged = append(c.logged[:0], c.logged[len(c.logged)-maxLogsPerMinute:]...) |
| + } |
| if len(c.lastLog) >= Capacity { |
| for k, at := range c.lastLog { |
| if s.At.Sub(at) >= time.Minute { |
diff --git a/internal/perf/perf_test.go b/internal/perf/perf_test.go| index abb6e21be..0cf01efbb 100644 |
| --- a/internal/perf/perf_test.go |
| +++ b/internal/perf/perf_test.go |
| @@ -127,6 +127,78 @@ func TestOverBudgetLogIsThrottled(t *testing.T) { |
| } |
| } |
| +// The aggregate limit is a rolling minute, not a window that resets: a |
| +// burst at the end of one minute and another at the start of the next |
| +// must not both get through. |
| +func TestOverBudgetLogBoundaryBurst(t *testing.T) { |
| + h := &countHandler{} |
| + log := slog.New(h) |
| + var r Recorder |
| + t0 := time.Date(2026, 10, 25, 12, 0, 0, 0, time.UTC) |
| + // An under-budget sample starts the clock. |
| + r.add(Sample{At: t0, Screen: "GET /ok", Method: "GET"}, log) |
| + burst := func(at time.Time, prefix string) int { |
| + before := h.count() |
| + for i := 0; i < 120; i++ { |
| + r.add(Sample{At: at, Screen: fmt.Sprintf("GET /%s%d", prefix, i), Method: "GET", Over: true}, log) |
| + } |
| + return h.count() - before |
| + } |
| + if got := burst(t0.Add(59*time.Second), "a"); got != 120 { |
| + t.Fatalf("first burst logged %d, want 120", got) |
| + } |
| + if got := burst(t0.Add(60*time.Second), "b"); got != 0 { |
| + t.Fatalf("second burst, 1s later, logged %d, want 0 (no 60s span may carry more than 120)", got) |
| + } |
| + // The allowance comes back as the first burst ages out. |
| + if got := burst(t0.Add(119*time.Second), "c"); got != 120 { |
| + t.Fatalf("burst a minute after the first logged %d, want 120", got) |
| + } |
| +} |
| + |
| +// A route's own once-a-minute limit is measured from its last logged |
| +// warning, so it rolls too. |
| +func TestOverBudgetLogPerRouteRolls(t *testing.T) { |
| + h := &countHandler{} |
| + log := slog.New(h) |
| + var r Recorder |
| + t0 := time.Date(2026, 10, 25, 12, 0, 0, 0, time.UTC) |
| + for _, d := range []time.Duration{0, 59 * time.Second, 60 * time.Second, 119 * time.Second, 120 * time.Second} { |
| + r.add(Sample{At: t0.Add(d), Screen: "GET /x", Method: "GET", Over: true}, log) |
| + } |
| + if got := h.count(); got != 3 { |
| + t.Fatalf("records = %d, want 3 (at 0s, 60s, 120s)", got) |
| + } |
| +} |
| + |
| +func TestSnapshotIsADeepCopy(t *testing.T) { |
| + var r Recorder |
| + in := Sample{Screen: "GET /x", Method: "GET", Spans: map[string]float64{"db": 1}, Calls: map[string]int{"s3": 2}} |
| + r.add(in, nil) |
| + |
| + // The caller's own maps are not shared with the ring either. |
| + in.Spans["db"] = 100 |
| + in.Calls["s3"] = 100 |
| + |
| + snap := r.Snapshot() |
| + if snap[0].Spans["db"] != 1 || snap[0].Calls["s3"] != 2 { |
| + t.Fatalf("add shared the caller's maps: %+v", snap[0]) |
| + } |
| + snap[0].Spans["db"] = 500 |
| + snap[0].Spans["new"] = 1 |
| + snap[0].Calls["s3"] = 500 |
| + snap[0].Calls["new"] = 1 |
| + |
| + again := r.Snapshot() |
| + if len(again[0].Spans) != 1 || again[0].Spans["db"] != 1 || len(again[0].Calls) != 1 || again[0].Calls["s3"] != 2 { |
| + t.Fatalf("mutating a snapshot changed the next one: %+v", again[0]) |
| + } |
| + g := r.Groups() |
| + if len(g) != 1 || g[0].Calls["s3"] != 2 || len(g[0].Calls) != 1 { |
| + t.Fatalf("mutating a snapshot changed Groups: %+v", g) |
| + } |
| +} |
| + |
| func TestUnderBudgetNeverLogs(t *testing.T) { |
| h := &countHandler{} |
| var r Recorder |