130 -> 96. forcetypeassert, predeclared and usetesting are now at zero.
Fanned out across four isolated worktrees partitioned by package so no
two workers could touch the same file: internal/tools (20 assertions),
internal/webui, internal/memory + internal/ratelimit, internal/secret.
Four stragglers outside those scopes fixed inline (internal/pairing,
internal/forge, internal/channels/telegram, internal/tools/mcp).
Unchecked type assertions became comma-ok with a t.Fatalf naming the
actual type. One site in a goroutine uses t.Errorf + return instead,
since t.Fatalf from a non-test goroutine is undefined behaviour.
predeclared renames chose names from meaning rather than adding a
suffix: max -> maxQueryLen, maxMatchLen, maxRequests, alphabetLen, and
nextBackoff's max -> ceiling. `real` -> `base` in the webui tests.
os.Setenv became t.Setenv, which restores on cleanup, so the paired
deferred Unsetenv calls went with it.
Note for future fan-outs: the agent worktrees branched from origin/main
(a7388a0), not from this branch, so every diff arrived based on a tree
without the phase 1 autofix. They were applied with `git apply --3way`
and the phase 1 changes to internal/memory/manager.go were verified to
survive — in particular the repaired `ctx, cancel =` assignment. A plain
`git apply` would have silently reverted it.
70 lines
1.7 KiB
Go
70 lines
1.7 KiB
Go
// Package ratelimit provides a sliding-window request limiter, keyed by
|
|
// platform and user, for gating inbound gateway traffic per identity
|
|
// rather than globally.
|
|
package ratelimit
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// Limiter enforces "at most max requests per window" per (platform,
|
|
// userID) pair using a sliding window log. It is safe for concurrent
|
|
// use.
|
|
type Limiter struct {
|
|
window time.Duration
|
|
max int
|
|
now func() time.Time
|
|
|
|
mu sync.Mutex
|
|
hits map[limiterKey][]time.Time
|
|
}
|
|
|
|
// limiterKey identifies one (platform, userID) budget. Using a struct
|
|
// rather than a concatenated string avoids any risk of two distinct
|
|
// pairs colliding on a chosen separator byte that turns up in
|
|
// externally-supplied platform or userID values.
|
|
type limiterKey struct {
|
|
platform string
|
|
userID string
|
|
}
|
|
|
|
// New returns a Limiter allowing at most maxRequests requests in any
|
|
// rolling window-length interval, per (platform, userID) pair. A
|
|
// maxRequests of 0 blocks every request.
|
|
func New(window time.Duration, maxRequests int) *Limiter {
|
|
return &Limiter{
|
|
window: window,
|
|
max: maxRequests,
|
|
now: time.Now,
|
|
hits: map[limiterKey][]time.Time{},
|
|
}
|
|
}
|
|
|
|
// Allow reports whether a request from userID on platform is within the
|
|
// limit, recording it if so.
|
|
func (l *Limiter) Allow(platform, userID string) bool {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
|
|
now := l.now()
|
|
key := limiterKey{platform: platform, userID: userID}
|
|
cutoff := now.Add(-l.window)
|
|
|
|
hits := l.hits[key]
|
|
live := hits[:0]
|
|
for _, h := range hits {
|
|
if h.After(cutoff) {
|
|
live = append(live, h)
|
|
}
|
|
}
|
|
|
|
if len(live) >= l.max {
|
|
l.hits[key] = live
|
|
return false
|
|
}
|
|
|
|
l.hits[key] = append(live, now)
|
|
return true
|
|
}
|