-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[vitest-pool-workers] fix: reset() now clears ratelimit binding state between tests #14409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
petebacondarwin
merged 22 commits into
cloudflare:main
from
matingathani:fix/vitest-pool-workers-reset-ratelimit
Jul 6, 2026
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
6b3aeff
[vitest-pool-workers] fix: reset() now clears ratelimit binding state…
matingathani 633e556
Update .changeset/vitest-pool-workers-reset-ratelimit.md
matingathani 681bd41
address Devin review: add miniflare changeset and explain globalThis …
matingathani 8500364
chore: regenerate env.d.ts with updated wrangler types hash
matingathani 52edb7e
Merge branch 'main' into fix/vitest-pool-workers-reset-ratelimit
matingathani 438f54b
refactor(vitest-pool-workers): replace globalThis string key with Sym…
matingathani cdaecb9
refactor(vitest-pool-workers): reset ratelimit state via wrapped bind…
matingathani b449326
Merge branch 'main' into fix/vitest-pool-workers-reset-ratelimit
matingathani e2f2246
refactor(vitest-pool-workers): reset ratelimit bindings by name inste…
matingathani 40d8906
refactor(miniflare): back Ratelimit binding with a Durable Object
matingathani 59a8f09
Merge branch 'main' into fix/vitest-pool-workers-reset-ratelimit
matingathani 0cdf1da
chore: retrigger CI
matingathani 62f97a1
Merge remote-tracking branch 'fork/fix/vitest-pool-workers-reset-rate…
matingathani 5be5912
Merge branch 'main' into fix/vitest-pool-workers-reset-ratelimit
matingathani 322c480
fix(miniflare): retry local-explorer dispatchFetch on Windows ECONNRESET
matingathani d13699a
Merge remote-tracking branch 'fork/fix/vitest-pool-workers-reset-rate…
matingathani 220a54d
fix(wrangler): also ignore EPIPE in hyperdrive e2e socket teardown ha…
matingathani 60481ea
chore(vitest-pool-workers): remove stale ratelimit external entry
matingathani 48d7c06
Revert "fix(wrangler): also ignore EPIPE in hyperdrive e2e socket tea…
matingathani cf60dfb
Revert "fix(miniflare): retry local-explorer dispatchFetch on Windows…
matingathani 2af1070
fix(miniflare): bind loopback service + sticky-blobs to ratelimit DO-…
matingathani c78da86
fix(miniflare): default ratelimit period to MINUTE when omitted
matingathani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "miniflare": patch | ||
| "@cloudflare/vitest-pool-workers": patch | ||
| --- | ||
|
|
||
| `reset()` from `cloudflare:test` now resets ratelimit binding state between tests. Previously, `RATE_LIMITERS` bindings retained their in-memory bucket counts across test boundaries, causing later tests in the same file to see stale rate-limit exhaustion state. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,16 @@ | ||
| /* eslint-disable */ | ||
| // Generated by Wrangler by running `wrangler types ./reset/src/env.d.ts -c ./reset/wrangler.jsonc --no-include-runtime` (hash: 524c60174bc1732153e84c1b8699023e) | ||
| interface __BaseEnv_Env { | ||
| KV_NAMESPACE: KVNamespace; | ||
| R2_BUCKET: R2Bucket; | ||
| DATABASE: D1Database; | ||
| COUNTER: DurableObjectNamespace<import("./index").Counter>; | ||
| } | ||
| // Generated by Wrangler by running `wrangler types src/env.d.ts -c wrangler.jsonc --no-include-runtime` (hash: 6992c0652c326145f228d37275b18131) | ||
| declare namespace Cloudflare { | ||
| interface GlobalProps { | ||
| mainModule: typeof import("./index"); | ||
| durableNamespaces: "Counter"; | ||
| } | ||
| interface Env extends __BaseEnv_Env {} | ||
| interface Env { | ||
| KV_NAMESPACE: KVNamespace; | ||
| R2_BUCKET: R2Bucket; | ||
| DATABASE: D1Database; | ||
| RATE_LIMITER: RateLimit; | ||
| COUNTER: DurableObjectNamespace<import("./index").Counter>; | ||
| } | ||
| } | ||
| interface Env extends __BaseEnv_Env {} | ||
| interface Env extends Cloudflare.Env {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/miniflare/src/workers/ratelimit/ratelimit-object.worker.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Durable Object backing the emulated Ratelimit binding. Moving bucket/epoch | ||
| // state here (instead of an in-memory object behind a `wrapped` binding) | ||
| // means it's automatically torn down by `deleteAllDurableObjects()`, so | ||
| // vitest-pool-workers' `reset()` clears it for free. | ||
| import { MiniflareDurableObject, POST } from "miniflare:shared"; | ||
| import type { RouteHandler } from "miniflare:shared"; | ||
|
|
||
| interface LimitRequestBody { | ||
| key: string; | ||
| limit: number; | ||
| period: number; | ||
| } | ||
|
|
||
| interface LimitResult { | ||
| success: boolean; | ||
| } | ||
|
|
||
| export class RateLimiterObject extends MiniflareDurableObject { | ||
| #buckets = new Map<string, number>(); | ||
| #epoch = 0; | ||
|
|
||
| @POST("/limit") | ||
| limit: RouteHandler = async (req) => { | ||
| const { key, limit, period } = await req.json<LimitRequestBody>(); | ||
|
|
||
| const epoch = Math.floor(Date.now() / (period * 1000)); | ||
| if (epoch !== this.#epoch) { | ||
| this.#epoch = epoch; | ||
| this.#buckets.clear(); | ||
| } | ||
|
|
||
| const value = this.#buckets.get(key) ?? 0; | ||
| if (value >= limit) { | ||
| return Response.json({ success: false } satisfies LimitResult); | ||
| } | ||
| this.#buckets.set(key, value + 1); | ||
| return Response.json({ success: true } satisfies LimitResult); | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.