feat(tcp): add timeout when trying to connect to a TCP endpoint
* the timeout parameters in options will trigger a timeout only when the socket is connected (cf. https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback). To handle connection timeout we have to handle it manually
This commit is contained in:
parent
f79a0444f6
commit
e9834daa31
|
@ -153,10 +153,21 @@ TcpConnection.createConnectingConnection = function(
|
|||
connection._initSocket(socket);
|
||||
if (onConnectionEstablished) onConnectionEstablished(connection);
|
||||
});
|
||||
var timer = setTimeout(function(){
|
||||
log.error('TcpConnection: timeout when connecting to %j in %d ms', remoteEndPoint, connectionTimeout);
|
||||
connection.close();
|
||||
if (onConnectionFailed) onConnectionFailed(connection, new Error('Connection failed'));
|
||||
}, connectionTimeout)
|
||||
socket.once('error', onError);
|
||||
function onError(err) {
|
||||
clearTimeout(timer);
|
||||
if (onConnectionFailed) onConnectionFailed(connection, err);
|
||||
}
|
||||
socket.once('connect', onConnect);
|
||||
function onConnect() {
|
||||
log.info('TcpConnection: successfully connected to %j', remoteEndPoint);
|
||||
clearTimeout(timer);
|
||||
}
|
||||
return connection;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user