定时承诺队列/油门

时间:2014-12-19 07:00:31

标签: javascript queue promise bluebird rate-limiting

我有一个request-promise函数向API发出请求。我受到此API的限制,我不断收到错误消息:

Exceeded 2 calls per second for api client. Reduce request rates to resume uninterrupted service.

我正在并行运行几个Promise.each循环导致问题,如果我只运行Promise.each的一个实例,一切运行正常。在这些Promise.each次调用中,它们通过request-promise调用导致相同的函数a。我想用另一个queue函数包装这个函数,并将间隔设置为500毫秒,以便request不是在彼此之后,或者是并行的,而是设置为在队列上的时间。事情是,即使需要相当长的时间才能获得回复,我仍然需要这些承诺来获取其内容。

有什么能帮我的吗?我能把一个函数包装好的东西,它会以一定的间隔响应而不是并行响应或者一个接一个地触发函数吗?

更新:也许它确实需要特定的承诺,我试图使用下划线的节流功能

var debug = require("debug")("throttle")
var _ = require("underscore")
var request = require("request-promise")

function requestSite(){
  debug("request started")
  function throttleRequest(){
    return request({
      "url": "https://www.google.com"
    }).then(function(response){
      debug("request finished")
    })
  }
  return _.throttle(throttleRequest, 100)
}

requestSite()
requestSite()
requestSite()

我得到的就是这个:

$ DEBUG=* node throttle.js 
throttle request started +0ms
throttle request started +2ms
throttle request started +0ms

1 个答案:

答案 0 :(得分:5)

更新

最后的答案是错误的,这有效,但我仍然认为我可以做得更好:

// call fn at most count times per delay.
const debounce = function (fn, delay, count) {
    let working = 0, queue = [];
    function work() {
        if ((queue.length === 0) || (working === count)) return;
        working++;
        Promise.delay(delay).tap(() => working--).then(work);
        let {context, args, resolve} = queue.shift();
        resolve(fn.apply(context, args));
    }
    return function debounced() {
        return new Promise(resolve => {
            queue.push({context: this, args: arguments, resolve});
            if (working < count) work();
        });
    };
};

function mockRequest() {
    console.log("making request");
    return Promise.delay(Math.random() * 100);
}

var bounced = debounce(mockRequest, 800, 5);
for (var i = 0; i < 5; i++) bounced();
setTimeout(function(){
    for (var i = 0; i < 20; i++) bounced();
},2000);

所以你需要在整个功能范围内调整请求 - 这没关系。 Promise几乎内置排队。

var p = Promise.resolve(); // our queue

function makeRequest(){
    p = p.then(function(){ // queue the promise, wait for the queue
        return request("http://www.google.com");
    });
    var p2 = p; // get a local reference to the promise
    // add 1000 ms delay to queue so the next caller has to wait 
    p = p.delay(1000); 
    return p2;
};

现在,makeRequest调用将至少相隔1000毫秒。

jfriend已经指出你每秒需要两个请求而不是一个请求 - 这与第二个队列一样容易解决:

var p = Promise.resolve(1); // our first queue
var p2 = Promise.resolve(2); // our second queue

function makeRequest(){

    var turn = Promise.any([p, p2]).then(function(val){ 

        // add 1000 ms delay to queue so the next caller has to wait 
        // here we wait for the request too although that's not really needed, 
        // check both options out and decide which works better in your case
        if(val === 1){
            p = p.return(turn).delay(1, 1000);
        } else {
            p2 = p2.return(turn).delay(1, 1000); 
        }
        return request("http://www.google.com");
    });

    return turn; // return the actual promise
};

这可以类似地使用数组推广到n个承诺