use `.destroy` if `.end` fails to close connection

This commit is contained in:
tigerbot 2017-06-05 11:28:53 -06:00
parent e3fbd768a2
commit a95f6ecead
1 changed files with 27 additions and 23 deletions

View File

@ -6,6 +6,12 @@ var PromiseA = require('bluebird');
var sni = require('sni'); var sni = require('sni');
var Packer = require('tunnel-packer'); var Packer = require('tunnel-packer');
function timeoutPromise(duration) {
return new PromiseA(function (resolve) {
setTimeout(resolve, duration);
});
}
function run(copts) { function run(copts) {
var activityTimeout = copts.activityTimeout || 2*60*1000; var activityTimeout = copts.activityTimeout || 2*60*1000;
var pongTimeout = copts.pongTimeout || 10*1000; var pongTimeout = copts.pongTimeout || 10*1000;
@ -38,37 +44,35 @@ function run(copts) {
} }
console.log('[closeSingle]', cid); console.log('[closeSingle]', cid);
try { PromiseA.resolve()
localclients[cid].end(); .then(function () {
setTimeout(function () { localclients[cid].end();
return timeoutPromise(500);
})
.then(function () {
if (localclients[cid]) { if (localclients[cid]) {
console.warn('[closeSingle]', cid, 'connection still present'); console.warn('[closeSingle]', cid, 'connection still present after calling `end`');
localclients[cid].destroy();
return timeoutPromise(500);
}
})
.then(function () {
if (localclients[cid]) {
console.error('[closeSingle]', cid, 'connection still present after calling `destroy`');
delete localclients[cid]; delete localclients[cid];
} }
}, 500); })
} catch (err) { .catch(function (err) {
console.warn('[closeSingle] failed to close connection', cid, err); console.error('[closeSingle] failed to close connection', cid, err);
delete localclients[cid]; delete localclients[cid];
} })
;
} }
, closeAll: function () { , closeAll: function () {
console.log('[closeAll]'); console.log('[closeAll]');
Object.keys(localclients).forEach(function (cid) { Object.keys(localclients).forEach(function (cid) {
try { clientHandlers.closeSingle(cid);
localclients[cid].end();
} catch (err) {
console.warn('[closeAll] failed to close connection', cid, err);
}
}); });
setTimeout(function () {
Object.keys(localclients).forEach(function (cid) {
if (localclients[cid]) {
console.warn('[closeAll]', cid, 'connection still present');
delete localclients[cid];
}
});
}, 500);
} }
, count: function () { , count: function () {