Skip to content

Prototype: configure rate limiter with component init args - #87

Draft
reeceyang wants to merge 1 commit into
mainfrom
reece/component-init-prototype
Draft

reeceyang wants to merge 1 commit into
mainfrom
reece/component-init-prototype

Conversation

@reeceyang

@reeceyang reeceyang commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Moves named rate limit definitions into the component's install args, app.use(rateLimiter, { args: { limits } }). The init stores one limits document per name, upserted through the name index and compared before writing so an unchanged push writes nothing, and deletes names that are no longer passed. Keying by name keeps each check's read dependency on just its own definition, so a push that changes one limit doesn't conflict with checks on the others, and it exercises an index read on the first push, in the same transaction that creates the table (convex/convex#61446). rateLimit, checkRateLimit, creditRateLimit, getValue and enqueueUpdates now take config as optional and read the stored definition by name when it's absent. Inline configs stay, since ad-hoc and dynamically named limits aren't per-installation config, and they still take precedence. Enqueued updates resolve their definition at enqueue time, so the worker is unchanged. The client still takes the same limits object for name types and to pick the transactional or asynchronous path, but no longer sends it per call.

When a later push changes a definition, future checks use it right away. Existing rateLimits state documents are left alone, even if the kind changes: the next check reinterprets the stored value and timestamp under the new definition, clamped to the new capacity, so it can't grant more than one capacity's worth. A token bucket that becomes a fixed window takes its last update time as its window start, so an explicit start isn't honored for keys that already have state until they're reset. Resetting on a kind change would mean deleting a document per key, which doesn't fit in the init. The init reads each stored definition twice (the upsert lookup and the scan for removed names), so about 50 named limits fit within the 100-document and 16 KiB limits. Larger sets aren't handled here.

The convex-test preview moves time forward a millisecond per commit, so a few existing tests compare values with toBeCloseTo instead of exact equality.

Not meant to merge. Depends on convex/convex#62236, convex/convex#62237 and convex/convex-test#212.

🤖 Generated with Claude Code

@reeceyang
reeceyang force-pushed the reece/component-init-prototype branch from db98ff8 to 013eb2c Compare November 20, 2026 02:20
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@reeceyang
reeceyang force-pushed the reece/component-init-prototype branch from 013eb2c to 6e449ff Compare November 20, 2026 20:09
@pkg-pr-new

pkg-pr-new Bot commented Nov 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@convex-dev/rate-limiter@87

commit: 6e449ff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant