-
Notifications
You must be signed in to change notification settings - Fork 165
add rate limits #123
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
Open
ctate
wants to merge
3
commits into
main
Choose a base branch
from
ctate/rate-limit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add rate limits #123
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { and, count, eq, gte } from "drizzle-orm"; | ||
| import { db } from "@/lib/db"; | ||
| import { workflowExecutions } from "@/lib/db/schema"; | ||
|
|
||
| type RateLimitConfig = { | ||
| maxRequests: number; | ||
| windowInHours: number; | ||
| }; | ||
|
|
||
| const RATE_LIMITS: Record<string, RateLimitConfig> = { | ||
| // AI generation: 50 requests per hour | ||
| aiGenerate: { maxRequests: 50, windowInHours: 1 }, | ||
| // Webhook execution: 1000 requests per hour | ||
| webhookExecute: { maxRequests: 1000, windowInHours: 1 }, | ||
| }; | ||
|
|
||
| type RateLimitResult = { | ||
| allowed: boolean; | ||
| remaining: number; | ||
| resetAt: Date; | ||
| }; | ||
|
|
||
| /** | ||
| * Check rate limit for workflow executions | ||
| */ | ||
| export async function checkExecutionRateLimit( | ||
| userId: string, | ||
| limitKey: "webhookExecute" = "webhookExecute" | ||
| ): Promise<RateLimitResult> { | ||
| const config = RATE_LIMITS[limitKey]; | ||
| const windowStart = new Date( | ||
| Date.now() - config.windowInHours * 60 * 60 * 1000 | ||
| ); | ||
| const resetAt = new Date(Date.now() + config.windowInHours * 60 * 60 * 1000); | ||
|
|
||
| const [result] = await db | ||
| .select({ count: count(workflowExecutions.id) }) | ||
| .from(workflowExecutions) | ||
| .where( | ||
| and( | ||
| eq(workflowExecutions.userId, userId), | ||
| gte(workflowExecutions.startedAt, windowStart) | ||
| ) | ||
| ); | ||
|
|
||
| const currentCount = result?.count ?? 0; | ||
| const remaining = Math.max(0, config.maxRequests - currentCount); | ||
|
|
||
| return { | ||
| allowed: currentCount < config.maxRequests, | ||
| remaining, | ||
| resetAt, | ||
| }; | ||
| } | ||
|
|
||
| // In-memory rate limit for AI generation (doesn't persist to DB) | ||
| const aiRequestCounts = new Map<string, { count: number; resetAt: number }>(); | ||
|
|
||
| /** | ||
| * Check rate limit for AI generation requests (in-memory) | ||
| */ | ||
| export function checkAIRateLimit( | ||
| userId: string, | ||
| limitKey: "aiGenerate" = "aiGenerate" | ||
| ): RateLimitResult { | ||
| const config = RATE_LIMITS[limitKey]; | ||
| const now = Date.now(); | ||
| const windowMs = config.windowInHours * 60 * 60 * 1000; | ||
|
|
||
| const existing = aiRequestCounts.get(userId); | ||
|
|
||
| // Reset if window has passed | ||
| if (!existing || now > existing.resetAt) { | ||
| aiRequestCounts.set(userId, { count: 1, resetAt: now + windowMs }); | ||
| return { | ||
| allowed: true, | ||
| remaining: config.maxRequests - 1, | ||
| resetAt: new Date(now + windowMs), | ||
| }; | ||
| } | ||
|
|
||
| // Check if within limit | ||
| if (existing.count >= config.maxRequests) { | ||
| return { | ||
| allowed: false, | ||
| remaining: 0, | ||
| resetAt: new Date(existing.resetAt), | ||
| }; | ||
| } | ||
|
|
||
| // Increment count | ||
| existing.count += 1; | ||
| return { | ||
| allowed: true, | ||
| remaining: config.maxRequests - existing.count, | ||
| resetAt: new Date(existing.resetAt), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Get rate limit headers for response | ||
| */ | ||
| export function getRateLimitHeaders(result: RateLimitResult): HeadersInit { | ||
| return { | ||
| "X-RateLimit-Remaining": result.remaining.toString(), | ||
| "X-RateLimit-Reset": result.resetAt.toISOString(), | ||
| }; | ||
| } | ||
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.