feathers-opossum
Advanced tools
Comparing version 0.2.0 to 1.0.0
@@ -32,5 +32,8 @@ const CircuitBreaker = require('opossum'); | ||
if (typeof options.fallback === 'function') breaker.fallback(options.fallback); | ||
Object.keys(options.events).forEach(e => { | ||
breaker.on(e, options.events[e]); | ||
}); | ||
return async (...params) => { | ||
// if (typeof options.fallback === 'function') breaker.fallback(options.fallback); | ||
// if (typeof options.onFallback === 'function') breaker.on('fallback', result => options.onFallback); | ||
const result = await breaker.fire(params); | ||
@@ -42,6 +45,7 @@ return result; | ||
circuitBreakerOptions(method, options) { | ||
const opt = options[method] || options; | ||
return { | ||
opossum: options.opossum || options[method].opossum || {}, | ||
fallback: options.fallback || options[method].fallback || false, | ||
onFallback: options.onFallback || options[method].onFallback || false | ||
opossum: opt.opossum || {}, | ||
fallback: opt.fallback || false, | ||
events: opt.events || {} | ||
}; | ||
@@ -48,0 +52,0 @@ } |
{ | ||
"name": "feathers-opossum", | ||
"description": "A service plugin for opossum", | ||
"version": "0.2.0", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/sajov/feathers-opossum", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -8,3 +8,3 @@ # feathers-opossum | ||
[feathers-opossum](https://github.com/sajov/feathers-opossum) is a circuit breaker for Feathers adapters. It implements the [opossum](https://github.com/nodeshift/opossum) module. | ||
[feathers-opossum](https://github.com/sajov/feathers-opossum) is a [circuit breaker](https://martinfowler.com/bliki/CircuitBreaker.html) for Feathers services. It implements the [opossum](https://github.com/nodeshift/opossum) module. | ||
@@ -17,4 +17,6 @@ ``` | ||
### `service(options)` | ||
### `service(Service, serviceOptions, opossumOptions)` | ||
Example: | ||
```javascript | ||
@@ -26,5 +28,5 @@ const service = require('service'); | ||
opossum: { | ||
timeout: 200, // If our function takes longer than 3 seconds, trigger a failure | ||
errorThresholdPercentage: 50, // When 50% of requests fail, trip the circuit | ||
resetTimeout: 30000 // After 30 seconds, try again. | ||
timeout: 3000, | ||
errorThresholdPercentage: 50, | ||
resetTimeout: 30000 | ||
}, | ||
@@ -34,5 +36,6 @@ fallback: () => { | ||
}, | ||
onFallback: result => reportFallbackEvent(result), | ||
// this means only find and get method relay on circur breaking | ||
methods: ['find', 'get'], // feel free to use updat, patch amd remove | ||
events: { | ||
fallback: result => reportFallbackEvent(result), | ||
} | ||
methods: ['find', 'get'] | ||
}; | ||
@@ -45,4 +48,51 @@ | ||
Options: | ||
- `Service` (**required**) - The main service | ||
- `opossum.timeout` (optional, default: 3000) - If our function takes longer than trigger a failure | ||
- `opossum.errorThresholdPercentage` (optional, default: 50) - When this ratio on requests fail, trip the circuit | ||
- `opossum.resetTimeout` (optional, default: 30000) - After this try again. | ||
- `fallback` - A fallback function that will be executed in the event of failure. | ||
- `onFallback` - A listener for the fallback event. | ||
Options per Service Method: | ||
```javascript | ||
const options = { | ||
find: { | ||
opossum: { | ||
timeout: 5000, | ||
errorThresholdPercentage: 50, | ||
resetTimeout: 30000 | ||
} | ||
fallback: () => { | ||
return 'Sorry, out of service right now'; | ||
}, | ||
events: { | ||
fallback: result => reportFallbackEvent(result), | ||
} | ||
}, | ||
get: { | ||
opossum: { | ||
timeout: 1000, | ||
errorThresholdPercentage: 50, | ||
resetTimeout: 30000 | ||
} | ||
fallback: () => { | ||
return 'Sorry, out of service right now'; | ||
}, | ||
events: { | ||
fallback: result => reportFallbackEvent(result), | ||
} | ||
} | ||
}; | ||
``` | ||
## Changelog | ||
**1.0.0** | ||
- implement opossum events | ||
- add event test | ||
**0.1.0** | ||
@@ -49,0 +99,0 @@ |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
9279
73
0
104