Comparing version 2.5.3 to 2.6.0
@@ -30,3 +30,2 @@ 'use strict'; | ||
class Brakes extends EventEmitter { | ||
constructor(func, opts) { | ||
@@ -46,2 +45,4 @@ super(); | ||
this._circuitGeneration = 1; | ||
this.name = this._opts.name; | ||
@@ -116,2 +117,3 @@ this.group = this._opts.group; | ||
this._circuitOpen = true; | ||
this._circuitGeneration++; | ||
if (this._healthCheck) { | ||
@@ -189,7 +191,7 @@ this._setHealthInterval(); | ||
}); | ||
this.on('timeout', d => { | ||
this._timeoutHandler(d); | ||
this.on('timeout', (d, error, execGeneration) => { | ||
this._timeoutHandler(d, execGeneration); | ||
}); | ||
this.on('failure', d => { | ||
this._failureHandler(d); | ||
this.on('failure', (d, error, execGeneration) => { | ||
this._failureHandler(d, execGeneration); | ||
}); | ||
@@ -237,8 +239,12 @@ this._stats.on('update', d => { | ||
_timeoutHandler(runTime) { | ||
this._stats.timeout(runTime); | ||
_timeoutHandler(runTime, execGeneration) { | ||
if (execGeneration === this._circuitGeneration) { | ||
this._stats.timeout(runTime); | ||
} | ||
} | ||
_failureHandler(runTime) { | ||
this._stats.failure(runTime); | ||
_failureHandler(runTime, execGeneration) { | ||
if (execGeneration === this._circuitGeneration) { | ||
this._stats.failure(runTime); | ||
} | ||
} | ||
@@ -245,0 +251,0 @@ |
@@ -19,3 +19,2 @@ 'use strict'; | ||
class Circuit extends EventEmitter { | ||
constructor(brakes, main, fallback, options) { | ||
@@ -54,2 +53,7 @@ super(); | ||
// Save circuit generation to scope so we can compare it | ||
// to the current generation when a request fails. | ||
// This prevents failures from bleeding between circuit generations. | ||
const execGeneration = this._brakes._circuitGeneration; | ||
if (this._brakes._circuitOpen) { | ||
@@ -63,3 +67,3 @@ this._brakes._stats.shortCircuit(); | ||
} | ||
return Promise.reject(new CircuitBrokenError(this._brakes._stats._totals, this._brakes._opts.threshold)); | ||
return Promise.reject(new CircuitBrokenError(this._brakes.name, this._brakes._stats._totals, this._brakes._opts.threshold)); | ||
} | ||
@@ -79,6 +83,6 @@ | ||
if (err instanceof TimeOutError) { | ||
this._brakes.emit('timeout', endTime, err); | ||
this._brakes.emit('timeout', endTime, err, execGeneration); | ||
} | ||
else if (this._opts.isFailure(err)) { | ||
this._brakes.emit('failure', endTime, err); | ||
this._brakes.emit('failure', endTime, err, execGeneration); | ||
} | ||
@@ -96,2 +100,6 @@ // if fallback exists, call it upon failure | ||
if (err.message && this._brakes.name) { | ||
err.message = `[Breaker: ${this._brakes.name}] ${err.message}`; | ||
} | ||
return Promise.reject(err); | ||
@@ -98,0 +106,0 @@ }); |
@@ -6,8 +6,14 @@ 'use strict'; | ||
class CircuitBrokenError extends Error { | ||
constructor(totals, threshold) { | ||
constructor(name, totals, threshold) { | ||
super(); | ||
this.message = `${consts.CIRCUIT_OPENED} - The percentage of failed requests (${Math.floor((1 - totals.successful / totals.total) * 100)}%) is greater than the threshold specified (${threshold * 100}%)`; | ||
let prefix = ''; | ||
if (name) { | ||
prefix = `[Breaker: ${name}] `; | ||
} | ||
this.message = `${prefix}${consts.CIRCUIT_OPENED} - The percentage of failed requests (${Math.floor((1 - totals.successful / totals.total) * 100)}%) is greater than the threshold specified (${threshold * 100}%)`; | ||
this.totals = totals; | ||
this.name = name; | ||
} | ||
@@ -14,0 +20,0 @@ } |
@@ -8,3 +8,2 @@ 'use strict'; | ||
class GlobalStats { | ||
constructor() { | ||
@@ -11,0 +10,0 @@ this._brakesInstances = []; |
@@ -15,3 +15,2 @@ 'use strict'; | ||
class Stats extends EventEmitter { | ||
constructor(opts) { | ||
@@ -65,3 +64,4 @@ super(); | ||
}, | ||
interval || this._opts.statInterval); | ||
interval || this._opts.statInterval | ||
); | ||
this._snapshotInterval.unref(); | ||
@@ -195,5 +195,4 @@ } | ||
} | ||
} | ||
module.exports = Stats; |
@@ -102,7 +102,7 @@ 'use strict'; | ||
propertyValue_circuitBreakerErrorThresholdPercentage: json.threshold, | ||
propertyValue_circuitBreakerForceOpen: false, // not reported | ||
propertyValue_circuitBreakerForceClosed: false, // not reported | ||
propertyValue_circuitBreakerEnabled: true, // not reported | ||
propertyValue_executionIsolationStrategy: 'THREAD', // not reported | ||
propertyValue_executionIsolationThreadTimeoutInMilliseconds: 800, // not reported | ||
propertyValue_circuitBreakerForceOpen: false, // not reported | ||
propertyValue_circuitBreakerForceClosed: false, // not reported | ||
propertyValue_circuitBreakerEnabled: true, // not reported | ||
propertyValue_executionIsolationStrategy: 'THREAD', // not reported | ||
propertyValue_executionIsolationThreadTimeoutInMilliseconds: 800, // not reported | ||
propertyValue_executionIsolationThreadInterruptOnTimeout: true, // not reported | ||
@@ -113,5 +113,5 @@ propertyValue_executionIsolationThreadPoolKeyOverride: null, // not reported | ||
propertyValue_metricsRollingStatisticalWindowInMilliseconds: 10000, // not reported | ||
propertyValue_requestCacheEnabled: false, // not reported | ||
propertyValue_requestLogEnabled: false, // not reported | ||
reportingHosts: 1 // not reported | ||
propertyValue_requestCacheEnabled: false, // not reported | ||
propertyValue_requestLogEnabled: false, // not reported | ||
reportingHosts: 1 // not reported | ||
}; | ||
@@ -118,0 +118,0 @@ } |
{ | ||
"name": "brakes", | ||
"version": "2.5.3", | ||
"version": "2.6.0", | ||
"description": "Node.js Circuit Breaker Pattern", | ||
@@ -40,12 +40,12 @@ "main": "index.js", | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"chai": "^4.0.1", | ||
"chai-things": "^0.2.0", | ||
"coveralls": "^2.11.16", | ||
"eslint": "^3.8.1", | ||
"eslint-config-airbnb-base": "^11.1.0", | ||
"eslint": "^4.6.1", | ||
"eslint-config-airbnb-base": "^12.0.0", | ||
"eslint-plugin-import": "^2.0.1", | ||
"mocha": "^3.1.2", | ||
"nyc": "^10.0.0", | ||
"sinon": "^2.1.0" | ||
"mocha": "^3.5.3", | ||
"nyc": "^11.2.1", | ||
"sinon": "^3.2.1" | ||
} | ||
} |
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
38663
7245
762