Build the rate limiter on the new CounterStore, so limits hold across server instances
I maintainer di solito rispondono entro 1 giorno
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 83/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- node.js, typescript
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- TypeScript
- Stelle
- 13
- Fork
- 7
- Merge medio
- 3h 41m
- PR unite (30g)
- 155
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di codercops/chatcops
-
Document the pre-chat formForse già presa @masyarakatai l’ha presa 6 giorni fa. Apertadocumentation good first issue hacktoberfest widget
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
codercops/chatcops#70 · 5 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
docs: update the providers page for openai-compatible, registerProvider and provider importsForse già presa @prakashNalla l’ha presa 1 giorno fa. Apertadocumentation good first issue hacktoberfest website
Difficoltà 3/5 1-2 giorni Idoneità per principianti 76/100
codercops/chatcops#291 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
docs: add a protocol reference page for custom clientsForse già presa @kachamsiddarth l’ha presa 1 giorno fa. Apertadocumentation good first issue hacktoberfest website
codercops/chatcops#286 · 2 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
Create @chatcops/store-cloudflare (D1, KV and a Durable Object counter)Forse già presa @bhushan4work l’ha presa 1 giorno fa. Apertaenhancement hacktoberfest help wanted server
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
codercops/chatcops#249 · 2 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
Create @chatcops/store-sqlite, a store for single-server setupsForse già presa @srikar-naidu l’ha presa 1 giorno fa. Apertaenhancement hacktoberfest help wanted server
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
codercops/chatcops#247 · 3 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di codercops/chatcops
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 76/100
logto-io/logto#9837 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 76/100
lingdojo/kana-dojo#32964 · 1 commento · 5 reazioni ·
I maintainer di solito rispondono entro 1 giorno
-
Documentation status: needs triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
mastra-ai/mastra#28194 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
priority-low refactor-3
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 3 giorni