Skip to content

Commit

Permalink
Merge pull request #4 from lee-elenbaas/master
Browse files Browse the repository at this point in the history
add support for forceShutdown
  • Loading branch information
thedillonb committed Sep 15, 2016
2 parents 7056d7e + 5d065d6 commit 1838b9f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
16 changes: 12 additions & 4 deletions index.js
Expand Up @@ -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];
}
Expand All @@ -43,7 +43,7 @@ function addShutdown(server) {
});
});

server.shutdown = function(cb) {
function shutdown(force, cb) {
isShuttingDown = true;
server.close(function(err) {
if (cb) {
Expand All @@ -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;
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -11,6 +11,7 @@
"http",
"https",
"graceful",
"force",
"shutdown"
],
"author": "Dillon Buchanan",
Expand Down
19 changes: 18 additions & 1 deletion test.js
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit 1838b9f

Please sign in to comment.