Build the rate limiter on the new CounterStore, so limits hold across server instances
Maintainer antworten meist innerhalb von 1 Tag
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 83/100
- Issue-Typ
- Feature
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- node.js, typescript
Rechercherichtung
Read packages/core/src/storage/types.ts for the CounterStore contract, then review packages/server/src/rate-limit.ts and its tests for existing behavior. Implement the algorithm in packages/core/src/limits/rate-limit.ts and add fake-clock and concurrency coverage in packages/core/tests/limits/rate-limit.test.ts, using the in-memory counter store. Done means the listed acceptance criteria pass and the core package exports the limiter with a minor changeset.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
The server's rate limiter keeps its counts in a Map in the process (packages/server/src/rate-limit.ts). On Vercel or Workers every instance has its own map, so a visitor spread over five instances gets five times the limit. It also needs a setInterval for cleanup (packages/server/src/handler.ts, line 35), and Workers don't allow timers at global scope.
@chatcops/core now has a CounterStore interface (packages/core/src/storage/types.ts): atomic counters that reset after a while, backed by Redis, Postgres and so on. This issue is the rate limit algorithm on top of it. Wiring it into the server comes in a later PR.
What to do
Create packages/core/src/limits/rate-limit.ts:
export interface RateLimitRule {
limit: number; // requests allowed per window
windowMs: number;
}
export interface RateLimitResult {
allowed: boolean;
/** Requests left in the current window, never below 0. */
remaining: number;
/** When denied: how long until a request would be allowed again. */
retryAfterMs?: number;
}
export function createRateLimiter(counters: CounterStore, rule: RateLimitRule, options?: { prefix?: string; now?: () => number }): {
check(key: string): Promise<RateLimitResult>;
};
Use a sliding window built from two fixed windows. It's smooth at window edges (no burst of 2x the limit at the boundary) and needs only two counters:
- The window index is
Math.floor(now / windowMs). Count this request withcounters.increment(${prefix}:${key}:${index}, 1, 2 * windowMs), and read the previous window's counter withget. - The estimated count is
previous * (1 - elapsedInWindow / windowMs) + current. Allow the request when it's at mostlimit. - Denied requests still count, so a client that keeps hammering stays limited.
retryAfterMsis an estimate: the time until the estimate drops back under the limit, at least 1 ms and at mostwindowMs. Explain the formula in a comment.prefixdefaults to'rl'. Validatelimit > 0andwindowMs > 0likeRateLimiterdoes.
Where to look
packages/server/src/rate-limit.ts(today's limiter and its tests inpackages/server/tests/rate-limit.test.ts)packages/core/src/storage/memory.ts(memoryStorage().counters, use it in the tests with a fakenow)- Cloudflare's write-up of the same sliding window idea
Acceptance criteria
- New
packages/core/src/limits/rate-limit.tsandpackages/core/tests/limits/rate-limit.test.ts, exported from@chatcops/core - Tests with a fake clock: the first
limitrequests pass and the next one doesn't; the count decays across the window boundary (half the previous window still counts at the midpoint); keys don't affect each other;retryAfterMsis set when denied and within bounds - A test with 30 concurrent
check()calls against a limit of 10 allows exactly 10 - Changeset for
@chatcops/core(minor) -
pnpm test:coverageandpnpm lintpass
Want to work on this? Comment below and I'll assign it to you. Please take one issue at a time, branch off dev, and open your PR against dev with Closes #<this issue number>. Setup notes (pnpm, build before test) are in CONTRIBUTING.md.
- Vorherrschende Sprache
- TypeScript
- Sterne
- 13
- Forks
- 7
- Ø Merge
- 3 Std. 33 Min.
- Gemergte PRs (30 T.)
- 176
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus codercops/chatcops
-
Add startup checks that catch weak or leaked security settingsEvtl. vergeben @varun-pahuja hat das vor 3 Tagen übernommen. Offenenhancement good first issue hacktoberfest server
codercops/chatcops#231 · 3 Kommentare · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 1 Tag
-
Type-check the TypeScript samples in the docsEvtl. vergeben @An16og hat das vor 2 Tagen übernommen. Offendocumentation hacktoberfest help wanted testing
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 70/100
codercops/chatcops#201 · 7 Kommentare · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 1 Tag
-
hacktoberfest
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 35/100
Maintainer antworten meist innerhalb von 1 Tag
-
Add built-in translations to the widgetEvtl. vergeben @piyusshhjangid hat das vor 1 Tag übernommen. Offenhelp wanted widget
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 52/100
codercops/chatcops#55 · 3 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in codercops/chatcops
Ähnliche Issues
-
level/task type/bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
wazuh/wazuh-dashboard-plugins#9508 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Testnet EOL page links Zonescan mainnetEvtl. vergeben @napelvs hat das heute übernommen. Offen
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 76/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Azure-Samples/azure-search-openai-demo#3249 ·
Maintainer antworten meist innerhalb von 1 Tag
-
[Docs] docs/architecture.mermaid leaves out 21 of the 51 src/ directoriesEvtl. vergeben @uyg7x hat das heute übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
Maintainer antworten meist innerhalb von 1 Tag
-
enhancement status:needs-triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
dongdongbh/Mindwtr#1438 ·
Maintainer antworten meist innerhalb von 1 Tag