GDAX API - 超出了费率限制 [英] GDAX API - Rate limit exceeded

查看:170
本文介绍了GDAX API - 超出了费率限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I get an error message saying "Rate limit exceeded" when I try to request historical data from GDAX. I use promises and setInterval to request historical price data from GDAX like this:

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.

来自官方API文档

速率限制
超过速率限制时,状态为429个太多请求将返回。

Rate Limits When a rate limit is exceeded, a status of 429 Too Many Requests will be returned.

REST API
PUBLIC ENDPOINTS
我们按IP限制公共端点:每秒3个请求,每秒最多6个请求突发。

REST API PUBLIC ENDPOINTS We throttle public endpoints by IP: 3 requests per second, up to 6 requests per second in bursts.

推荐答案

当你有一个承诺时,请求已经发出,所以你的promiseArray是一系列正在进行的请求。

When you have a promise then the request is already made so your promiseArray is an array of ongoing requests.

假设我有一个网址数组并使用 fetch 来获取内容。使用 map 将url映射到promise并将promises数组提供给 Promise.all

Let's say I have an array of urls and use fetch to get the content. Using map to map the url to a promise and give the array of promises to Promise.all:

Promise.all(
  urls.map(fetch)
).then(
  resulst=>...
);

如果网址有10个项目,此程序将立即发出10个请求。

If urls has 10 items this program will make 10 requests immediately.

您可以将 fetch 函数传递给一个限制函数,该函数将以每秒3次调用的方式调度fetch。油门功能将返回一个承诺,但不会立即调用fetch。

You can pass the fetch function to a throttle function that will schedule fetch in such a way that it will only be called 3 times a second. The throttle function will return a promise but will not call fetch immediately.

可以找到throttlePeriod函数这里。如果您只是要从该模块复制粘贴代码而不导入整个模块,那么您需要以后也起作用,因为throttlePeriod取决于它。

The throttlePeriod function can be found here. If you're only going to copy paste code from that module and not import the whole module then you need the later function as well because throttlePeriod depends on it.

const fetchMaxThreePerSecond = throttlePeriod(3,1100)/*give it an extra 100*/(fetch)
Promise.all(
  urls.map(fetchMaxThreePerSecond)
).then(
  resulst=>...
);

它负责限制,但如果你知道如何 Promise.all 让你知道如果只有一个拒绝,所有承诺都会被拒绝。因此,如果你有一百个网址和99个解决方案,但一个拒绝你的 .then 永远不会被调用。您将失去99个成功请求。

That takes care of the throttling but if you know how Promise.all works you know that all promises reject if only one rejects. So if you have a hundred urls and 99 resolve but one rejects your .then never gets called. You will loose the 99 successful requests.

您可以将承诺传递给 Promise.all ,但不会拒绝,你可以通过捕获被拒绝的承诺并在catch中返回一个特殊值,你可以在处理结果时过滤掉它:

You could pass a promise to Promise.all that will not reject, you can do so by catching a rejected promise and in the catch return a special value that you can filter out when processing results:

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

这篇关于GDAX API - 超出了费率限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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