rate-limiter-flexible package can be used for that. (可以使用rate-limiter-flexible软件包。)
It supports many stores like Redis, MongoDB, etc. Here is the most simple example with in memory storage: (它支持Redis,MongoDB等许多存储。这是内存存储中最简单的示例:)
const opts = {
points: 10, // 10 points
duration: 1, // Per second
};
const rateLimiter = new RateLimiterMemory(opts);
rateLimiter.consume(remoteAddress, 1) // consume 1 point
.then((rateLimiterRes) => {
// 1 point consumed
// Some app logic here
})
.catch((rateLimiterRes) => {
// Not enough points to consume
});
More examples on Wiki (Wiki上的更多示例)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…