hapi-brakes
Plugin to simplify the circuit breaker system using Brakes library inside an Hapi.js environment.
To learn more about circuit breaker system, see Netflix Hystrix Wiki
There are other Hystrix's implementation in Node.js like HystrixJS
Settings
When registering the plugin, you can give custom settings or using the default values :
Default values
this settings that will be the default value for each circuitBreaker :
Variable | Type | Default Value | Documentation |
---|
name | String | defaultBrake | hystrix command name, must be unique for each circuit within the same group. |
group | String | defaultBrakeGroup | Hystrix command group, used to group different command |
timeout | Number | 15000 | Time in ms before a service call will timeout |
threshold | Float | 0.5 | % threshold for successful calls. If the % of successful calls dips below this threshold the circuit will break |
waitThreshold | Number | 100 | Number of requests to wait before testing circuit health |
statInterval | Number | 1200 | Interval in ms that brakes should emit a snapshot event |
healthCheckInterval | Number | 5000 | Time in ms interval between each execution of health check function |
bucketSpan | Number | 1000 | Time in ms that a specific bucket should remain active |
bucketNum | Number | 60 | Number of buckets to retain in a rolling window |
percentiles | Number[] | [0.0, 0.25, 0.5, 0.75, 0.9, 0.95, 0.99, 0.995, 1] | Array that defines the percentile levels that should be calculated on the stats object (i.e. 0.9 for 90th percentile) |
circuitDuration | Number | 30000 | Time in ms that a circuit should remain broken |
isFailure | Function | null | Function used to determine if the function failed or not, must return a Boolean. |
healthCheck | Function | null | Function used to determine if the service is now available, must return a Promise. |
fallback | Function | null | Function executed if the service failed to deliver data, must return a Promise. |
isPromise | Boolean | false | to opt out of check for callback in function. This affects the passed in function, health check and fallback |
isFunction | Boolean | false | to opt out of check for callback, always promisifying in function. This affects the passed in function, health check and fallback |
Since it's a wrapper you can set the config as described in Brakes documentation
PS : i haven't tried the isPromise
and isFunction
option yet
Hystrix Route
You can enable/disable the Hystrix SSE Endpoint by setting the hystrixRoute
when registering the plugin.
It's a basic Hapi endpoint (see Hapi route documentation).
By passing hystrixRoute
as false
you'll disable the Hystrix route.
The default value is :
hystrixRoute : {
method : 'GET',
path : '/hystrix',
config : {
tags : [ 'api', 'hystrix' ],
}
}
Warning, if you are changing this value, keep in mind that i'll override your method
value to GET
and the handler
or the Hystrix Dashboard will not recognize the SSE stream.
Example of config
config = {
name : 'test',
group : 'groupTest',
timeout : 150000,
threshold : 1
hystrixRoute : {
method : 'GET',
path : '/hystrix',
config : {
tags : [ 'someOtherFunnyTag' ]
}
}
}
Usage
The plugin will decorate Server
with a circuitBreaker
object that contains a wrap
function and a circuits
object.
wrap
will return the Circuit
object and add it to the server.circuitBreaker.circuits
listing.
server.circuitBreaker = {
wrap: [Function],
circuits: {
'The name of the group': {
'The name of the first circuit breaker': [Circuit],
'The name of the second circuit breaker': [Circuit]
}
}
}
Reusing wrap
with the same name and group as an other circuit will override the first one.
Examples
Wrapping a function inside a Circuit :
var circuit = server.circuitBreaker.wrap({
name : 'CircuitBreakerName',
group : 'CircuitBreakerGroup',
timeout : 2000,
threshold : 0.66,
run : (data) => {
return new Promise((fulfill, reject) => {
request(data, (err, result) => {
if (err || result.statusCode != 200) {
reject(err);
return;
}
fulfill(result);
});
});
},
isFailure : (err) => {
return true;
},
healthCheck : () => {
return new Promise((fulfill, reject) => {
}
},
fallback : () => {
return new Promise((resolve) => {
resolve( 'something you want when the circuit is open, like asking a cache or something like that' );
});
}
});
Executing the function inside the circuit :
circuit.exec({
uri : 'http://0.0.0.0:' + '8080' + '/users/' + 5,
json : true,
time : true
}).then((data) => {
}).catch((err) => {
});
You can use the server.circuitBreaker.circuits
to get the Circuit object :
server.circuitBreaker.circuits.CircuitBreakerGroup.CircuitBreakerName.exec({
uri : 'http://0.0.0.0:' + '8080' + '/users/' + 6,
json : true,
time : true
}).then((data) => {
}).catch((err) => {
});