recoder-code
Version:
Complete AI-powered development platform with ML model training, plugin registry, real-time collaboration, monitoring, infrastructure automation, and enterprise deployment capabilities
35 lines • 1.2 kB
JavaScript
/**
* Rate Limiting Service
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.RateLimitService = void 0;
const redis_1 = require("../redis");
class RateLimitService {
constructor() {
this.redis = (0, redis_1.getRedis)();
}
async checkRateLimit(key, limit, window) {
const current = await this.redis.get(key);
if (!current) {
await this.redis.setex(key, window, '1');
return { allowed: true, remaining: limit - 1 };
}
const count = parseInt(current);
if (count >= limit) {
return { allowed: false, remaining: 0 };
}
await this.redis.incr(key);
return { allowed: true, remaining: limit - count - 1 };
}
async getUserRateLimit(userId) {
const key = `rate_limit:user:${userId}`;
return this.checkRateLimit(key, 1000, 3600); // 1000 requests per hour
}
async getApiKeyRateLimit(apiKey) {
const key = `rate_limit:api_key:${apiKey}`;
return this.checkRateLimit(key, 5000, 3600); // 5000 requests per hour
}
}
exports.RateLimitService = RateLimitService;
//# sourceMappingURL=RateLimitService.js.map
;