当前位置:网站首页>并发优化请求
并发优化请求
2022-04-23 05:57:00 【zjLOVEcyj】
// 并发优化10个请求,同时最多只能执行3个
// 输入:promise数组,limit参数
// 存储 reqPool - 并发池
// 思路:塞入 + 执行
function qpsLimit (requestPipe, limitMax=3) {
let reqPool = []
// 往并发池里塞入请求
const add = () => {
let req = requestPipe.shift()
reqPool.push(req)
}
// 执行实际请求
const run = () => {
if (requestPipe.length === 0) return
let finsh = Promise.race(reqPool)
finsh.then(res => {
let done = reqPool.indexOf(finsh)
reqPool.splice(done, 1)
add()
})
run()
}
while (reqPool.length < limitMax) {
add()
}
run()
}
版权声明
本文为[zjLOVEcyj]所创,转载请带上原文链接,感谢
https://blog.csdn.net/cyj5201314/article/details/124035188
边栏推荐
猜你喜欢
随机推荐
【Markdown笔记】
WebSocket(基础)
C# 监听WMI事件
EF CORE在ASP.NET CORE项目中基于数据库优先模式生成实体模型
Color string conversion
Promise(一)
启用AHCI后无法启动系统
Navicat 连接 oracle library is not loaded的解决方法
ASP.NET CORE 配置选项(上篇)
若依框架从零开始
.Net Core3.1 使用 RazorEngine.NetCore 制作实体生成器 (MVC网页版)
C# Dapper 基本使用 增删改查事务等
写一个正则
Devexpress Gridview 添加全选列
查漏补缺(三)
Makefile foundation, common functions and general makefile
leetcode刷题之整数加一
Node模版引擎(ejs, art-template)
FOC single resistance sampling position loop control servo motor
1-2 JSX语法规则