diff --git a/src/transport/tcp/tcpConnection.js b/src/transport/tcp/tcpConnection.js index 1d2ab55..1326c49 100644 --- a/src/transport/tcp/tcpConnection.js +++ b/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; \ No newline at end of file +module.exports = TcpConnection;