A key with a day and a week limit has both windows start at 00:00 on a
Monday; a day and a month do on the 1st. sumsFor gave both limits one
*sums, but once it was kept from an earlier call it was appended to
the kept list once per limit, and Append adds a call to each kept
sums. From the second call of such a day each call counted twice in
the day and the week (three times on a Monday the 1st): calls, tokens
and cost, so the key was refused at half its day while limit list and
Usage, which read the file, showed the right sum (oxsean). A restart
hid it until the next call.
sumsFor now looks first in the list it is building, then in what was
kept, so each window start is kept once. Reserve's holds already took
one a window. JoeyWu-tech's 119a09f, as posted; xiedekai's repro
(calls and tokens double too) added its checks to the test.
TestKeyLimitWindowsThatStartTogetherCountACallOnce runs a day limit
of 400 tokens ($4) beside a week, a month, or both, on 2026-10-19 (a
Monday), 2026-10-01 and 2026-06-01 (a Monday the 1st), by tokens and by
cost, 100 tokens ($1) a call: four calls are let in, the fifth refused
on the day at 400, with 4 calls and 400 tokens, and budget.All (what
limit list shows) agrees with the refusal.
Red on origin/main's budget.go, all six subtests: "call 4 refused with
300 of 400 used; it counts 500", and on a Monday the 1st "call 3
refused with 200 of 400 used; it counts 400". Green with the change.
Run:
- Full suite, `go test -timeout 45m ./...` (pipefail, log kept whole),
darwin arm64, go1.26.8, at 31136c446 (86254d404 + this and the #2932
change): 68 packages ok, no FAIL.
- At the pushed head (rebased on ec7e50b82, a redact-only change):
TestKeyLimit|TestRemoteKeyLimit ok; earlier -race -count=3 ok, and
the new test under TZ=UTC, America/Los_Angeles, Pacific/Kiritimati.
- Windows 11 box (cross-compiled gateway test binary): TestKeyLimit and
TestRemoteKeyLimit PASS.
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows:
ok.
Not tried: a real gateway key through the app on a Monday; the test
drives budget.Reserve and budget.Append as the gateway does.
Co-authored-by: JoeyWu-tech <71250493+JoeyWu-tech@users.noreply.github.com>
Co-authored-by: xiedekai <285581061+xiedekai@users.noreply.github.com>