forked from JawherKl/user-caching-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrateLimiter.js
More file actions
56 lines (45 loc) · 1.75 KB
/
rateLimiter.js
File metadata and controls
56 lines (45 loc) · 1.75 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const redis = require('redis');
const redisClient = redis.createClient({
host: '172.28.0.2',
port: 6379
});
// Properly connect Redis client
redisClient.connect().catch(console.error);
// Connect Redis client
redisClient.on('connect', () => {
console.log('Connected to Redis');
});
// Error handling for Redis
redisClient.on('error', (err) => {
console.error('Redis error:', err);
});
// Rate limiting middleware
const rateLimit = (limit, windowInSeconds) => {
return async (req, res, next) => {
const clientIP = req.headers['x-forwarded-for']
? req.headers['x-forwarded-for'].split(',')[0] // First IP in X-Forwarded-For is the real client IP
: req.connection.remoteAddress;
const key = `rate-limit:${clientIP}`;
try {
const requestCount = await redisClient.incr(key);
if (requestCount === 1) {
// Set expiration only on first request
await redisClient.expire(key, windowInSeconds);
console.log(`Key ${key} set with ${windowInSeconds} seconds expiration.`);
}
// Log current request count and TTL for debugging
const ttl = await redisClient.ttl(key);
console.log(`Key ${key} has ${requestCount} requests. TTL: ${ttl}`);
if (requestCount > limit) {
return res.status(429).json({
message: 'Rate limit exceeded. Try again later.',
});
}
next(); // Allow the request to continue if under the limit
} catch (error) {
console.error('Error in rate limiting:', error);
return res.status(500).json({ message: 'Internal server error' });
}
};
};
module.exports = rateLimit;