retry-axios icon indicating copy to clipboard operation
retry-axios copied to clipboard

Does not retry on timeout

Open some-user123 opened this issue 4 years ago • 4 comments

I'm using retry-axios succesfully to retry on server-side errors like 5xx responses. However, I'm also experiencing sporadically timeouts on the server-side. In this cases, there is no retry attempt.

I would consider timeouts a service-side error as well. Shouldn't there be a retry attempt?

some-user123 avatar Dec 04 '20 08:12 some-user123

I had a similar issue and found that I needed to pass e.g. { timeout: 3_000 } to Axios itself, and combine that with the noResponseRetries: 3 options of this retry-axios library.

HTH

Grundlefleck avatar Dec 04 '20 11:12 Grundlefleck

@Grundlefleck Can you provide an example? Using your settings, it didn't trigger a retry.

Naramsim avatar Mar 29 '21 17:03 Naramsim

@Naramsim Here's an attempt at an SSCCE:

axios v0.21.1 retry-axios v2.4.0 node v14.2.0 (TypeScript but it shouldn't matter)

/* eslint-disable */
import axios from "axios";
import * as rax from "retry-axios";

const axiosInstance = axios.create({
  timeout: 2_000
});
axiosInstance.defaults.raxConfig = {
  retry: 3,
  noResponseRetries: 3,
  instance: axiosInstance,
  onRetryAttempt: (err) => {
    const retryAttempt = rax.getConfig(err)!.currentRetryAttempt!;
    console.log(`Retry attempt #${retryAttempt}. Error: ${err.code}: ${err.message}`);
  }
};
rax.attach(axiosInstance);

void axiosInstance
  .get("http://google.com:2013/") // Happens to be a URL that always times out
  .then(() => console.log("Request(s) successful"))
  .catch((error) => {
    console.log("Request(s) failed");
    console.error(error.message);
  });

Results in output:

Retry attempt #1. Error: ECONNABORTED: timeout of 2000ms exceeded
Retry attempt #2. Error: ECONNABORTED: timeout of 2000ms exceeded
Retry attempt #3. Error: ECONNABORTED: timeout of 2000ms exceeded
Request(s) failed
timeout of 2000ms exceeded

Without timeout: 2_000 given to axios.create() this example takes a several minutes to timeout, but it still does retry.

I also discovered that the actual retries attempted is essentially Math.min(raxConfig.retry, raxConfig.noResponseRetries). Doesn't seem we can specify noResponseRetries independently of retry.

Grundlefleck avatar Mar 30 '21 12:03 Grundlefleck

Thanks @Grundlefleck

In the end, I switched to Got. It has the retry mechanism built-in also for timeouts

Naramsim avatar Mar 30 '21 13:03 Naramsim