From b8b9da2c304314aeb70a8c54f76c2186d9986d75 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 5 Nov 2015 12:44:42 -0500 Subject: [PATCH] Squashed commit of the following: commit 62d59620d26bc97c7d689e9af57cd8bff654c79e Author: Julian Lam Date: Wed Nov 4 11:04:27 2015 -0500 tweaked 503 template commit 15a61cbc239c9d654691d91cdadce59e13d97586 Author: Julian Lam Date: Wed Nov 4 10:58:19 2015 -0500 added text about reloading being required if threshold values are changed commit 3fe87699332ef0628b4db31f4afef245802a7bc0 Author: Julian Lam Date: Wed Nov 4 10:53:35 2015 -0500 added ACP settings for toobusy commit f6a9964baff051072052e6ef99da9e1ffba014df Author: Julian Lam Date: Wed Nov 4 08:53:53 2015 -0500 removed unused 503a template commit e6d444736baf4c676f8461d30a5504c6e45df163 Author: Julian Lam Date: Wed Nov 4 02:01:11 2015 -0500 updated code to send static 503 instead of maintenance-style page commit 11089ae2bb833e068b01ee77ee745d9fd5344805 Author: Julian Lam Date: Wed Nov 4 01:18:45 2015 -0500 added toobusy support so the Node process doesn't fall over at high load --- package.json | 1 + public/503.html | 160 ++++++++++++++++++++++++++ public/language/en_GB/pages.json | 4 +- src/middleware/middleware.js | 12 +- src/routes/helpers.js | 2 +- src/views/admin/settings/advanced.tpl | 29 +++++ 6 files changed, 205 insertions(+), 3 deletions(-) create mode 100644 public/503.html diff --git a/package.json b/package.json index e5f1b23fa7..ccf4ecb694 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,7 @@ "socketio-wildcard": "~0.1.1", "string": "^3.0.0", "templates.js": "0.3.0", + "toobusy-js": "^0.4.2", "uglify-js": "^2.4.24", "underscore": "~1.8.3", "underscore.deep": "^0.5.1", diff --git a/public/503.html b/public/503.html new file mode 100644 index 0000000000..124e92ef26 --- /dev/null +++ b/public/503.html @@ -0,0 +1,160 @@ + + + Excessive Load Warning + + + + + +
+
+

503

+

+ This forum is temporarily unavailable due to excessive load.
+ We shouldn't be down for long. Please check back shortly. Sorry for the inconvenience! +

+

+ Alright. You can stop clicking... it's not going to make the site come back sooner! +

+
+
+ + diff --git a/public/language/en_GB/pages.json b/public/language/en_GB/pages.json index f398d5b1ff..b5dbc7140a 100644 --- a/public/language/en_GB/pages.json +++ b/public/language/en_GB/pages.json @@ -42,5 +42,7 @@ "account/watched": "Topics watched by %1", "maintenance.text": "%1 is currently undergoing maintenance. Please come back another time.", - "maintenance.messageIntro": "Additionally, the administrator has left this message:" + "maintenance.messageIntro": "Additionally, the administrator has left this message:", + + "throttled.text": "%1 is currently unavailable due to excessive load. Please come back another time." } \ No newline at end of file diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index 6b8447bc57..89416cd929 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -11,13 +11,13 @@ var app, validator = require('validator'), nconf = require('nconf'), ensureLoggedIn = require('connect-ensure-login'), + toobusy = require('toobusy-js'), plugins = require('../plugins'), meta = require('../meta'), user = require('../user'), groups = require('../groups'), - analytics = require('../analytics'), controllers = { @@ -25,6 +25,9 @@ var app, helpers: require('../controllers/helpers') }; +toobusy.maxLag(parseInt(meta.config.eventLoopLagThreshold, 10) || 70); +toobusy.interval(parseInt(meta.config.eventLoopInterval, 10) || 500); + middleware.authenticate = function(req, res, next) { if (req.user) { return next(); @@ -217,6 +220,13 @@ middleware.privateUploads = function(req, res, next) { next(); }; +middleware.busyCheck = function(req, res, next) { + if (toobusy()) { + res.type('text/html').sendFile(path.join(__dirname, '../../public/503.html')); + } else { + next(); + } +}; module.exports = function(webserver) { app = webserver; diff --git a/src/routes/helpers.js b/src/routes/helpers.js index 3cd1524fc5..3dcca80bc4 100644 --- a/src/routes/helpers.js +++ b/src/routes/helpers.js @@ -5,7 +5,7 @@ var helpers = {}; helpers.setupPageRoute = function(router, name, middleware, middlewares, controller) { middlewares = middlewares.concat([middleware.pageView, middleware.pluginHooks]); - router.get(name, middleware.buildHeader, middlewares, controller); + router.get(name, middleware.busyCheck, middleware.buildHeader, middlewares, controller); router.get('/api' + name, middlewares, controller); }; diff --git a/src/views/admin/settings/advanced.tpl b/src/views/admin/settings/advanced.tpl index f6c3417082..3a0a314106 100644 --- a/src/views/admin/settings/advanced.tpl +++ b/src/views/admin/settings/advanced.tpl @@ -41,4 +41,33 @@ +
+
Traffic Management
+
+

+ NodeBB deploys equipped with a module that automatically denies requests in high-traffic + situations. You can tune these settings here, although the defaults are a good starting + point. +

+
+
+ + +

+ Lowering this value decreases wait times for page loads, but will also show the + "excessive load" message to more users. (Reload required) +

+
+
+ + +

+ Lowering this value causes NodeBB to become more sensitive to spikes in load, but + may also cause the check to become too sensitive. (Reload required) +

+
+
+
+
+ \ No newline at end of file