如何限制节点js中的异步调用量?

问题描述 投票:0回答:1

我试图将我可以对服务器进行的异步调用量限制为每秒1次调用。我看了async eachOfLimit,但不确定这是否是正确的解决方案。

我正在寻找一个已经存在的库,它将包裹请求模块并实际上将请求限制为每秒1。我只是不知道如何处理这种情况,我希望有某种标准。

node.js asynchronous throttling
1个回答
0
投票

如果你正在使用express,你可以使用npm的express-rate-limit包。用法如下 -

const rateLimit = require("express-rate-limit");


app.enable("trust proxy"); // only if you're behind a reverse proxy (Heroku, Bluemix, AWS ELB, Nginx, etc)

const limiter = rateLimit({
  windowMs: 60 * 1000, // 1 minute
  max: 1 // limit each IP to 1 requests per windowMs
});

//  apply to all requests
app.use(limiter);
© www.soinside.com 2019 - 2024. All rights reserved.