Node.js-使用每秒5个请求的API限制 [英] Node.js - Working with an API limit of 5 requests per second

查看:215
本文介绍了Node.js-使用每秒5个请求的API限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本",可以对特定的API进行数千个请求.该API每秒仅允许5个请求(可能与我的测量方式不同).为了发出请求,我使用了request-promise框架,并且我用以下方法取代了常规的request-promise函数:

I've got a 'script' that does thousands of requests to a specific API. This API will only allow 5 requests per second (and probably it measures differently then me). To make the requests I'm using request-promise framework, and I've superseded the normal request-promise function with this:

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个请求(发出或接收)被短暂触发,这些请求就会开始相互干扰.有谁有更好的解决方案?我认为对同一函数的递归调用会有所帮助,虽然可以,但是并不能解决问题.也许有一种将请求排队的模式,并且一次只处理几个吗?图书馆?

Once around 300 requests (give or take) are fired off in short succession, these requests start to interfere with each other. Does anyone have a better solution? I thought the recursive call to this same function would help, and It did but it didn't solve the problem. Maybe there is a pattern to queue requests, and do them a few at a time? A library perhaps?

谢谢!

推荐答案

好,而不是重复调用rp等,只需确保在两次请求之间延迟适当的时间...每秒5次,即200毫秒

OK, rather than recursing the call to rp etc, just make sure you delay between requests by an appropriate amount ... for 5 per second, that's 200ms

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();

这篇关于Node.js-使用每秒5个请求的API限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆