-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add enable/disable for a circuit (#160)
This commit also includes some code climate refactoring. Fixes: #136
- Loading branch information
Showing
4 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict'; | ||
|
||
const { FALLBACK_FUNCTION } = require('./symbols'); | ||
|
||
function handleError (error, circuit, timeout, args, latency, resolve, reject) { | ||
clearTimeout(timeout); | ||
fail(circuit, error, args, latency); | ||
const fb = fallback(circuit, error, args, latency); | ||
if (fb) resolve(fb); | ||
else reject(error); | ||
} | ||
|
||
function fallback (circuit, err, args) { | ||
if (circuit[FALLBACK_FUNCTION]) { | ||
const result = | ||
circuit[FALLBACK_FUNCTION].apply(circuit[FALLBACK_FUNCTION], args); | ||
/** | ||
* Emitted when the circuit breaker executes a fallback function | ||
* @event CircuitBreaker#fallback | ||
*/ | ||
circuit.emit('fallback', result, err); | ||
if (result instanceof Promise) return result; | ||
return Promise.resolve(result); | ||
} | ||
} | ||
|
||
function fail (circuit, err, args, latency) { | ||
/** | ||
* Emitted when the circuit breaker action fails | ||
* @event CircuitBreaker#failure | ||
*/ | ||
circuit.emit('failure', err, latency); | ||
|
||
// check stats to see if the circuit should be opened | ||
const stats = circuit.stats; | ||
const errorRate = stats.failures / stats.fires * 100; | ||
if (errorRate > circuit.options.errorThresholdPercentage || | ||
circuit.options.maxFailures >= stats.failures || | ||
circuit.halfOpen) { | ||
circuit.open(); | ||
} | ||
} | ||
|
||
// http://stackoverflow.com/a/2117523 | ||
const nextName = () => | ||
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => { | ||
const r = Math.random() * 16 | 0; | ||
const v = c === 'x' ? r : (r & 0x3 | 0x8); | ||
return v.toString(16); | ||
}); | ||
|
||
module.exports = exports = { | ||
handleError, | ||
fallback, | ||
fail, | ||
nextName | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
const STATE = Symbol('state'); | ||
const OPEN = Symbol('open'); | ||
const CLOSED = Symbol('closed'); | ||
const HALF_OPEN = Symbol('half-open'); | ||
const PENDING_CLOSE = Symbol('pending-close'); | ||
const FALLBACK_FUNCTION = Symbol('fallback'); | ||
const STATUS = Symbol('status'); | ||
const NAME = Symbol('name'); | ||
const GROUP = Symbol('group'); | ||
const HYSTRIX_STATS = Symbol('hystrix-stats'); | ||
const ENABLED = Symbol('Enabled'); | ||
|
||
module.exports = exports = { | ||
STATE, | ||
OPEN, | ||
CLOSED, | ||
HALF_OPEN, | ||
PENDING_CLOSE, | ||
FALLBACK_FUNCTION, | ||
STATUS, | ||
NAME, | ||
GROUP, | ||
HYSTRIX_STATS, | ||
ENABLED | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
|
||
const test = require('tape'); | ||
const opossum = require('../'); | ||
const { passFail } = require('./common'); | ||
|
||
test('Defaults to enabled', t => { | ||
t.plan(1); | ||
const breaker = opossum(passFail); | ||
t.equals(breaker.enabled, true); | ||
t.end(); | ||
}); | ||
|
||
test('Accepts options.enabled', t => { | ||
t.plan(1); | ||
const breaker = opossum(passFail, { enabled: false }); | ||
t.equals(breaker.enabled, false); | ||
t.end(); | ||
}); | ||
|
||
test('When disabled the circuit should always be closed', t => { | ||
t.plan(8); | ||
const options = { | ||
errorThresholdPercentage: 1, | ||
resetTimeout: 1000 | ||
}; | ||
|
||
const breaker = opossum(passFail, options); | ||
breaker.disable(); | ||
breaker.fire(-1) | ||
.catch(e => t.equals(e, 'Error: -1 is < 0')) | ||
.then(() => { | ||
t.ok(breaker.closed, 'should be closed'); | ||
t.notOk(breaker.pendingClose, | ||
'should not be pending close'); | ||
}) | ||
.then(() => { | ||
breaker // fire and fail again | ||
.fire(-1) | ||
.catch(e => t.equals(e, 'Error: -1 is < 0')) | ||
.then(() => { | ||
t.ok(breaker.closed, 'should be closed'); | ||
t.notOk(breaker.pendingClose, | ||
'should not be pending close'); | ||
}); | ||
}) | ||
.then(() => { // reenable the circuit | ||
breaker.enable(); | ||
breaker.fire(-1) | ||
.catch(e => t.equals(e, 'Error: -1 is < 0')) | ||
.then(() => { | ||
t.ok(breaker.opened, 'should be closed'); | ||
}); | ||
}); | ||
}); |