Browse Source

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
master
maniolias 2 years ago
parent
commit
e9834daa31
  1. 13
      src/transport/tcp/tcpConnection.js

13
src/transport/tcp/tcpConnection.js

@ -153,11 +153,22 @@ 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;
};
module.exports = TcpConnection;
module.exports = TcpConnection;

Loading…
Cancel
Save