circuit-breaker-await-async
Advanced tools
Comparing version 1.3.3 to 1.3.4
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,17 +6,12 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.CircuitBreaker = exports.errors = exports.states = undefined; | ||
exports.states = exports.errors = exports.CircuitBreaker = void 0; | ||
var _debug = require('debug'); | ||
var _debug = _interopRequireDefault(require("debug")); | ||
var _debug2 = _interopRequireDefault(_debug); | ||
var _events = _interopRequireDefault(require("events")); | ||
var _events = require('events'); | ||
var _events2 = _interopRequireDefault(_events); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const log = (0, _debug2.default)('circuit-breaker-await-async'); | ||
const states = exports.states = { | ||
const log = (0, _debug.default)('circuit-breaker-await-async'); | ||
const states = { | ||
CLOSED: 'CLOSED', | ||
@@ -26,8 +21,9 @@ OPEN: 'OPEN', | ||
}; | ||
const errors = exports.errors = { | ||
exports.states = states; | ||
const errors = { | ||
CIRCUIT_IS_OPEN: 'CIRCUIT_IS_OPEN' | ||
}; | ||
exports.errors = errors; | ||
class CircuitBreaker extends _events2.default { | ||
class CircuitBreaker extends _events.default { | ||
constructor(fn, { | ||
@@ -44,3 +40,2 @@ state = states.CLOSED, | ||
super(); | ||
this.fn = fn; | ||
@@ -50,6 +45,4 @@ this.maxFailures = maxFailures; | ||
this.resetTimeoutMs = resetTimeoutMs; | ||
this.state = state; | ||
this.errors = []; | ||
this.currentAttempt = 0; | ||
@@ -59,6 +52,12 @@ this.resetAttempt = 0; | ||
const handleCall = async () => { | ||
const { args, state } = this; | ||
log('circuit-breaker.call received', { args, state }); | ||
const { | ||
args, | ||
state | ||
} = this; | ||
log('circuit-breaker.call received', { | ||
args, | ||
state | ||
}); | ||
const isHalfOpen = state === states.HALF_OPEN; | ||
const isHalfOpen = state === states.HALF_OPEN; | ||
if (isHalfOpen) { | ||
@@ -76,2 +75,3 @@ this.currentAttempt = this.maxFailures - 1; | ||
} | ||
const result = await this.fn(...args); | ||
@@ -81,2 +81,3 @@ this.emit('circuit-breaker.call.succeeded', result); | ||
this.errors.push(e); | ||
if (isHalfOpen) { | ||
@@ -87,4 +88,4 @@ log(`HalfOpen trail called has failed: ${e.message}`); | ||
} else { | ||
log(`Attempt ${this.currentAttempt} Failed: ${e.message}`); | ||
// attempt again in callTimeoutMs | ||
log(`Attempt ${this.currentAttempt} Failed: ${e.message}`); // attempt again in callTimeoutMs | ||
setTimeout(() => { | ||
@@ -109,5 +110,4 @@ this.emit('circuit-breaker.call'); | ||
log('tripping circuitbreaker'); | ||
this.state = states.OPEN; | ||
this.state = states.OPEN; // in resetTimeoutMs, attempt resetting the circuit | ||
// in resetTimeoutMs, attempt resetting the circuit | ||
setTimeout(() => { | ||
@@ -132,10 +132,10 @@ this.emit('circuit-breaker.attempt-reset'); | ||
const doCall = ({ state }) => { | ||
const doCall = ({ | ||
state | ||
}) => { | ||
return new Promise((resolve, reject) => { | ||
this.emit('circuit-breaker.call'); | ||
this.on('circuit-breaker.call.succeeded', result => { | ||
resolve(result); | ||
}); | ||
this.on('circuit-breaker.call.failed', err => { | ||
@@ -159,3 +159,6 @@ reject(err); | ||
case states.HALF_OPEN: | ||
return doCall({ args: this.args, state: this.state }); | ||
return doCall({ | ||
args: this.args, | ||
state: this.state | ||
}); | ||
@@ -166,3 +169,5 @@ case states.OPEN: | ||
} | ||
} | ||
exports.CircuitBreaker = CircuitBreaker; |
{ | ||
"name": "circuit-breaker-await-async", | ||
"version": "1.3.3", | ||
"version": "1.3.4", | ||
"description": "ES6 circuit breaker built around await/async patterns", | ||
@@ -23,10 +23,9 @@ "main": "dist/main.js", | ||
"devDependencies": { | ||
"babel-cli": "6.26.0", | ||
"babel-core": "6.26.3", | ||
"babel-eslint": "10.1.0", | ||
"@babel/cli": "7.15.7", | ||
"@babel/core": "7.15.8", | ||
"@babel/eslint-parser": "7.15.8", | ||
"@babel/preset-env": "7.15.8", | ||
"@babel/register": "7.15.3", | ||
"babel-jest": "27.3.1", | ||
"babel-plugin-module-resolver": "4.1.0", | ||
"babel-plugin-transform-object-rest-spread": "6.26.0", | ||
"babel-preset-env": "1.7.0", | ||
"babel-register": "6.26.0", | ||
"commitizen": "4.2.4", | ||
@@ -33,0 +32,0 @@ "cz-conventional-changelog": "3.3.0", |
@@ -17,3 +17,3 @@ import debug from 'debug' | ||
export class CircuitBreaker extends EventEmitter { | ||
constructor(fn, { | ||
constructor (fn, { | ||
state = states.CLOSED, | ||
@@ -107,3 +107,3 @@ maxFailures = 10, | ||
call() { | ||
call () { | ||
this.args = arguments | ||
@@ -135,10 +135,10 @@ | ||
switch (this.state) { | ||
case states.CLOSED: | ||
case states.HALF_OPEN: | ||
return doCall({ args: this.args, state: this.state }) | ||
case states.CLOSED: | ||
case states.HALF_OPEN: | ||
return doCall({ args: this.args, state: this.state }) | ||
case states.OPEN: | ||
return rejectCall() | ||
case states.OPEN: | ||
return rejectCall() | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
20
500
21317