开发者

throttling http requests google chrome extension

开发者 https://www.devze.com 2023-03-22 19:08 出处:网络
As it turns out the answer to my prior question does not work.The problem is that I am overloading the server with requests.The process needs a throttle so that subsequent requests incur a little slow

As it turns out the answer to my prior question does not work. The problem is that I am overloading the server with requests. The process needs a throttle so that subsequent requests incur a little slowdown. Here is the code in question. The important segment of code that needs a timer or setInterval is that prefaced by the alert "Profile Rejected" although it would be acceptable to slow them both down. Any suggestions?

if (greetThisOne==true && !bGreeted)
{
    //alert ("Requesting Message Page");
    console.log="Message Page Requested";
    chrome.extension.sendRequest({cmd: "openMessage", url: messageLink, keyWordsFound: keyWordList, greeted: bGreeted});    
}
else 
{
    //alert("Profile Rejected");
    console.log="Profile Rejected";
    chrome.exte开发者_开发知识库nsion.sendRequest({cmd: "profileRejected", url: messageLink, keyWordsFound: keyWordList, greeted: bGreeted});
}


You would need to implement some queue in the background page. For example:

var rejectedProfiles = [];
processRejectedProfiles();

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if(request.cmd == "profileRejected") {
        //add to the end of queue
        rejectedProfiles.push({url: request.url, other: request.parameters});
    }
    sendResponse({});
});

function processRejectedProfiles() {
    if(rejectedProfiles.length > 0) {
        //get the oldest element in queue
        var profile = rejectedProfiles.shift();

        //process profile
        ...
    }

    //process next entry in the queue in 3 seconds
    setTimeout(processRejectedProfiles, 3000);
}

This way you will be processing one profile at a time, with a provided delay.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号