26
26
27
27
public class CircuitBreakerConfig {
28
28
29
- private static final int DEFAULT_MAX_FAILURE_THRESHOLD = 50 ; // Percentage
30
- private static final int DEFAULT_WAIT_DURATION_IN_OPEN_STATE = 60 ; // Seconds
31
- private static final int DEFAULT_RING_BUFFER_SIZE_IN_HALF_OPEN_STATE = 10 ;
32
- private static final int DEFAULT_RING_BUFFER_SIZE_IN_CLOSED_STATE = 100 ;
29
+ public static final int DEFAULT_MAX_FAILURE_THRESHOLD = 50 ; // Percentage
30
+ public static final int DEFAULT_WAIT_DURATION_IN_OPEN_STATE = 60 ; // Seconds
31
+ public static final int DEFAULT_RING_BUFFER_SIZE_IN_HALF_OPEN_STATE = 10 ;
32
+ public static final int DEFAULT_RING_BUFFER_SIZE_IN_CLOSED_STATE = 100 ;
33
+ public static final int DEFAULT_EXCEPTION_RING_BUFFER_SIZE = 10 ;
33
34
34
35
private final float failureRateThreshold ;
35
- private int ringBufferSizeInHalfOpenState ;
36
- private int ringBufferSizeInClosedState ;
36
+ private final int ringBufferSizeInHalfOpenState ;
37
+ private final int exceptionRingBufferSize ;
38
+ private final int ringBufferSizeInClosedState ;
37
39
private final Duration waitDurationInOpenState ;
38
40
private final CircuitBreakerEventListener circuitBreakerEventListener ;
39
41
private final Predicate <Throwable > exceptionPredicate ;
40
42
41
- private CircuitBreakerConfig (float failureRateThreshold ,
42
- Duration waitDurationInOpenState ,
43
- int ringBufferSizeInHalfOpenState ,
44
- int ringBufferSizeInClosedState ,
45
- Predicate <Throwable > exceptionPredicate ,
46
- CircuitBreakerEventListener circuitBreakerEventListener ){
47
- this .failureRateThreshold = failureRateThreshold ;
48
- this .waitDurationInOpenState = waitDurationInOpenState ;
49
- this .ringBufferSizeInHalfOpenState = ringBufferSizeInHalfOpenState ;
50
- this .ringBufferSizeInClosedState = ringBufferSizeInClosedState ;
51
- this .exceptionPredicate = exceptionPredicate ;
52
- this .circuitBreakerEventListener = circuitBreakerEventListener ;
43
+ private CircuitBreakerConfig (Context context ){
44
+ this .failureRateThreshold = context .failureRateThreshold ;
45
+ this .waitDurationInOpenState = context .waitDurationInOpenState ;
46
+ this .ringBufferSizeInHalfOpenState = context .ringBufferSizeInHalfOpenState ;
47
+ this .ringBufferSizeInClosedState = context .ringBufferSizeInClosedState ;
48
+ this .exceptionRingBufferSize = context .exceptionRingBufferSize ;
49
+ this .exceptionPredicate = context .exceptionPredicate ;
50
+ this .circuitBreakerEventListener = context .circuitBreakerEventListener ;
53
51
54
52
}
55
53
@@ -77,6 +75,10 @@ public Predicate<Throwable> getExceptionPredicate() {
77
75
return exceptionPredicate ;
78
76
}
79
77
78
+ public int getExceptionRingBufferSize () {
79
+ return exceptionRingBufferSize ;
80
+ }
81
+
80
82
/**
81
83
* Returns a builder to create a custom CircuitBreakerConfig.
82
84
*
@@ -95,15 +97,9 @@ public static CircuitBreakerConfig ofDefaults(){
95
97
return new Builder ().build ();
96
98
}
97
99
98
-
99
100
public static class Builder {
100
- private int failureRateThreshold = DEFAULT_MAX_FAILURE_THRESHOLD ;
101
- private int ringBufferSizeInHalfOpenState = DEFAULT_RING_BUFFER_SIZE_IN_HALF_OPEN_STATE ;
102
- private int ringBufferSizeInClosedState = DEFAULT_RING_BUFFER_SIZE_IN_CLOSED_STATE ;
103
- private Duration waitDurationInOpenState = Duration .ofSeconds (DEFAULT_WAIT_DURATION_IN_OPEN_STATE );
104
- private CircuitBreakerEventListener circuitBreakerEventListener = new DefaultCircuitBreakerEventListener ();
105
- // The default exception predicate counts all exceptions as failures.
106
- private Predicate <Throwable > exceptionPredicate = (exception ) -> true ;
101
+
102
+ private Context context = new Context ();
107
103
108
104
/**
109
105
* Configures the failure rate threshold in percentage above which the CircuitBreaker should trip open and start short-circuiting calls.
@@ -117,7 +113,7 @@ public Builder failureRateThreshold(int failureRateThreshold) {
117
113
if (failureRateThreshold < 1 || failureRateThreshold > 100 ) {
118
114
throw new IllegalArgumentException ("failureRateThreshold must be between 1 and 100" );
119
115
}
120
- this .failureRateThreshold = failureRateThreshold ;
116
+ context .failureRateThreshold = failureRateThreshold ;
121
117
return this ;
122
118
}
123
119
@@ -132,7 +128,7 @@ public Builder waitDurationInOpenState(Duration waitDurationInOpenState) {
132
128
if (waitDurationInOpenState .getSeconds () < 1 ) {
133
129
throw new IllegalArgumentException ("waitDurationInOpenState must be at least 1000[ms]" );
134
130
}
135
- this .waitDurationInOpenState = waitDurationInOpenState ;
131
+ context .waitDurationInOpenState = waitDurationInOpenState ;
136
132
return this ;
137
133
}
138
134
@@ -150,7 +146,7 @@ public Builder ringBufferSizeInHalfOpenState(int ringBufferSizeInHalfOpenState)
150
146
if (ringBufferSizeInHalfOpenState < 1 ) {
151
147
throw new IllegalArgumentException ("ringBufferSizeInHalfOpenState must be greater than 0" );
152
148
}
153
- this .ringBufferSizeInHalfOpenState = ringBufferSizeInHalfOpenState ;
149
+ context .ringBufferSizeInHalfOpenState = ringBufferSizeInHalfOpenState ;
154
150
return this ;
155
151
}
156
152
@@ -168,7 +164,23 @@ public Builder ringBufferSizeInClosedState(int ringBufferSizeInClosedState) {
168
164
if (ringBufferSizeInClosedState < 1 ) {
169
165
throw new IllegalArgumentException ("ringBufferSizeInClosedState must be greater than 0" );
170
166
}
171
- this .ringBufferSizeInClosedState = ringBufferSizeInClosedState ;
167
+ context .ringBufferSizeInClosedState = ringBufferSizeInClosedState ;
168
+ return this ;
169
+ }
170
+
171
+ /**
172
+ * Configures the size of the ring buffer which buffers the latest exceptions which are recorded as a failure and thus increase the failure rate.
173
+ *
174
+ * Default size is 10. A size of 0 disables buffering.
175
+ *
176
+ * @param exceptionRingBufferSize the size of the exception ring buffer.
177
+ * @return the CircuitBreakerConfig.Builder
178
+ */
179
+ public Builder exceptionRingBufferSize (int exceptionRingBufferSize ) {
180
+ if (exceptionRingBufferSize < 0 ) {
181
+ throw new IllegalArgumentException ("exceptionRingBufferSize must be greater than or equal to 0" );
182
+ }
183
+ context .exceptionRingBufferSize = exceptionRingBufferSize ;
172
184
return this ;
173
185
}
174
186
@@ -182,7 +194,7 @@ public Builder onCircuitBreakerEvent(CircuitBreakerEventListener circuitBreakerE
182
194
if (circuitBreakerEventListener == null ) {
183
195
throw new IllegalArgumentException ("circuitBreakerEventListener must not be null" );
184
196
}
185
- this .circuitBreakerEventListener = circuitBreakerEventListener ;
197
+ context .circuitBreakerEventListener = circuitBreakerEventListener ;
186
198
return this ;
187
199
}
188
200
@@ -194,7 +206,7 @@ public Builder onCircuitBreakerEvent(CircuitBreakerEventListener circuitBreakerE
194
206
* @return the CircuitBreakerConfig.Builder
195
207
*/
196
208
public Builder recordFailure (Predicate <Throwable > predicate ) {
197
- this .exceptionPredicate = predicate ;
209
+ context .exceptionPredicate = predicate ;
198
210
return this ;
199
211
}
200
212
@@ -204,13 +216,18 @@ public Builder recordFailure(Predicate<Throwable> predicate) {
204
216
* @return the CircuitBreakerConfig
205
217
*/
206
218
public CircuitBreakerConfig build () {
207
- return new CircuitBreakerConfig (
208
- failureRateThreshold ,
209
- waitDurationInOpenState ,
210
- ringBufferSizeInHalfOpenState ,
211
- ringBufferSizeInClosedState ,
212
- exceptionPredicate ,
213
- circuitBreakerEventListener );
219
+ return new CircuitBreakerConfig (context );
214
220
}
215
221
}
222
+
223
+ private static class Context {
224
+ float failureRateThreshold = DEFAULT_MAX_FAILURE_THRESHOLD ;
225
+ int ringBufferSizeInHalfOpenState = DEFAULT_RING_BUFFER_SIZE_IN_HALF_OPEN_STATE ;
226
+ int ringBufferSizeInClosedState = DEFAULT_RING_BUFFER_SIZE_IN_CLOSED_STATE ;
227
+ int exceptionRingBufferSize = DEFAULT_EXCEPTION_RING_BUFFER_SIZE ;
228
+ Duration waitDurationInOpenState = Duration .ofSeconds (DEFAULT_WAIT_DURATION_IN_OPEN_STATE );
229
+ CircuitBreakerEventListener circuitBreakerEventListener = new DefaultCircuitBreakerEventListener ();
230
+ // The default exception predicate counts all exceptions as failures.
231
+ Predicate <Throwable > exceptionPredicate = (exception ) -> true ;
232
+ }
216
233
}
0 commit comments