I'm trying to automatically retry HTTP requests on timeout or error.
Currently my code looks like this:
var req = http.get(url, doStuff)
.on('error', retry)
.setTimeout(10000, retry);
However, a single request can sometimes trigger both "on error" and "timeout" events. What is a better way of implementing retry?
I was looking for same thing and found interesting module requestretry, well suited for such requirement.
Here is usage:
This was the code that worked for me. The key was to destroy the socket after timeout as well as to check that the response is complete.
You could try something like this:
UPDATE: In recent/modern versions of node, you can now specify a
timeout
option (measured in milliseconds) which sets the socket timeout (before the socket is connected). For example: