From 513d8a19d6a00ed1a6fa038f828798ba3cc79de0 Mon Sep 17 00:00:00 2001 From: Ben Hall Date: Sun, 14 Jun 2015 19:09:11 +0100 Subject: [PATCH] Introduce a Connection Timeout for connecting to Docker --- lib/modem.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/modem.js b/lib/modem.js index 83d16eb..eba4beb 100644 --- a/lib/modem.js +++ b/lib/modem.js @@ -59,6 +59,7 @@ var Modem = function(opts) { this.cert = opts.cert; this.ca = opts.ca; this.timeout = opts.timeout; + this.connectionTimeout = opts.connectionTimeout; this.checkServerIdentity = opts.checkServerIdentity; if (this.key && this.cert && this.ca) { @@ -150,6 +151,7 @@ Modem.prototype.dial = function(options, callback) { Modem.prototype.buildRequest = function(options, context, data, callback) { var self = this; + var connectionTimeoutTimer; var req = http[self.protocol].request(options, function() {}); debug('Sending: %s', util.inspect(options, { @@ -157,6 +159,13 @@ Modem.prototype.buildRequest = function(options, context, data, callback) { depth: null })); + if (self.connectionTimeout) { + connectionTimeoutTimer = setTimeout(function() { + debug('Connection Timeout of %s ms exceeded', self.connectionTimeout); + req.abort(); + }, self.connectionTimeout); + } + if (self.timeout) { req.on('socket', function(socket) { socket.setTimeout(self.timeout); @@ -167,7 +176,16 @@ Modem.prototype.buildRequest = function(options, context, data, callback) { }); } + req.on('connect', function() { + clearTimeout(connectionTimeoutTimer); + }); + + req.on('disconnect', function() { + clearTimeout(connectionTimeoutTimer); + }); + req.on('response', function(res) { + clearTimeout(connectionTimeoutTimer); if (context.isStream === true) { self.buildPayload(null, context.isStream, context.statusCodes, context.openStdin, req, res, null, callback); } else { @@ -191,6 +209,7 @@ Modem.prototype.buildRequest = function(options, context, data, callback) { }); req.on('error', function(error) { + clearTimeout(connectionTimeoutTimer); self.buildPayload(error, context.isStream, context.statusCodes, false, {}, {}, null, callback); });