From c84f10e9639dbd76d9e8a6c7912f24cdb89abc0b Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Fri, 6 Jan 2012 12:38:29 -0700
Subject: [PATCH] gracefully handle excessive load - all cases where bcrypt
 will take to long return 503, loadgen special cases 503 errors for better
 output.  closes #787

---
 lib/load_gen/activities/change_pass.js |  1 +
 lib/load_gen/activities/reset_pass.js  |  4 +++-
 lib/wsapi/complete_user_creation.js    |  6 ++++++
 lib/wsapi/update_password.js           | 10 ++++++++++
 4 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/lib/load_gen/activities/change_pass.js b/lib/load_gen/activities/change_pass.js
index bc0f40238..5c4741eba 100644
--- a/lib/load_gen/activities/change_pass.js
+++ b/lib/load_gen/activities/change_pass.js
@@ -78,6 +78,7 @@ exports.startFunc = function(cfg, cb) {
       newpass: user.password
     }, function (r) {
       try {
+        if (r && r.code === 503) return cb("server is too busy");
         cb(JSON.parse(r.body).success === true ? undefined : "password update failed");
       } catch(e) {
         cb("password update failed: " + e.toString());
diff --git a/lib/load_gen/activities/reset_pass.js b/lib/load_gen/activities/reset_pass.js
index ae8a9d8c5..eb56281be 100644
--- a/lib/load_gen/activities/reset_pass.js
+++ b/lib/load_gen/activities/reset_pass.js
@@ -91,7 +91,9 @@ exports.startFunc = function(cfg, cb) {
         token: r.body,
         pass: user.password
       }, function (r) {
-        if (!r || r.code !== 200) {
+        if (r && r.code === 503) {
+          return cb("server is too busy");
+        } else if (!r || r.code !== 200) {
           return cb("failed to complete user creation");
         }
         try {
diff --git a/lib/wsapi/complete_user_creation.js b/lib/wsapi/complete_user_creation.js
index 1623ebc10..912e94262 100644
--- a/lib/wsapi/complete_user_creation.js
+++ b/lib/wsapi/complete_user_creation.js
@@ -27,6 +27,12 @@ exports.process = function(req, res) {
     // now bcrypt the password
     wsapi.bcryptPassword(req.body.pass, function (err, hash) {
       if (err) {
+        console.log(err);
+        if (err.indexOf('exceeded') != -1) {
+          logger.warn("max load hit, failing on auth request with 503: " + err);
+          res.status(503);
+          return res.json({ success: false, reason: "server is too busy" });
+        }
         logger.error("can't bcrypt: " + err);
         return res.json({ success: false });
       }
diff --git a/lib/wsapi/update_password.js b/lib/wsapi/update_password.js
index 0b64f7198..fe647160e 100644
--- a/lib/wsapi/update_password.js
+++ b/lib/wsapi/update_password.js
@@ -27,6 +27,11 @@ exports.process = function(req, res) {
 
     bcrypt.compare(req.body.oldpass, hash, function (err, success) {
       if (err) {
+        if (err.indexOf('exceeded') != -1) {
+          logger.warn("max load hit, failing on auth request with 503: " + err);
+          res.status(503);
+          return res.json({ success: false, reason: "server is too busy" });
+        }
         logger.warn("error comparing passwords with bcrypt: " + err);
         return res.json({ success: false });
       }
@@ -39,6 +44,11 @@ exports.process = function(req, res) {
       logger.info("updating password for email " + req.session.userid);
       wsapi.bcryptPassword(req.body.newpass, function(err, hash) {
         if (err) {
+          if (err.indexOf('exceeded') != -1) {
+            logger.warn("max load hit, failing on auth request with 503: " + err);
+            res.status(503);
+            return res.json({ success: false, reason: "server is too busy" });
+          }
           logger.error("error bcrypting  password for password update for " + req.body.email, err);
           return res.json({ success: false });
         }
-- 
GitLab