admin管理员组文章数量:1130349
rate
我正在使用 rate-limiter-flexible 应用速率限制
const redis = require('redis');
const { RateLimiterRedis } = require('rate-limiter-flexible');
这是我的代码
/ Create a Redis client
const redisClient = redis.createClient({
host: 'localhost', // Replace with your Redis server hostname
port: 6379 // Replace with your Redis server port
});
// Define rate limiting options
const rateLimitOptions = {
storeClient: redisClient,
keyPrefix: 'rate-limiter:',
points: 10, // Number of points a user can accumulate before getting rate limited
duration: 60, // Time window in seconds
blockDuration: 60 // Time in seconds for which a user will be blocked after getting rate limited
};
// Create a rate limiter instance
const rateLimiter = new RateLimiterRedis(rateLimitOptions);
// Express middleware to enforce rate limiting
const rateLimitMiddleware = async (req, res, next) => {
const ip = req.connection.remoteAddress; // Get the user's IP address
const url = req.originalUrl; // Get the URL of the request
const key = `${url}_${ip}`;// Use a combination of IP and URL as the key
try {
const rateLimiterRes = await rateLimiter.consume(key); // Pass the key and points as arguments
next(); // Call next() to proceed to the next middleware or route handler
} catch (rejRes) {
// Handle rate limit exceeded
const remainingTime = Math.ceil(rejRes.msBeforeNext / 1000);
这行给我错误 Lua redis() 命令参数必须是字符串或整数\
const rateLimiterRes = await rateLimiter.consume(key); // Pass the key and points as arguments
我传递的密钥是'/v2.0/json/login-user_::1'
回答如下:
rate
我正在使用 rate-limiter-flexible 应用速率限制
const redis = require('redis');
const { RateLimiterRedis } = require('rate-limiter-flexible');
这是我的代码
/ Create a Redis client
const redisClient = redis.createClient({
host: 'localhost', // Replace with your Redis server hostname
port: 6379 // Replace with your Redis server port
});
// Define rate limiting options
const rateLimitOptions = {
storeClient: redisClient,
keyPrefix: 'rate-limiter:',
points: 10, // Number of points a user can accumulate before getting rate limited
duration: 60, // Time window in seconds
blockDuration: 60 // Time in seconds for which a user will be blocked after getting rate limited
};
// Create a rate limiter instance
const rateLimiter = new RateLimiterRedis(rateLimitOptions);
// Express middleware to enforce rate limiting
const rateLimitMiddleware = async (req, res, next) => {
const ip = req.connection.remoteAddress; // Get the user's IP address
const url = req.originalUrl; // Get the URL of the request
const key = `${url}_${ip}`;// Use a combination of IP and URL as the key
try {
const rateLimiterRes = await rateLimiter.consume(key); // Pass the key and points as arguments
next(); // Call next() to proceed to the next middleware or route handler
} catch (rejRes) {
// Handle rate limit exceeded
const remainingTime = Math.ceil(rejRes.msBeforeNext / 1000);
这行给我错误 Lua redis() 命令参数必须是字符串或整数\
const rateLimiterRes = await rateLimiter.consume(key); // Pass the key and points as arguments
我传递的密钥是'/v2.0/json/login-user_::1'
回答如下:本文标签: rate
版权声明:本文标题:rate 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/jiaocheng/1717139106a448427.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论