mirror of
https://github.com/bitbaum/limitkit.git
synced 2026-12-04 04:27:46 +01:00
Twelve near-identical rate limiters existed across nine repos of this fleet.
orangecat alone carried four, and its ADR-0002 to unify them sat "Proposed" for
seven months while the count doubled — the measured proof that writing a plan
down is not the same as extracting the code.
What actually varied between the twelve was ONE thing: where the counts live
(eleven in process memory, one in Upstash). Everything else — window
arithmetic, refusal shape, headers, client-IP extraction — was the same idea
written twelve slightly different ways, with independently re-invented bugs.
So the algorithm is fixed and the store is the seam:
slidingWindow / fixedWindow pure over store + clock. Both shapes shipped in
the fleet, and they refuse differently; the
choice is now named at the call site instead of
implied by whichever file got copied.
Store / MemoryStore two methods. The default is BOUNDED (LRU past
maxKeys): one replaced implementation kept a
bare Map keyed by stranger IPs with no eviction
— a slow memory leak fed by the public
internet, now impossible by construction.
toHeaders / clientIp the HTTP edges everyone also rewrote. Standard
X-RateLimit-* trio, Retry-After on refusals
only — exactly what orangecat's ADR specified.
Two decisions worth naming:
A REFUSAL COUNTS NOTHING. Several replaced implementations let refused
requests extend the window, so a scripted attacker hammering a key locked
the legitimate user behind the same NAT out indefinitely. Here a hammered
key recovers the moment the attacker stops; there is a test for exactly
this shape.
retryAfterSeconds IS DERIVED, never fabricated — computed from when the
oldest counted hit ages out. "Try again shortly" on a window that opens in
an hour is the same lie ai-ration's 429 classifier exists to prevent, one
layer down.
Deliberately not included, per SHARED.md's extraction rules: no HTTP client,
no middleware, no framework types, no distributed store (the seam is two
methods; your Redis is your ops posture), and NO LIMIT VALUES — how many
attempts a login route allows is app semantics, asserted locally.
Every function takes `now` as an argument, so all of it is testable without
sleeping — which is why this package has 14 tests and the twelve files it
replaces, collectively, had almost none. Tests import the package BY NAME, so
the exports map is exercised on every run, not at the first consumer's
install.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
22 lines
1.0 KiB
Plaintext
22 lines
1.0 KiB
Plaintext
MIT License
|
|
|
|
Copyright (c) 2026 Mao Nakamoto
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|