@@ -18,6 +18,7 @@ const HYSTRIX_STATS = Symbol('hystrix-stats');
18
18
const CACHE = new WeakMap ( ) ;
19
19
const ENABLED = Symbol ( 'Enabled' ) ;
20
20
const WARMING_UP = Symbol ( 'warming-up' ) ;
21
+ const VOLUME_THRESHOLD = Symbol ( 'volume-threshold' ) ;
21
22
const deprecation = `options.maxFailures is deprecated. \
22
23
Please use options.errorThresholdPercentage` ;
23
24
@@ -65,6 +66,11 @@ Please use options.errorThresholdPercentage`;
65
66
* allow before enabling the circuit. This can help in situations where no matter
66
67
* what your `errorThresholdPercentage` is, if the first execution times out or
67
68
* fails, the circuit immediately opens. Default: 0
69
+ * @param options.volumeThreshold {Number} the minimum number of requests within
70
+ * the rolling statistical window that must exist before the circuit breaker
71
+ * can open. This is similar to `options.allowWarmUp` in that no matter how many
72
+ * failures there are, if the number of requests within the statistical window
73
+ * does not exceed this threshold, the circuit will remain closed. Default: 0
68
74
*/
69
75
class CircuitBreaker extends EventEmitter {
70
76
constructor ( action , options ) {
@@ -78,6 +84,7 @@ class CircuitBreaker extends EventEmitter {
78
84
79
85
this . semaphore = new Semaphore ( this . options . capacity ) ;
80
86
87
+ this [ VOLUME_THRESHOLD ] = Number . isInteger ( options . volumeThreshold ) ? options . volumeThreshold : 0 ;
81
88
this [ WARMING_UP ] = options . allowWarmUp === true ;
82
89
this [ STATUS ] = new Status ( this . options ) ;
83
90
this [ STATE ] = CLOSED ;
@@ -246,6 +253,10 @@ class CircuitBreaker extends EventEmitter {
246
253
return this [ WARMING_UP ] ;
247
254
}
248
255
256
+ get volumeThreshold ( ) {
257
+ return this [ VOLUME_THRESHOLD ] ;
258
+ }
259
+
249
260
/**
250
261
* Provide a fallback function for this {@link CircuitBreaker}. This
251
262
* function will be executed when the circuit is `fire`d and fails.
@@ -511,6 +522,7 @@ function fail (circuit, err, args, latency) {
511
522
512
523
// check stats to see if the circuit should be opened
513
524
const stats = circuit . stats ;
525
+ if ( stats . fires < circuit . volumeThreshold ) return ;
514
526
const errorRate = stats . failures / stats . fires * 100 ;
515
527
if ( errorRate > circuit . options . errorThresholdPercentage ||
516
528
circuit . options . maxFailures >= stats . failures ||
0 commit comments