feat: add in-memory rate limiter
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
281329a1af
commit
db71f29288
@@ -0,0 +1,42 @@
|
||||
import logger from './logger.js';
|
||||
|
||||
const rateLimitMap = new Map();
|
||||
|
||||
export const checkRateLimit = (key) => {
|
||||
const now = Date.now();
|
||||
const windowMs = parseInt(process.env.RATE_LIMIT_WINDOW_MS, 10) || 60000;
|
||||
const maxRequests = parseInt(process.env.RATE_LIMIT_MAX_REQUESTS, 10) || 30;
|
||||
|
||||
if (!rateLimitMap.has(key)) {
|
||||
rateLimitMap.set(key, { count: 0, reset: now + windowMs });
|
||||
}
|
||||
|
||||
const record = rateLimitMap.get(key);
|
||||
|
||||
if (now > record.reset) {
|
||||
record.count = 0;
|
||||
record.reset = now + windowMs;
|
||||
}
|
||||
|
||||
if (record.count >= maxRequests) {
|
||||
logger.warn('Rate limit exceeded', { key, count: record.count, reset: record.reset });
|
||||
return false;
|
||||
}
|
||||
|
||||
record.count++;
|
||||
return true;
|
||||
};
|
||||
|
||||
export const cleanupRateLimitCache = () => {
|
||||
const now = Date.now();
|
||||
const windowMs = parseInt(process.env.RATE_LIMIT_WINDOW_MS, 10) || 60000;
|
||||
const keysToDelete = [];
|
||||
|
||||
for (const [key, record] of rateLimitMap.entries()) {
|
||||
if (now > record.reset) {
|
||||
keysToDelete.push(key);
|
||||
}
|
||||
}
|
||||
|
||||
keysToDelete.forEach(key => rateLimitMap.delete(key));
|
||||
};
|
||||
Reference in New Issue
Block a user