5

我有一个“脚本”,可以对特定 API 执行数千个请求。这个 API 每秒只允许 5 个请求(可能它的测量方式与我不同)。为了提出我正在使用request-promise框架的请求,我已经request-promise用这个取代了正常功能:

const request_promise  = require('request-promise')

function waitRetryPromise() {
  var count = 0 // keeps count of requests
  function rp(options) {
    const timedCall = (resolve) => setTimeout( ()=>resolve(rp(options)),1000) // recursive call
    count += 1
    if (count % 3 == 0) { // recalls after a second on every third request
      return new Promise(timedCall)
    } else {
      return request_promise(options)
    }
  }
  return rp
}

const rp = waitRetryPromise()

一旦连续发出大约 300 个请求(给予或接受),这些请求就会开始相互干扰。有没有人有更好的解决方案?我认为对同一函数的递归调用会有所帮助,它确实有效,但并没有解决问题。也许有一种模式来排队请求,一次做几个?也许是图书馆?

谢谢!

4

3 回答 3

4

好的,而不是递归调用 rp 等,只需确保您在请求之间延迟适当的数量......每秒 5 次,即 200 毫秒

function waitRetryPromise() {
    let promise = Promise.resolve();
    return function rp(options) {
        return promise = promise
        .then(() => new Promise(resolve => setTimeout(resolve, 200)))
        .then(() => request_promise(options));
    }
}
const rp = waitRetryPromise();
于 2017-10-12T05:09:03.180 回答
3

TimedQueue只要他们有工作要做,我的代码就会运行。该process()方法在所有工作完成后解析:

class Queue {
    constructor() {
        this.queue = [];
    }

    enqueue(obj) {
        return this.queue.push(obj);
    }

    dequeue() {
        return this.queue.shift();
    }

    hasWork() {
        return (this.queue.length > 0);
    }
}

function t_o(delay) {
    return new Promise(function (resolve, reject) {
        setTimeout(function () {
            resolve();
        }, delay);
    });
}

class TimedQueue extends Queue {
    constructor(delay) {
        super();
        this.delay = delay;
    }

    dequeue() {
        return t_o(this.delay).then(() => {
            return super.dequeue();
        });
    }
    
    process(cb) {
        return this.dequeue().then(data => {
            cb(data);
            if (this.hasWork())
                return this.process(cb);
        });
    }
}


var q = new TimedQueue(500);

for (var request = 0; request < 10; ++request)
    q.enqueue(request);

q.process(console.log).then(function () {
    console.log('done');
});

于 2017-10-12T04:33:37.557 回答
-1

我不确定,但也许你从下面得到一些想法

function placeAnOrder(orderNumber) {
    console.log("customer order:", orderNumber)
    cookAndDeliverFood(function () {
        console.log("food delivered order:", orderNumber);
    });
}

//  1 sec need to cook

function cookAndDeliverFood(callback){
    setTimeout(callback, 1000);
}

//users web request
placeAnOrder(1);
placeAnOrder(2);
placeAnOrder(3);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

于 2017-10-12T04:13:44.800 回答