首页 > 解决方案 > 限制并发请求 + node.js

问题描述

我有一个使用 node.js 公开的 api。我一次只能限制 API 调用一次。我尝试了以下方法。

应用程序.js

globals.consumed = false;

路由.js

app.get("/route1", checkForAlreadyConsumed,controller.method);

控制器方法

globals.consumed = true;
//Business logic goes here
//After all the stuff is done
globals.consumed = false;

checkForAlreadyConsumed

checkForAlreadyConsumed() {
   if(consumed == false) {
      next();
  } else {
    retrun res.send(403);
  }
}

因此,通过上述逻辑,我试图让任何人都不应使用该 API,如果它已经在执行它的过程。But by the above approach if i do 2 concurrent calls , the second call is waiting for the first call to finish so the 403 code is sent as response to frotend. 请分享你的想法。

标签: node.jsasynchronous

解决方案


推荐阅读