Open zebeel opened 3 years ago
const rateLimit = require('express-rate-limit');
const redis = require('redis');
const RedisStore = require('rate-limit-redis');
const REDIS_CONFIG = {
PORT: 6379, // Replace with your Redis port
HOST: 'localhost', // Replace with your Redis host
};
const RATE_LIMIT = {
timerange: 15 * 60 * 1000, // 15 minutes in milliseconds
count: 100, // Max requests
message: 'Too many requests, please try again later.',
};
let apiLimiter;
// Create Redis client with error handling
const client = redis.createClient(REDIS_CONFIG.PORT, REDIS_CONFIG.HOST);
client.on('error', (err) => {
console.error('ERR:REDIS:', err);
});
client.on('connect', () => {
console.info('INFO:REDIS: Connected!');
});
// Handle Redis connection error and retry logic
client.on('reconnecting', () => {
console.info('INFO:REDIS: Reconnecting...');
});
client.on('end', () => {
console.info('INFO:REDIS: Connection closed');
});
// Create rate limiter
apiLimiter = rateLimit({
store: new RedisStore({
client: client,
passIfNotConnected: true, // This option allows to pass if the Redis server is not connected
}),
windowMs: RATE_LIMIT.timerange,
max: RATE_LIMIT.count,
keyGenerator: (req) => req.ip, // Use req.ip for rate limiting
message: { status: 'rateLimit', msg: RATE_LIMIT.message },
});
// Export apiLimiter to use in your Express app
module.exports = apiLimiter;
Here is my code
const rateLimit = require('express-rate-limit'); const redis = require('redis'); const RedisStore = require('rate-limit-redis'); let apiLimiter; const client = redis.createClient(REDIS_CONFIG.PORT, REDIS_CONFIG.HOST); client.on('error', err => console.info('ERR:REDIS:', err)); client.on('connect', () => console.info('INFO:REDIS:Connected!')); apiLimiter = rateLimit({ store: new RedisStore({ client: client, passIfNotConnected: true, }), windowMs: RATE_LIMIT.timerange, max: RATE_LIMIT.count, keyGenerator: 'mykeyhere', message: { status: 'rateLimit', msg: RATE_LIMIT.message }, });
and this is the log
I got this error on my GCP(GAE and Redis) and don't know how to resolve it. Please help.
Environment
12.x.x
5.o
macOS Big Sur 11.2