node.js - 限制并发请求 + 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
. 请分享你的想法。
解决方案
推荐阅读
- java - ChromeDriver Selenium 出现“java.lang.module.InvalidModuleDescriptorException:提供程序类 org.apache.bsf.BSFManager 不在模块中”错误
- javascript - 文本未定义 - JavaScript
- flutter - 参数类型“GeneratedIntColumn”不能分配给参数类型“int”:Moor-Flutter
- javascript - 通过索引而不是它们的名称获取 json 键和值
- powershell - 带有参数的 PowerShell 脚本,用于 Google Cloud Platform 上的 Windows VM 实例
- java - 深度优先搜索(堆栈还是递归?)
- mysql - 将持续时间添加到日期后,Django MySQL过滤日期记录
- r - 长到宽格式 R
- ios - 为什么不调用 searchBarTextDidEndEditing?
- angular - 等待服务 API 调用在 Angular 8 中完成