I am using https.get to request send a GET request to an URL: https://nodejs.org/api/http.html#httprequestoptions-callback
I am trying to figure out how the timeout
option works.
Someone mentions that the timeout
option strictly means connect timeout, and has no effect once the socket is established in this post: How to set a timeout on a http.request() in Node?
I have simple getRequest function with retry logic. Besides the timeout
option I also added a setTimeout
function to handle issues after the socket is opened.
function getRequest(url) {
return new Promise((resolve, reject) => {
const req = https.get(url, {timeout: 1000}, res => {
let rawData = '';
res.on('data', chunk => {
rawData += chunk;
});
res.on('end', () => {
resolve(rawData);
});
}).setTimeout(1000);
req.on('error', err => {
reject(new Error(err))
})
req.on('timeout', () => {
req.end();
getRequest(url);
})
});
}
I ran some tests, and out of 100 requests with this function, at least 1 takes exactly 5 seconds. That 5 seconds must be coming from somewhere, some default values, that I could not find.
How can I make sure that after 1 second the connection is closed and retry is initiated?
Edit: I tried all different kind of node modules with timeout capabilities: node-fetch, axios, etc. I face this issue using these modules as well. Axios throws the timeout error, but the node app itself does not finish, only after 5 seconds.