Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry api call #1691

Merged
merged 14 commits into from
Mar 30, 2023
Merged

Retry api call #1691

merged 14 commits into from
Mar 30, 2023

Conversation

Ashar2shahid
Copy link
Contributor

@Ashar2shahid Ashar2shahid commented Mar 22, 2023

closes #1658

There are two ways to have 2 attempts one of them is this:

  const goRes = await go(() => adapter.buildAndExecuteRequest(options, { timeout: (timeout * 2) / 3 }), {
    retries: 1,
    attemptTimeoutMs: [timeout / 3, (timeout * 2) / 3],
    totalTimeoutMs: timeout,
  });

The go function will take 10s and 20s for each attempt before it timeouts, however the timeout that is passed to adapter cannot be adjusted dynamically for each attempt which means we have to supply the max timeout which will block the axios socket for an extra 10 seconds in the first attempt if the api call fails

The other way is:

  const goRes = await go(() => adapter.buildAndExecuteRequest(options, { timeout: timeout / 3 }), {
    totalTimeoutMs: timeout / 3,
  });
  if (!goRes.success) {
    const goRes = await go(() => adapter.buildAndExecuteRequest(options, { timeout: (timeout * 2) / 3 }), {
      totalTimeoutMs: (timeout * 2) / 3,
    });
    if (!goRes.success) {
      const { aggregatedApiCall } = payload;
      const log = logger.pend('ERROR', `Failed to call Endpoint:${aggregatedApiCall.endpointName}`, goRes.error);
      // eslint-disable-next-line import/no-named-as-default-member
      const axiosErrorMsg = axios.isAxiosError(goRes.error) ? errorMsgFromAxiosError(goRes.error) : '';
      const errorMessage = compact([RequestErrorMessage.ApiCallFailed, axiosErrorMsg]).join(' ');
      return [[log], { success: false, errorMessage: errorMessage }];
    }
    return [[], { ...goRes.data }];
  }

This doesn't block but has extra lines of code

@Ashar2shahid
Copy link
Contributor Author

@bdrhn9 @amarthadan thoughts on which is better ?

@Ashar2shahid Ashar2shahid marked this pull request as draft March 22, 2023 16:29
@amarthadan
Copy link
Contributor

I see. Well, if we want to avoid Axios blocking file descriptors we should go with option 2. I don't think having those few extra lines is a big deal.

@Ashar2shahid Ashar2shahid marked this pull request as ready for review March 28, 2023 14:14
Copy link
Contributor

@Siegrift Siegrift left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extra complexity with TIMEOUT/3 and TIMEOUT*2/3 is not worth it. I think it's better to define two constants FIRST_API_CALL_TIMEOUT and SECOND_API_CALL_TIMEOUT.

packages/airnode-node/src/api/index.ts Show resolved Hide resolved
packages/airnode-node/src/api/index.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@Siegrift Siegrift left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 LGTM after the naming improvements.

packages/airnode-node/src/api/index.ts Outdated Show resolved Hide resolved
packages/airnode-node/src/api/index.ts Show resolved Hide resolved
packages/airnode-node/src/api/index.ts Outdated Show resolved Hide resolved
packages/airnode-node/src/api/index.ts Outdated Show resolved Hide resolved
@Ashar2shahid Ashar2shahid merged commit ad64271 into master Mar 30, 2023
@Ashar2shahid Ashar2shahid deleted the retry-api-call branch March 30, 2023 21:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add retries to performApiCall
3 participants