Comparing version 1.1.3 to 1.2.0
26
index.js
@@ -32,5 +32,3 @@ const ms = require('ms'); | ||
var failures = 0; | ||
var lastFailure = 0; | ||
var currentCooldown = config.cooldown; | ||
var failures, lastFailure, currentCooldown; | ||
@@ -49,3 +47,11 @@ function getState() { | ||
return function () { | ||
function reset() { | ||
failures = 0; | ||
lastFailure = 0; | ||
currentCooldown = config.cooldown; | ||
} | ||
reset(); | ||
function protector() { | ||
const args = Array.from(arguments); | ||
@@ -66,3 +72,5 @@ const originalCallback = args.pop(); | ||
lastFailure = Date.now(); | ||
currentCooldown = Math.min(currentCooldown * failures, config.maxCooldown); | ||
if (currentState === states[2]) { | ||
currentCooldown = Math.min(currentCooldown * failures, config.maxCooldown); | ||
} | ||
config.monitor({err, args}); | ||
@@ -83,2 +91,3 @@ originalCallback(err); | ||
} | ||
reset(); | ||
originalCallback.apply(null, arguments); | ||
@@ -88,3 +97,8 @@ } | ||
protected.apply(null, args.concat([callback])); | ||
}; | ||
} | ||
protector.reset = reset; | ||
return protector; | ||
} | ||
@@ -91,0 +105,0 @@ |
{ | ||
"name": "disyuntor", | ||
"description": "A circuit-breaker implementation with exponential backoff.", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"author": "José F. Romaniello <jfromaniello@gmail.com> (http://joseoncode.com)", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -208,2 +208,59 @@ const disyuntor = require('./..'); | ||
describe('reseting after cooldown', function () { | ||
var sut; | ||
var fail = false; | ||
beforeEach(function () { | ||
fail = false; | ||
sut = disyuntor((i, callback) => { | ||
if (fail) { return callback(new Error('failure')); } | ||
callback(null, i); | ||
}, { | ||
name: 'test.func', | ||
timeout: 10, | ||
maxFailures: 2, | ||
cooldown: '100ms', | ||
}); | ||
}); | ||
it('should work', function (done) { | ||
fail = true; | ||
async.series([ | ||
//fail twice | ||
cb => sut(2, () => cb()), | ||
cb => sut(2, () => cb()), | ||
//circuit is open | ||
cb => { | ||
sut(3, (err) => { | ||
assert.equal(err.message, 'test.func: the circuit-breaker is open'); | ||
cb(); | ||
}); | ||
}, | ||
//wait the circuits cooldown | ||
cb => setTimeout(cb, 100), | ||
//this one works | ||
cb => { | ||
//reset the breaker | ||
fail = false; | ||
sut(2, () => cb()); | ||
}, | ||
//fail | ||
cb => { | ||
fail = true; | ||
sut(2, () => cb()); | ||
}, | ||
//circuit should be still closed because maxFailures is 2. | ||
cb => { | ||
sut(3, (err) => { | ||
assert.equal(err.message, 'failure'); | ||
cb(); | ||
}); | ||
}, | ||
], done); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21613
508