From 3ab329acc6cece7d13d9a0709ee1231576d48a20 Mon Sep 17 00:00:00 2001 From: Lee Elenbaas Date: Tue, 13 Sep 2016 15:31:23 +0300 Subject: [PATCH 1/2] add support for forceShutdown --- index.js | 16 ++++++++++++---- package.json | 1 + test.js | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 4556c43..6837ce3 100644 --- a/index.js +++ b/index.js @@ -16,8 +16,8 @@ function addShutdown(server) { var isShuttingDown = false; var connectionCounter = 0; - function destroy(socket) { - if (socket._isIdle && isShuttingDown) { + function destroy(socket, force) { + if (force || (socket._isIdle && isShuttingDown)) { socket.destroy(); delete connections[socket._connectionId]; } @@ -43,7 +43,7 @@ function addShutdown(server) { }); }); - server.shutdown = function(cb) { + function shutdown(force, cb) { isShuttingDown = true; server.close(function(err) { if (cb) { @@ -52,10 +52,18 @@ function addShutdown(server) { }); Object.keys(connections).forEach(function(key) { - destroy(connections[key]); + destroy(connections[key], force); }); }; + server.shutdown = function(cb) { + shutdown(false, cb); + }; + + server.forceShutdown = function(cb) { + shutdown(true, cb); + }; + return server; }; diff --git a/package.json b/package.json index 69a4844..5787817 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "http", "https", "graceful", + "force", "shutdown" ], "author": "Dillon Buchanan", diff --git a/test.js b/test.js index 61a812d..0e26243 100644 --- a/test.js +++ b/test.js @@ -35,4 +35,22 @@ describe('http-shutdown', function(done) { setTimeout(server.shutdown, 100); }); }); + + it('Should force shutdown without waiting for outstanding traffic', function(done) { + var server = http.createServer(function(req, res) { + setTimeout(function() { + res.writeHead(200); + res.end('All done'); + }, 500); + }).withShutdown(); + + server.listen(16789, function(err) { + request.get('http://localhost:16789/', function(err, response) { + should.exist(err); + done(); + }); + + setTimeout(server.forceShutdown, 100); + }); + }); }); From 5d065d6039d7cc92693e2fa0f482c7c126cf5feb Mon Sep 17 00:00:00 2001 From: Lee Elenbaas Date: Wed, 14 Sep 2016 12:37:26 +0300 Subject: [PATCH 2/2] reduce lines that should never be run - fail test if they run --- test.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test.js b/test.js index 0e26243..2db3172 100644 --- a/test.js +++ b/test.js @@ -6,7 +6,7 @@ var request = require('request'); describe('http-shutdown', function(done) { it('Should shutdown with no traffic', function(done) { var server = http.createServer(function(req, res) { - res.end('OK'); + done.fail(); }).withShutdown(); server.listen(16789, function() { @@ -39,8 +39,7 @@ describe('http-shutdown', function(done) { it('Should force shutdown without waiting for outstanding traffic', function(done) { var server = http.createServer(function(req, res) { setTimeout(function() { - res.writeHead(200); - res.end('All done'); + done.fail(); }, 500); }).withShutdown();