Node version 8.11.3. 64-bit Windows 8.1
As I understood from docs, timeoutproperty in https.request options sets socket connection timeout.
I set it to minimum - 1 millisecond and it should definitely trigger 'timeout' event.
const options = {
host: 'yobit.net',
path: '/api/3/depth/ltc_btc',
timeout: 1
}
const req = https.request(options, res => {
});
req.on('timeout', () => {
console.error('request timeout');
})
req.end();
I expect 'reqest timeout' console message but it doen't appear.
However, when I create my custom https Agent with overriden createConnectionmethod that sets initial socket timeout to 1 millisecond there:
MyAgent.prototype.createConnection = function() {
const socket = https.Agent.prototype.createConnection.apply(this, arguments);
socket.setTimeout(1);
return socket;
}
and run the first code with agent parameter set to my custom agent, now it fires 'timeout' event.
I can confirm this behaviour in my Ubuntu 18.04 as well, with Node.js 8.11.3.
cc @nodejs/http
This issue may be related back to #12005, #8101, #7580, which dates back to when this feature is requested. By the way, it does timeout with Node.js 10.11.0 on my Windows 10 machine using the first part of code above.
I'm observing the same behavior with v10.15.2 on Ubuntu 16.04.6 LTS.
If the timeout is set as part of the HTTPS request options, than the timeout event is emitted as expected. Instead, a timeout set with request.setTimeout() does not seem to take effect.
I still can't find documentation on how to set a custom timeout for a given request :-( the following isn't working .... :
https.get("url", { timeout: 1000 }, (res) => {
resp.on("timeout", () => {
console.log("Never fired");
});
});
Doing this doesn't work either :
https.get("url", (req, res) => {
req.setTimeout(1000);
});
Can someone help ?
this worked for me:
let promiseHTTPSRequest = function(options)
{
return new Promise(function (resolve, reject)
{
options.timeout = 5000; //limit https request
var req = https.request(options, function(res)
{
var msg = "";
res.setEncoding("utf8");
res.on("data", function(chunk) {
msg += chunk;
});
res.on("end", function() {
//console.log(msg);
resolve(msg);
});
});
req.on("error", function(e) {
console.error(e);
reject(e);
});
req.on("timeout", function(chunk) {
console.log("Waited for response for " + options.timeout/1000 + " seconds. Exiting");
reject("Waited for response for " + options.timeout/1000 + " seconds. Exiting");
});
req.write(options.post_data);
req.end();
}).catch(function (error) {
console.log(error);
});
};
I tried reproducing the original issue and for me everything works as expected
Tested on Linux and Windows 10 with Node 12 and Node 14
I tried both request.timeout parameter on creation and request.setTimeout() call after creation
Does anyone still have this issue on a recent version?
Most helpful comment
I'm observing the same behavior with
v10.15.2onUbuntu 16.04.6 LTS.If the timeout is set as part of the HTTPS request options, than the
timeoutevent is emitted as expected. Instead, a timeout set withrequest.setTimeout()does not seem to take effect.