retry-axios
retry-axios copied to clipboard
Does not retry on timeout
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?
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 Can you provide an example? Using your settings, it didn't trigger a retry.
@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
.
Thanks @Grundlefleck
In the end, I switched to Got. It has the retry mechanism built-in also for timeouts