-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(webapp): share rate limit bucket across additional API keys per environment #4508
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| area: webapp | ||
| type: fix | ||
| --- | ||
|
|
||
| API rate limits now apply per environment, so creating extra API keys no longer increases how many requests an environment can make. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| import { tryCatch } from "@trigger.dev/core/v3"; | ||
| import { trail } from "agentcrumbs"; // @crumbs | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Remove the Agentcrumbs instrumentation before merge. Remove the Also applies to: 11-11, 44-48 Source: Coding guidelines |
||
| import { env } from "~/env.server"; | ||
| import { resolvePrivateApiKeyRateLimitScope } from "~/models/runtimeEnvironment.server"; | ||
| import { batchStreamGrants } from "~/runEngine/concerns/batchStreamGrantsInstance.server"; | ||
| import { authenticateAuthorizationHeader } from "./apiAuth.server"; | ||
| import { authorizationRateLimitMiddleware } from "./authorizationRateLimitMiddleware.server"; | ||
| import type { Duration } from "./rateLimiter.server"; | ||
|
|
||
| const BATCH_STREAM_ITEMS_PATH = /^\/api\/v3\/batches\/([^/]+)\/items$/; | ||
| const crumb = trail("webapp"); // @crumbs | ||
|
|
||
| export const apiRateLimiter = authorizationRateLimitMiddleware({ | ||
| redis: { | ||
|
|
@@ -29,6 +32,27 @@ export const apiRateLimiter = authorizationRateLimitMiddleware({ | |
| maxItems: 1000, | ||
| }, | ||
| limiterConfigOverride: async (authorizationValue) => { | ||
| const rawApiKey = authorizationValue.replace(/^Bearer /, ""); | ||
|
|
||
| if (rawApiKey.startsWith("tr_")) { | ||
| const scope = await resolvePrivateApiKeyRateLimitScope(rawApiKey); | ||
|
|
||
| if (!scope) { | ||
| return; | ||
| } | ||
|
|
||
| // #region @crumbs | ||
| crumb("resolved private API key rate limit scope", { | ||
| environmentId: scope.environmentId, | ||
| }); | ||
| // #endregion @crumbs | ||
|
|
||
| return { | ||
| config: scope.apiRateLimiterConfig, | ||
| identifier: scope.environmentId, | ||
| }; | ||
| } | ||
|
|
||
| const authenticatedEnv = await authenticateAuthorizationHeader(authorizationValue, { | ||
| allowPublicKey: true, | ||
| allowJWT: true, | ||
|
|
@@ -40,13 +64,19 @@ export const apiRateLimiter = authorizationRateLimitMiddleware({ | |
|
|
||
| if (authenticatedEnv.type === "PUBLIC_JWT") { | ||
| return { | ||
| type: "fixedWindow", | ||
| window: env.API_RATE_LIMIT_JWT_WINDOW, | ||
| tokens: env.API_RATE_LIMIT_JWT_TOKENS, | ||
| config: { | ||
| type: "fixedWindow", | ||
| window: env.API_RATE_LIMIT_JWT_WINDOW, | ||
| tokens: env.API_RATE_LIMIT_JWT_TOKENS, | ||
| }, | ||
| }; | ||
| } else { | ||
| return authenticatedEnv.environment.organization.apiRateLimiterConfig; | ||
| } | ||
|
|
||
| return { | ||
| config: authenticatedEnv.environment.organization.apiRateLimiterConfig, | ||
| // Public keys are browser-distributed, so keep them on per-key buckets. | ||
| identifier: authenticatedEnv.type === "PRIVATE" ? authenticatedEnv.environment.id : undefined, | ||
| }; | ||
|
carderne marked this conversation as resolved.
carderne marked this conversation as resolved.
|
||
| }, | ||
| pathMatchers: [/^\/api/], | ||
| // Allow /api/v1/tasks/:id/callback/:secret | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.