Skip to content

Commit

Permalink
#24
Browse files Browse the repository at this point in the history
  • Loading branch information
veny committed Jun 27, 2015
1 parent 2c6c574 commit 2a99be9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
5 changes: 3 additions & 2 deletions lib/gearmanode/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ var util = require('util'),
Job = require('./job').Job,
lb = require('./load-balancing'),
protocol = require('./protocol'),
common = require('./common');
common = require('./common'),
JS_CONSTANTS = require('./job-server').CONSTANTS;


/**
Expand All @@ -50,7 +51,7 @@ var Client = exports.Client = function(options) {
if (options.hasOwnProperty('loadBalancing')) { delete options.loadBalancing; }
if (options.hasOwnProperty('recoverTime')) { delete options.recoverTime; }

returned = common.verifyAndSanitizeOptions(clientOptions, { loadBalancing: 'Sequence', recoverTime: 30000 });
returned = common.verifyAndSanitizeOptions(clientOptions, { loadBalancing: 'Sequence', recoverTime: JS_CONSTANTS.DEFAULT_RECOVER_TIME });
if (returned instanceof Error) { return returned; }

this._type = 'Client';
Expand Down
6 changes: 6 additions & 0 deletions lib/gearmanode/job-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ var net = require('net'),
common = require('./common');


// constants
var constants = exports.CONSTANTS = {
DEFAULT_RECOVER_TIME: 30000
};


/**
* @class JobServer
* @classdesc A class representing an abstraction to Gearman job server (gearmand).
Expand Down
17 changes: 9 additions & 8 deletions lib/gearmanode/load-balancing.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
*
*/

var util = require('util'),
winston = require('winston'),
common = require('./common');
var util = require('util'),
winston = require('winston'),
common = require('./common');
JS_CONSTANTS = require('./job-server').CONSTANTS;


// After what time [s] can be a failed node reused in load balancing.
Expand All @@ -17,15 +18,15 @@ var util = require('util'),

/**
* Constructor.
*
*
* @params nodeCount count of nodes to be balanced
*/
var LBStrategy = function(nodeCount) {
if (!common.isNumber(nodeCount)) { return new Error('count of nodes is not number'); }

this.nodeCount = nodeCount;
this.badNodes = {};
this.recoverTime = 30000;
this.recoverTime = JS_CONSTANTS.DEFAULT_RECOVER_TIME;
};

/** Static logger. */
Expand All @@ -42,7 +43,7 @@ LBStrategy.prototype.nextIndex = common.abstractMethod;

/**
* Marks an index as good that means it can be used for next server calls.
*
*
* @param idx index to be marked as good
*/
LBStrategy.prototype.goodOne = function(idx) {
Expand Down Expand Up @@ -106,7 +107,7 @@ LBStrategy.prototype._searchNextGood = function (badIdx) {
/**
* Implementation of Sequence strategy.
* Assigns work in the order of nodes defined by the client initialization.
*
*
* @params nodeCount count of nodes to be balanced
*/
var Sequence = exports.Sequence = function(nodeCount) {
Expand Down Expand Up @@ -136,7 +137,7 @@ Sequence.prototype.nextIndex = function () {
/**
* Implementation of Round Robin strategy.
* Assigns work in round-robin order per nodes defined by the client initialization.
*
*
* @params nodeCount count of nodes to be balanced
*/
var RoundRobin = exports.RoundRobin = function(nodeCount) {
Expand Down
2 changes: 1 addition & 1 deletion lib/gearmanode/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


exports.VERSION_HISTORY = [
['0.9.1', '2015-06-26', 'PR #35, Enh #24'],
['0.9.1', '2015-06-28', 'PR #35, Enh #24'],
['0.9.0', '2015-06-19', 'PR #29, fully implemented Gearman Protocol'],
['0.2.2', '2015-02-04', 'BF #26'],
['0.2.1', '2015-01-04', 'BF #25'],
Expand Down
6 changes: 6 additions & 0 deletions test/test-load-balancing.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ describe('load-balancing', function() {


describe('#constructor', function() {
it('should return default instance of Sequence', function() {
lb.should.be.an.instanceof(Sequence);
lb.nodeCount.should.equal(2);
Object.keys(lb.badNodes).length.should.equal(0);
lb.recoverTime.should.equal(30000);
})
it('should return error when violated validation', function() {
// duplicate servers
lb = new Sequence();
Expand Down

0 comments on commit 2a99be9

Please sign in to comment.