简体   繁体   中英

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. 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. 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. This means that every 10 seconds, the amount of permitted requests resets. 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)

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?

This was was my attempt so far using PLimit - For the sake of brevity I left out the code block for the first promise:

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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM