由于每秒上限限制和排队 API 请求 [英] Throttle and queue up API requests due to per second cap

查看:24
本文介绍了由于每秒上限限制和排队 API 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 mikeal/request 进行 API 调用.我最常使用的 API 之一(Shopify API).最近推出了新的通话限制, 我看到如下错误:

I'm use mikeal/request to make API calls. One of the API's I use most frequently (the Shopify API). Recently put out a new call limit, I'm seeing errors like:

Exceeded 6.0 calls per second for api client. Slow your requests or contact support for higher limits.

我已经升级了,但无论我获得多少带宽,我都必须考虑到这一点.对 Shopify API 的大部分请求都在 async.map() 函数内,这些函数循环异步请求,然后收集尸体.

I've already gotten an upgrade, but regardless of how much bandwidth I get I have to account for this. A large majority of the requests to the Shopify API are within async.map() functions, which loop asynchronous requests, and gather the bodies.

我正在寻找任何帮助,也许是一个已经存在的库,它会环绕请求模块并实际阻止、睡眠、节流、分配、管理异步触发的许多并发请求并将它们限制为一次说 6 个请求.如果它不存在,我对处理这样的项目没有问题.就是不知道怎么处理这种情况,希望能有个标准.

I'm looking for any help, perhaps a library that already exists, that would wrap around the request module and actually block, sleep, throttle, allocate, manage, the many simultaneous requests that are firing off asynchronously and limit them to say 6 requests at a time. I have no problem with working on such a project if it doesn't exist. I just don't know how to handle this kind of situation, and I'm hoping for some kind of standard.

我用 mikeal/request 制作了一张票.

I made a ticket with mikeal/request.

推荐答案

我在使用各种 API 时遇到了同样的问题.AWS 也以节流而闻名.

I've run into the same issue with various APIs. AWS is famous for throttling as well.

可以使用几种方法.你提到了 async.map() 函数.您是否尝试过 async.queue()?queue 方法应该允许你设置一个固定的限制(比如 6),任何超过这个数量的东西都会被放入队列中.

A couple of approaches can be used. You mentioned async.map() function. Have you tried async.queue()? The queue method should allow you to set a solid limit (like 6) and anything over that amount will be placed in the queue.

另一个有用的工具是 oibackoff.如果您从服务器返回错误并重试,该库将允许您退避您的请求.

Another helpful tool is oibackoff. That library will allow you to backoff your request if you get an error back from the server and try again.

包装这两个库以确保覆盖您的两个库会很有用:async.queue 确保您不会超过限制,oibackoff 确保您再次尝试获取您的请求,如果服务器告诉您有错误.

It can be useful to wrap the two libraries to make sure both your bases are covered: async.queue to ensure you don't go over the limit, and oibackoff to ensure you get another shot at getting your request in if the server tells you there was an error.

这篇关于由于每秒上限限制和排队 API 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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