Skip to content

Commit

Permalink
Merge branch 'connection-timeout' of https://github.com/BenHall/docke…
Browse files Browse the repository at this point in the history
…r-modem into BenHall-connection-timeout
  • Loading branch information
apocas committed Sep 20, 2019
2 parents 4ea0bbb + 513d8a1 commit 36b3522
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/modem.js
Expand Up @@ -67,6 +67,7 @@ var Modem = function(options) {
this.cert = opts.cert;
this.ca = opts.ca;
this.timeout = opts.timeout;
this.connectionTimeout = opts.connectionTimeout;
this.checkServerIdentity = opts.checkServerIdentity;
this.agent = opts.agent;
this.headers = opts.headers || {};
Expand Down Expand Up @@ -188,6 +189,8 @@ Modem.prototype.dial = function(options, callback) {

Modem.prototype.buildRequest = function(options, context, data, callback) {
var self = this;
var connectionTimeoutTimer;

var opts = self.protocol === 'ssh' ? Object.assign(options, {
agent: ssh({'host': self.host, 'port': self.port}),
protocol: 'http'
Expand All @@ -200,6 +203,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);
Expand All @@ -211,12 +221,22 @@ Modem.prototype.buildRequest = function(options, context, data, callback) {
}

if (context.hijack === true) {
clearTimeout(connectionTimeoutTimer);
req.on('upgrade', function(res, sock, head) {
return callback(null, sock);
});
}

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 {
Expand All @@ -238,6 +258,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);
});

Expand Down

0 comments on commit 36b3522

Please sign in to comment.