Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript GDAX API-超出速率限制_Javascript_Api_Gdax Api - Fatal编程技术网

Javascript GDAX API-超出速率限制

Javascript GDAX API-超出速率限制,javascript,api,gdax-api,Javascript,Api,Gdax Api,当我试图从GDAX请求历史数据时,我收到一条错误消息,说“超出了速率限制”。我使用promises和setInterval从GDAX请求历史价格数据,如下所示: let promiseArray = //Array with promises let j = 0; let grabPrices = setInterval(() => { if (j === promiseArray.length) { //Do something when all promises

当我试图从GDAX请求历史数据时,我收到一条错误消息,说“超出了速率限制”。我使用promises和setInterval从GDAX请求历史价格数据,如下所示:

let promiseArray = //Array with promises
let j = 0;
let grabPrices = setInterval(() => {
    if (j === promiseArray.length) {
        //Do something when all promises have been resolved
    } 
    else {
        promiseArray[j].then(
            data => {
                //Work with data
            }
        ).catch(error => console.log('An error occurred: ' + error));
        j++;
    }
}, 1000) //I have even tried to use 10 seconds (10000) as a second argument to setInterval, but it doesn't make any difference.

利率限制 当超过速率限制时,将返回429过多请求的状态

RESTAPI 公共端点
我们通过IP限制公共端点:每秒3个请求,每秒最多6个突发请求。

当您有承诺时,请求已经发出,因此您的promiseArray是一个正在进行的请求数组

假设我有一个URL数组,并使用
fetch
获取内容。使用
map
将url映射到承诺,并向
promise提供承诺数组。所有

Promise.all(
  urls.map(fetch)
).then(
  resulst=>...
);
如果URL有10个项目,这个程序将立即发出10个请求

您可以将
fetch
函数传递给throttle函数,该函数将以每秒仅调用3次的方式安排fetch。throttle函数将返回承诺,但不会立即调用fetch

可以找到throttlePeriod函数。如果您只想从该模块复制粘贴代码,而不导入整个模块,那么您也需要该函数,因为throttlePeriod依赖于它

const fetchMaxThreePerSecond = throttlePeriod(3,1100)/*give it an extra 100*/(fetch)
Promise.all(
  urls.map(fetchMaxThreePerSecond)
).then(
  resulst=>...
);
这就解决了节流问题,但如果您知道
Promise.all
是如何工作的,您就会知道,如果只有一个拒绝,那么所有的承诺都会被拒绝。因此,如果您有100个URL和99个解析,但有一个拒绝了您的
。那么
永远不会被调用。您将丢失99个成功请求

您可以将承诺传递给
promise。所有不会拒绝的
,您可以通过捕获被拒绝的承诺并在捕获中返回一个特殊值来完成此操作,您可以在处理结果时过滤掉该值:

const fetchMaxThreePerSecond = throttlePeriod(3,1100)/*give it an extra 100*/(fetch);
const Fail = function(reason){this.reason = reason;};
const isFail = x=>(x&&x.constructor)===Fail;
const isNotFail = x=>!isFail(x);
Promise.all(
  urls.map(
    url=>
      fetchMaxThreePerSecond(url)
      .catch(e=>new Fail([url,e]))//save url and error in Fail object
  )
)
.then(//even if a request fails, it will not reject
  results=>{
    const successes = results.filter(isNotFail);
    const failed = results.filter(isFail);
    if(failed.length!==0){
      console.log("some requests failed:");
      failed.forEach(
        (fail)=>{
          const [url,error] = fail.reason;
          console.log(`Url: ${url}`);
          console.log(JSON.stringify(error,undefined,2));
        }
      )
    }
  }
); 

当我将速率限制设置为每2.1秒3次API调用时,我对GDAX历史调用没有问题

当我将速率限制设置为每1.8秒调用3次API时,我偶尔会遇到一些问题

我已经用自动化测试测试了这些值

重要提示:我对GDAX的所有呼叫共享相同的速率限制(!)


为了保存,请将@HMR答案中的代码与我测试的参数一起使用。

这太棒了。非常感谢。