From 76bd2fbea2e2dac5b580fa6d20b8779f541b06f7 Mon Sep 17 00:00:00 2001 From: Feng Zhou Date: Tue, 30 Jun 2015 17:13:54 +0800 Subject: [PATCH] handle connect() timeout --- lib/connect.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/connect.js b/lib/connect.js index 714d8cb2..883083d0 100644 --- a/lib/connect.js +++ b/lib/connect.js @@ -134,10 +134,6 @@ function connect(url, socketOptions, openCallback) { function onConnect() { sockok = true; sock.setNoDelay(noDelay); - if (timeout) { - sock.setTimeout( - timeout, openCallback.bind(this, new Error('connect ETIMEDOUT'))); - } if (keepAlive) sock.setKeepAlive(keepAlive, keepAliveDelay); var c = new Connection(sock); @@ -162,6 +158,11 @@ function connect(url, socketOptions, openCallback) { throw new Error("Expected amqp: or amqps: as the protocol; got " + protocol); } + if (timeout) { + sock.setTimeout( + timeout, openCallback.bind(this, new Error('connect ETIMEDOUT'))); + } + sock.once('error', function(err) { if (!sockok) openCallback(err); });