kontent-ai / delivery-sdk-js

Kontent Delivery SDK for Javascript
https://kontent.ai
MIT License
50 stars 34 forks source link

ECONNRESET error code while calling sync API #386

Closed rajarajac closed 7 months ago

rajarajac commented 7 months ago

Brief bug description

What went wrong? I am using delivery-sdk and performing a sync API request. Sync API requests are running into failures with error code "ECONNRESET".

Repro steps

I have a node.js based cron job which runs every 1 minute and executes a sync API request against a project. Unless the request is successful, the same continuation token will be used to make the request every min. Everyday exactly between 10-11AM EST i am observing these errors.

Expected behavior

I like to understand under which scenario, SDK will give this error code and how to overcome this.

Test environment

Additional context

I have necessary logs captured and provided to Kontent.ai support team via chat.

Screenshots

Add links to screenshots, if possible.

Enngage commented 7 months ago

Hi @rajarajac,

This exception is not actually caused by the SDK, but rather it is a generic error which occurs when there is a network related issue. When executing high number of requests, it may happen that some of them fail for no apparent reason.

The only thing you can try within the SDK is to customize the retryStrategy and set it up in a way that all failed requests are retried no matter the reason. If the issue is transient, it will help as the next retry attempt might succeed.

You can configure retry strategy such as:

 const client = createDeliveryClient({
            environmentId: 'x',
            retryStrategy: {
                canRetryError: (error) => true,
                maxAttempts: 5
            },
        })
rajarajac commented 7 months ago

@Enngage I am currently using below retry mechanism. Is there any improvement i can make here?

import { IRetryStrategyOptions } from '@kontent-ai/core-sdk';
import { createDeliveryClient } from '@kontent-ai/delivery-sdk';
import '../loadEnv.js';

const kontentAiRetryStrategy: IRetryStrategyOptions = {
  addJitter: false,
  canRetryError: (error) => {
    if (error.code === 'ECONNRESET') {
      return true;
    }
    return false;
  },
  deltaBackoffMs: 100,
  maxAttempts: 3
};

export const deliveryClientConfig = {
  "XXXXXXXXXXXXXXXXXXX": createDeliveryClient({
    environmentId: "XXXXXXXXXX",
    linkedItemsReferenceHandler: 'ignore',
    retryStrategy: kontentAiRetryStrategy,
    secureApiKey: process.env.SECURE_ACCESS_API_KEY_XXXXXXX,
    defaultQueryConfig: {
      useSecuredMode: true,
    }
  })
}
Enngage commented 7 months ago

Hi @rajarajac,

The code you have will not work - the Error object likely doesn't have code property that equals to ECONNRESET. You would have to inspect the received exception to see exactly how it's structured.

Additionally, with this you are saying that all other error types will not be retried because you are returning false in all other cases. I think it's much better if you set it up in an opposite way - retry all errors and skip retry for specific error types that you can identify (e.g. you could catch specific Delivery errors):

const client = createDeliveryClient({
     environmentId: 'x',
     retryStrategy: {
      canRetryError: (error) => {
           // only return false if you know what the error is and you are sure you don't want to retry it           
           return true;
       },
      maxAttempts: 5
        },
})