From a853619512392ff2e08f3f8fc7ad6dad71d1c08d Mon Sep 17 00:00:00 2001 From: Jonas Lundberg Date: Wed, 15 Mar 2017 08:02:11 +0100 Subject: [PATCH 1/2] Remove non-existant sticky balancer from docs The sticky load balancing pattern does not exist and gives an error when trying to run. --- README.md | 2 +- test/balancer-error.js | 2 +- test/basic.js | 2 +- test/middleware.js | 2 +- test/ws.js | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a41c8cf..ad6c561 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ var CONFIG = { // Load balancing pattern // As of now a few are builtin - // random, roundrobin, sticky + // random, roundrobin pattern: 'roundrobin' } ], diff --git a/test/balancer-error.js b/test/balancer-error.js index 03b7f33..12d4d4c 100644 --- a/test/balancer-error.js +++ b/test/balancer-error.js @@ -44,7 +44,7 @@ var CONFIG = { // Load balancing pattern // As of now a few are builtin - // random, roundrobin, sticky + // random, roundrobin balancer: function(backends, req, cb) { // Fail first cb(new Error('Failed balancing because of whatever')); diff --git a/test/basic.js b/test/basic.js index b28d2b3..b9f45f9 100644 --- a/test/basic.js +++ b/test/basic.js @@ -44,8 +44,8 @@ var CONFIG = { // Load balancing pattern // As of now a few are builtin - // random, roundrobin, sticky balancer: loadfire.balancers.roundrobin, + // random, roundrobin middleware: [ function(req, res, next) { diff --git a/test/middleware.js b/test/middleware.js index 999582d..50ae73b 100644 --- a/test/middleware.js +++ b/test/middleware.js @@ -65,7 +65,7 @@ var CONFIG = { // Load balancing pattern // As of now a few are builtin - // random, roundrobin, sticky + // random, roundrobin balancer: loadfire.balancers.roundrobin, middleware: [ diff --git a/test/ws.js b/test/ws.js index 02438f7..42ae10c 100644 --- a/test/ws.js +++ b/test/ws.js @@ -25,7 +25,7 @@ var CONFIG = { // Load balancing pattern // As of now a few are builtin - // random, roundrobin, sticky + // random, roundrobin balancer: function(backends, req, cb) { return cb(null, { host: 'localhost', From 724755b7de245df4bd81fb2de265152c349b7ca6 Mon Sep 17 00:00:00 2001 From: Jonas Lundberg Date: Wed, 15 Mar 2017 08:04:38 +0100 Subject: [PATCH 2/2] Random balancer uses _.sample which is not defined The underscore version that is shipped with this lib (1.4.4) does not have the method sample. Manually generate a random array index since bumping the version of underscore to current (1.8.3) breaks more stuff. Verification: * Borrow the test/basic.js and substitute roundrobin for random balancer. Access it in a browser and verify that it indeed returns random positions. --- lib/balancers/random.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/balancers/random.js b/lib/balancers/random.js index 6122f33..ced0a1a 100644 --- a/lib/balancers/random.js +++ b/lib/balancers/random.js @@ -4,7 +4,8 @@ var _ = require('underscore'); // Random Pattern function random(backends, req, cb) { - return cb(null, _.sample(backends)); + var randomArrayIndex = _.random(1, backends.length) - 1; + return cb(null, backends[randomArrayIndex]); }