[英]How to throttle/rate limit requests to prevent 429 error with Axios
I'm attempting to use the Intercom API to close an array of conversations that match a certain criteria.我正在尝试使用 Intercom API 来关闭符合特定条件的一系列对话。 I am using Axios to first call their API to get an array of conversation ID's, then I am looping over those ID's and calling their API to close them.我使用 Axios 首先调用他们的 API 来获取一组对话 ID,然后我循环遍历这些 ID 并调用他们的 API 来关闭它们。 According to their documentation they are limited by the following:根据他们的文档,他们受到以下限制:
Although the permitted limit of requests lasts for 1 minute, we evenly distribute this into 10 second windows.尽管允许的请求限制持续 1 分钟,但我们将其均匀分配到 10 秒的窗口中。 This means that every 10 seconds, the amount of permitted requests resets.这意味着每 10 秒,允许的请求数量就会重置。 For example, a default rate limit of 1000 per minute means that you can send a maximum of 166 operations per 10 second period (1000/6)例如,默认速率限制为每分钟 1000 次意味着每 10 秒周期 (1000/6) 最多可以发送 166 个操作
I attempted to use P-Limit and that did allow for more successful requests before eventually getting a 429. Is there a good solution to throttle the requests to match the criteria they have set in their documentation?我尝试使用 P-Limit 并且在最终获得 429 之前确实允许更成功的请求。是否有一个很好的解决方案来限制请求以匹配他们在文档中设置的标准?
This was was my attempt so far using PLimit - For the sake of brevity I left out the code block for the first promise:这是我到目前为止使用 PLimit 的尝试 - 为简洁起见,我省略了第一个承诺的代码块:
const listOfConversations = [];
axios
.post(searchUrl, searchBodyParameters, config)
.then((response) => {...
.then(() => {
const promises = [];
listOfConversations.forEach((conversation) => {
const p = axios
.post(
`https://api.intercom.io/conversations/${conversation}/parts`,
closeBodyParameters,
config,
)
.catch((error) => {
console.log(
`Error. Failed to close conversations. Server Returned - ${error.response.status}`,
);
});
promises.push(limit(() => p));
});
})
.catch((error) => {
console.log(
`Error. Failed to get number of pages. Server Returned - ${error.response.status}`,
);
});
I found a perfect fix to this issue on another thread where the user had posted a very nice solution using set timeout.我在另一个线程上找到了解决此问题的完美方法,该线程中用户使用 set timeout 发布了一个非常好的解决方案。 I applied it here:我在这里应用了它:
const listOfConversations = [];
axios
.post(searchUrl, searchBodyParameters, config)
.then((response) => {...
.then(() => {
const interval = 70;
let promise = Promise.resolve();
listOfConversations.forEach((conversation) => {
promise = promise.then(() => {
axios.post(
`https://api.intercom.io/conversations/${conversation}/parts`,
closeBodyParameters,
config,
)
.catch((error) => {
console.log(
`Error. Failed to close conversations. Server Returned - ${error.response.status}`,
);
})
return new Promise((resolve) => {
setTimeout(resolve, interval);
})
})
});
})
.catch((error) => {
console.log(
`Error. Failed to get number of pages. Server Returned - ${error.response.status}`,
);
});
}
The original posted solution can be found here .可以在此处找到原始发布的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.