Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
1 Skipped Deployment
|
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughAdds workspace-level API rate limiting: DB/schema and model additions for quota ratelimit fields, a new ratelimit namespace service (create/get/invalidate) with caching and singleflight, quota cache wiring, keys service workspace-rate checks, refactors ratelimit route handlers to use Namespaces, and OpenAPI/error mappings for 429 responses. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Client
participant KeysSvc as KeysService
participant QuotaCache as WorkspaceQuotaCache
participant DB as Database
participant NamespaceSvc as NamespaceService
participant Ratelimiter as Ratelimiter
Client->>KeysSvc: GetRootKey(req)
KeysSvc->>QuotaCache: SWR Get(workspaceID)
alt cache hit
QuotaCache-->>KeysSvc: quota
else cache miss
QuotaCache->>DB: FindQuotaByWorkspaceID(workspaceID)
DB-->>QuotaCache: quota
QuotaCache-->>KeysSvc: quota
end
KeysSvc->>KeysSvc: checkWorkspaceRateLimit(ctx, quota)
KeysSvc->>NamespaceSvc: Get/Maybe Create(namespaceName)
NamespaceSvc->>DB: Get/Create namespace row
DB-->>NamespaceSvc: namespace
NamespaceSvc-->>KeysSvc: namespace
KeysSvc->>Ratelimiter: Ratelimit(namespace.ID, limit, duration)
alt allowed
Ratelimiter-->>KeysSvc: allowed
KeysSvc-->>Client: 200 OK
else denied
Ratelimiter-->>KeysSvc: denied
KeysSvc-->>Client: 429 Too Many Requests
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~75 minutes 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
65378db to
1a41386
Compare
b28a526 to
d8dee2e
Compare

What does this PR do?
Fixes #4507
FIxes #4850
This well adds ratelimiting workspace wide for all of our API routes using a single general limit basically rps per workspace.
in the quotas table both the limit and duration can be overriden, null being unlimited and 0 nothing and everything above well yeah.
This refactors our the ratelimit namespace creation into a seperate internal service as this is somewhat shared so we can raw-dog our own analytics in the unkey workspace that also hosts the rootkeys. It will own the analytics.
If we wanted we could also make overrides work? and just use that but the db does work nicely here.
Type of change
How should this be tested?
The tests should cover it, otherwise:
make devrun
UPDATE unkey.quota t SET t.ratelimit_api_limit = 1, t.ratelimit_api_duration = 60000 WHERE t.slug = 'ws_local'e.gCall the api multiple times
Api calls should fail with 429 once exceeded.
Checklist
Required
pnpm buildpnpm fmtmake fmton/godirectoryconsole.logsgit pull origin mainAppreciated