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..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() { @@ -35,4 +35,21 @@ 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() { + done.fail(); + }, 500); + }).withShutdown(); + + server.listen(16789, function(err) { + request.get('http://localhost:16789/', function(err, response) { + should.exist(err); + done(); + }); + + setTimeout(server.forceShutdown, 100); + }); + }); });