Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

disyuntor

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

disyuntor - npm Package Compare versions

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);
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc