-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
net.Socket has no setConnectionTimeout(timeout) method #5757
Comments
The socket.setTimeout(timeout[, callback]) doesn't works to you ? (Node.js - Net Module) |
setTimeout is for socket timeout which means the max idle time (no bytes received or sent) for an established connection, what I need is a connection timeout, that's the max time to establish a connection. |
Just need the same. |
I don't think libuv supports this. Usually, people handle |
var socket = net.connect(/* ... */);
socket.setTimeout(5e3, () => socket.destroy());
socket.once('connect', () => socket.setTimeout(0)); I'm closing this as the existing APIs have this covered. |
The solution provided by @bnoordhuis is unreliable and might not work. I had no success getting it to work on Windows 10 (Electron 7). The reality of the situation is that However, it would be great if we could have used the |
@falahati Does this work?
|
Essentially yeah. My code is a little more complicated but in the end the same underlying logic is at work. isAlive(): Promise<boolean> {
if (!this.config || !this.config.portNumber || !this.config.ipAddress) {
return Promise.resolve(false);
}
var client = new net.Socket();
function destroy() {
if (client != null) {
client.destroy();
client = null;
}
}
return new Promise<boolean>(
(resolve) => {
try {
client.once(
"connect",
() => {
destroy();
resolve(true);
}
);
client.once(
"error",
() => {
destroy();
resolve(false);
}
);
client.connect(this.config.portNumber, this.config.ipAddress);
} catch (e) {
destroy();
resolve(false);
}
}
)
.timeout(2000)
.catch(() => {
destroy();
return false;
}
);
} With Promise.delay = (duration: number) => new Promise((resolve) => setTimeout(() => resolve(), duration));
Promise.prototype.timeout = function (duration): Promise<any> {
return new Promise((resolve, reject) => {
this.then(value => { resolve(value) });
Promise.delay(duration).then(() => { reject(new Error("timed out")) });
});
}; |
Have exactly the same problem, and actually found good solution
this setTimeout works only for sockets whose connection established
general timeout, if we dont want to wait time set by default,
You are welcome :) |
@kvelaro the timeout you mentioned is for the whole duration including establishing the connection, transferring, and finally closing. What we need here is the just connection timeout. |
@gbidkar actually the existing API works. Tested with Node 10.17 and 13.2 'use strict';
const net = require('net');
const events = require('events');
const sock = new net.Socket();
sock.setTimeout(5000);
sock.on('timeout', () => sock.emit('error', new Error('ETIMEDOUT')));
sock.on('error', (e) => console.log((new Date()).toISOString(), 'Error', e.message));
sock.connect(111, '8.8.8.8');
console.log((new Date()).toISOString(), 'Connecting'); Prints
Note that by default socket already has 20 sec timeout that won't be overridden by your custom one (the last log line) even if timeout is set to 0. So unless the socket is destroyed |
@Antonius-S The problem is to set connection timeout, not the socket timeout. |
@DanielYWoo IDK what do you mean by 'connection' and 'socket' timeout but my code detects timeout during connect. |
@Antonius-S @gbidkar However, I strongly ask for a direct |
I've read your comment before posting. Honestly I'm neither an expert in Node internals nor have an idea what your opinion is based on, I just prefer to trust the facts. Your move ;-P |
Hi For those who are still interested in connect timeout, here are some resources I found from various sources: In nodejs (since v10) it supports the http agent option timeout (acting as connect timeout) and the http send option as request timeout: Similarly in axios (v0.19.2): Now, with node's native fetch, connect timeout can be set using:
A discussion on how we can apply this globally: So, if you are wanting to implement your own connect timeout, you can refer to the above for ideas. I have briefly looked at them, and it looks like their implementation is done at a higher level using setTimeout. |
function connectWithTimeout(host, port, timeout) {
return new Promise((resolve, reject) => {
const socket = new net.Socket();
const timer = setTimeout(() => {
socket.destroy();
reject(new Error('Connection timed out'));
}, timeout);
socket.connect(port, host, () => {
clearTimeout(timer);
resolve(socket);
});
socket.on('error', (err) => {
clearTimeout(timer);
reject(err);
});
});
} |
Currently we can only set timeout for an established idle socket, I cannot find a way to set connection timeout. This is missing piece, right?
The text was updated successfully, but these errors were encountered: