大规模请求数据并发问题

1.请求队列

javascript 复制代码
const queue = [];

queue.push();
queue.shift;

class RequestQueue {
    constructor(maxConcurrent) {
        this.maxConcurrent = maxConcurrent; // 最大并发请求数
        this.currentConcurrent = 0; // 当前并发请求数
        this.queue = []; // 请求队列
    add(request) {
        return new Promise(resolve,reject) => {
            this.queue.push({ request, resolve, reject });
            this.processQuese();
        });
}

processQuese() {
    if (this.queue.length > 0 && this.currentConcurrent  < maxConcurrent) {
        const { request, resolve, reject } = this.queue.shift();
        this.currentConcurrent++;
        request() 
            .then(resolve)
            .catch(reject)
            .finaly(() => {
                this.currentConcurrent--;
                 this.processQuese();
            });
    }
   }
}

// 实力请求函数
function fetchData(url) {
    return fetch(url).then(respomse => response.json());
}

// 使用请求队列
constrequestQuese = new RequestQueue(5); // 设定并发请求数为5

const urls = [ 
                "https://api.example.com/data1",
                 "https://api.example.com/data2",
            };

const requests = urls.map(url => () => fetchData((url));
Promise.all(requests.map(request => requestQueue.add(request)))
    .then(results => {
        console.log("所有请求完成", result);
    })
    .catch(error => {
        console.error("请求失败", error);
    });

2.防抖/节流

-防抖(debounce)

-节流 (throttle)

-防抖 (Debounce) : 确保在指定时间内函数只执行一次,常用于输入框的搜索建议

-节流 (Throttle) : 确保在指定时间间隔内函数执行一次,常用于窗口的 resize scroll 事件

javascript 复制代码
function debounce(func, wait) {
    let timeout;
return function(...args) {
    clearTimeout(timeout);
    timeout = setTimeout(() => {
        func.apply(this
相关推荐
超级新零售13 分钟前
零售 SaaS 与本地部署,技术责任边界究竟怎么分?
数据库·零售
考虑考虑31 分钟前
瀚高数据库
数据库·后端·postgresql
Fuyo_11191 小时前
C++ AVL树保姆级详解:从平衡因子到四大旋转机制
java·c++·算法
dishugj1 小时前
oracle日志切换三条命令区别
数据库
锋行天下1 小时前
DeepSeek写Langgraph就是个大傻逼,一本正经胡说八道
前端·人工智能·后端
Patrick在香港1 小时前
一个斜杠,把毫秒变成十几秒
android·数据库·python·性能优化·正则表达式·回溯·redos
Sarvartha1 小时前
Date、LocalDate、LocalTime、LocalDateTime、DateTimeFormatter类
java
jktantan1 小时前
每个节点 41 字节:把一棵百万节点树拆进 14 个 typed array
前端·javascript·vue.js·算法·typescript
悟道书生1 小时前
(一)Navicate为什么转存结构和数据只转存表而不包含建库?
数据库·oracle
阿俊-全栈开发2 小时前
LikeShop 多商户 Java 版商城微服务模块拆分与部署架构演进实践
java·微服务·架构·likeshop开源商城·多商户源码