From 794db085a26181ee85ea5ef06b022ce91a19b76b Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Thu, 18 Sep 2014 12:18:56 +0100 Subject: [PATCH] split app from server, add toobusy --- app.js | 24 ++++++++++++++++++++++++ index.js | 26 ++++++-------------------- middleware/toobusy.js | 19 +++++++++++++++++++ package.json | 3 ++- 4 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 app.js create mode 100644 middleware/toobusy.js diff --git a/app.js b/app.js new file mode 100644 index 00000000..30761954 --- /dev/null +++ b/app.js @@ -0,0 +1,24 @@ + +var app = require('express')(); + +/** ----------------------- middleware ----------------------- **/ + +app.use( require('./middleware/toobusy') ); // should be first +app.use( require('./middleware/headers') ); +app.use( require('./middleware/cors') ); +app.use( require('./middleware/jsonp') ); + +/** ----------------------- routes ----------------------- **/ + +// api root +app.get( '/', require('./controller/index') ); + +// suggest API +app.get( '/suggest', require('./sanitiser/suggest'), require('./controller/suggest') ); + +/** ----------------------- error middleware ----------------------- **/ + +app.use( require('./middleware/404') ); +app.use( require('./middleware/500') ); + +app.listen( process.env.PORT || 3100 ); \ No newline at end of file diff --git a/index.js b/index.js index e74fed10..e788c86b 100644 --- a/index.js +++ b/index.js @@ -1,23 +1,9 @@ -var app = require('express')(); +/** cluster webserver across all cores **/ -/** ----------------------- middleware ----------------------- **/ +var cluster = require('cluster'), + app = require('./app'); -app.use( require('./middleware/headers') ); -app.use( require('./middleware/cors') ); -app.use( require('./middleware/jsonp') ); - -/** ----------------------- routes ----------------------- **/ - -// api root -app.get( '/', require('./controller/index') ); - -// suggest API -app.get( '/suggest', require('./sanitiser/suggest'), require('./controller/suggest') ); - -/** ----------------------- error middleware ----------------------- **/ - -app.use( require('./middleware/404') ); -app.use( require('./middleware/500') ); - -app.listen( process.env.PORT || 3100 ); \ No newline at end of file +cluster(app) + .use(cluster.stats()) + .listen( process.env.PORT || 3100 ); \ No newline at end of file diff --git a/middleware/toobusy.js b/middleware/toobusy.js new file mode 100644 index 00000000..bbe6784f --- /dev/null +++ b/middleware/toobusy.js @@ -0,0 +1,19 @@ + +// middleware which blocks requests when the eventloop is too busy +var toobusy = require('toobusy'); + +function middleware(req, res, next){ + if( toobusy() ){ + res.status(503); // Service Unavailable + return next('Server Overwhelmed'); + } + return next(); +} + +// calling .shutdown allows your process to exit normally +process.on('SIGINT', function() { + toobusy.shutdown(); + process.exit(); +}); + +module.exports = middleware; \ No newline at end of file diff --git a/package.json b/package.json index 46c15a5d..32e98b8a 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,8 @@ "dependencies": { "express": "^4.8.8", "geopipes-elasticsearch-backend": "0.0.7", - "pelias-esclient": "0.0.25" + "pelias-esclient": "0.0.25", + "toobusy": "^0.2.4" }, "devDependencies": { "ciao": "^0.3.4",