forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrate-limiter.ts
More file actions
33 lines (26 loc) · 733 Bytes
/
Copy pathrate-limiter.ts
File metadata and controls
33 lines (26 loc) · 733 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Simple in-memory rate limiter
const RATE_LIMIT_WINDOW = 60 * 1000 // 1 minute
const MAX_REQUESTS = 5 // 5 requests per minute
interface RateLimit {
count: number
resetAt: number
}
const rateLimits = new Map<string, RateLimit>()
export function isRateLimited(userId: string): boolean {
const now = Date.now()
const userRateLimit = rateLimits.get(userId)
// Clean up expired rate limits
if (userRateLimit && userRateLimit.resetAt < now) {
rateLimits.delete(userId)
}
if (!rateLimits.has(userId)) {
rateLimits.set(userId, {
count: 1,
resetAt: now + RATE_LIMIT_WINDOW,
})
return false
}
const limit = rateLimits.get(userId)!
limit.count++
return limit.count > MAX_REQUESTS
}