Search code examples
node.jsgoogle-apigoogle-api-nodejs-client

ECONNRESET and SOCKET HANG UP errors when trying to send Requests to Google API with node.js


I´m using Google´s node.js API Library to send product data to google merchant center. In about 30% of the requests i get the following errors:

I think the reason might be that the requests are send to fast and the quota limits of the API are hit. Which method can I use to limit the number of requests to the API? I´ve tried

setTimeout(() => { null }, 3000);

every time before the API request is send, but it looks like it doesn´t change anything.


Solution

  • Here´s the solution to that issue. I´ve added a wait method which returns a Promise.

    while((batchOffset + batchSize)  <= products.length) {
    
          const entries = [];
          const productsBatch = products.slice(batchOffset, (batchOffset+batchSize));
    
          // Wait between API calls to avoid hitting Google API quota limits 
          await this.wait(100);
    
          // Send a batch of n products to Google content API
          contentApi.products.custombatch(
            {
              requestBody: {
                entries: entries,
              },
            },
            (err, res) => {
              // handle err and response
              if (err !== null || res.status !== 200) {
                this.logger.log(
                  'error',
                  '...'
                );
              } else {
                this.logger.log(
                  'info',
                  '...',
                );
              }
            },
          );
    
          batchOffset+=batchSize;
    
    }
    
    
    ......
    
    private wait(ms: number) {
        return new Promise(resolve => setTimeout(resolve, ms))
      }