简体   繁体   English

如何限制 API http 请求

[英]How to throttle API http request

I am currently building an endpoint to process a large amount of data.我目前正在构建一个端点来处理大量数据。 However I am running into an error over running out of resources.但是,由于资源不足,我遇到了错误。 I'm pretty sure the issue is the fact that I am making too many request to the server without limiting it.我很确定问题是我向服务器发出了太多请求而没有限制它。 However every attempt to throttle it has been unsuccessful.然而,每一次试图扼杀它的尝试都没有成功。 So I'm looking at how I can throttle my asynchronous endpoint and avoid this issue.所以我正在研究如何限制我的异步端点并避免这个问题。

  • SPECIFIC ERROR net::ERR_INSUFFICIENT_RESOURCES特定错误 net::ERR_INSUFFICIENT_RESOURCES

Code代码

 const sendStepData = async() =>{
    try{

        const body = {a,b,c,d,e,f}

        assignToBody(body)

        const response = await fetch('http://localhost:5000/stepdata',{
                method:"POST",
                headers: {"Content-Type": "application/json"},
                body: JSON.stringify(body)
    })}catch(err){
        console.log(err.message)
    }
    
}

Even after adding a SetTimeOut I am unable to throttle the endpoint.即使添加了 SetTimeOut,我也无法限制端点。

Code with timeout超时代码

 const sendStepData = async() =>{
    try{

        const body = {a,b,c,d,e,f}

        assignToBody(body)

        const response = await fetch('http://localhost:5000/stepdata',{
                method:"POST",
                headers: {"Content-Type": "application/json"},
                body: JSON.stringify(body)
    })}catch(err){
        console.log(err.message)
    }
    
}

const delayQue = () =>{
    setTimeout(sendStepData,5000)
}   

One thing to clarify as well that may be helpful.还要澄清一件事,这可能会有所帮助。 This endpoint is in a separate file that I call after I have prepped my data to be passed to my db.此端点位于一个单独的文件中,在我准备好要传递到我的数据库的数据后调用该文件。

I'll even take suggestions at material to look at if it may help.我什至会在材料上提出建议,看看它是否有帮助。 Thank you for your time.感谢您的时间。

You need to debounce your method :你需要去抖动你的方法:

let now = Date.now();
const delayQue = () =>{
    if(Date.now() - now > 5000) {
        now = Date.now();
        sendStepData()
    }
}   

it will only make the call if 5000ms has passed since the last call, and it will wait 5000ms before the first call also.它只会在自上次调用后 5000 毫秒后才进行调用,并且在第一次调用之前它也会等待 5000 毫秒。

You can also wait for the 5000ms to pass and call the method您也可以等待5000ms过去并调用该方法

let now = Date.now();
let timeoutId = null;
const delayQue = () =>{
    if(Date.now() - now > 5000) {
        now = Date.now();
        sendStepData()
    } else if(!timeoutId) {
        timeoutId = setTimeout(() => {
            now = Date.now();
            sendStepData();
            timeoutId = null,    
        },  5000 - (Date.now() - now))
    }
}   

you could look for the lodash debounce method also, if you don't want to do it yourself, and have a more complete solution如果你不想自己做,你也可以寻找 lodash debounce 方法,并有一个更完整的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM