1
1
package org .hypertrace .circuitbreaker .grpcutils ;
2
2
3
3
import com .typesafe .config .Config ;
4
- import java .time .Duration ;
5
4
import java .util .Map ;
6
5
import java .util .Set ;
7
6
import java .util .stream .Collectors ;
10
9
@ Slf4j
11
10
public class CircuitBreakerConfigParser {
12
11
13
- private static final Set <String > nonThresholdKeys =
14
- Set .of ("enabled" , "defaultCircuitBreakerKey" , "defaultThresholds" );
15
-
16
12
// Percentage of failures to trigger OPEN state
17
13
private static final String FAILURE_RATE_THRESHOLD = "failureRateThreshold" ;
18
14
// Percentage of slow calls to trigger OPEN state
@@ -32,6 +28,7 @@ public class CircuitBreakerConfigParser {
32
28
private static final String SLIDING_WINDOW_TYPE = "slidingWindowType" ;
33
29
public static final String ENABLED = "enabled" ;
34
30
public static final String DEFAULT_THRESHOLDS = "defaultThresholds" ;
31
+ private static final Set <String > NON_THRESHOLD_KEYS = Set .of (ENABLED , DEFAULT_THRESHOLDS );
35
32
36
33
public static <T > CircuitBreakerConfiguration .CircuitBreakerConfigurationBuilder <T > parseConfig (
37
34
Config config ) {
@@ -50,7 +47,7 @@ public static <T> CircuitBreakerConfiguration.CircuitBreakerConfigurationBuilder
50
47
51
48
Map <String , CircuitBreakerThresholds > circuitBreakerThresholdsMap =
52
49
config .root ().keySet ().stream ()
53
- .filter (key -> !nonThresholdKeys .contains (key )) // Filter out non-threshold keys
50
+ .filter (key -> !NON_THRESHOLD_KEYS .contains (key )) // Filter out non-threshold keys
54
51
.collect (
55
52
Collectors .toMap (
56
53
key -> key , // Circuit breaker key
@@ -61,30 +58,47 @@ public static <T> CircuitBreakerConfiguration.CircuitBreakerConfigurationBuilder
61
58
}
62
59
63
60
private static CircuitBreakerThresholds buildCircuitBreakerThresholds (Config config ) {
64
- return CircuitBreakerThresholds .builder ()
65
- .failureRateThreshold ((float ) config .getDouble (FAILURE_RATE_THRESHOLD ))
66
- .slowCallRateThreshold ((float ) config .getDouble (SLOW_CALL_RATE_THRESHOLD ))
67
- .slowCallDurationThreshold (config .getDuration (SLOW_CALL_DURATION_THRESHOLD ))
68
- .slidingWindowType (getSlidingWindowType (config .getString (SLIDING_WINDOW_TYPE )))
69
- .slidingWindowSize (config .getInt (SLIDING_WINDOW_SIZE ))
70
- .waitDurationInOpenState (config .getDuration (WAIT_DURATION_IN_OPEN_STATE ))
71
- .permittedNumberOfCallsInHalfOpenState (
72
- config .getInt (PERMITTED_NUMBER_OF_CALLS_IN_HALF_OPEN_STATE ))
73
- .minimumNumberOfCalls (config .getInt (MINIMUM_NUMBER_OF_CALLS ))
74
- .build ();
61
+ CircuitBreakerThresholds .CircuitBreakerThresholdsBuilder builder =
62
+ CircuitBreakerThresholds .builder ();
63
+
64
+ if (config .hasPath (FAILURE_RATE_THRESHOLD )) {
65
+ builder .failureRateThreshold ((float ) config .getDouble (FAILURE_RATE_THRESHOLD ));
66
+ }
67
+
68
+ if (config .hasPath (SLOW_CALL_RATE_THRESHOLD )) {
69
+ builder .slowCallRateThreshold ((float ) config .getDouble (SLOW_CALL_RATE_THRESHOLD ));
70
+ }
71
+
72
+ if (config .hasPath (SLOW_CALL_DURATION_THRESHOLD )) {
73
+ builder .slowCallDurationThreshold (config .getDuration (SLOW_CALL_DURATION_THRESHOLD ));
74
+ }
75
+
76
+ if (config .hasPath (SLIDING_WINDOW_TYPE )) {
77
+ builder .slidingWindowType (getSlidingWindowType (config .getString (SLIDING_WINDOW_TYPE )));
78
+ }
79
+
80
+ if (config .hasPath (SLIDING_WINDOW_SIZE )) {
81
+ builder .slidingWindowSize (config .getInt (SLIDING_WINDOW_SIZE ));
82
+ }
83
+
84
+ if (config .hasPath (WAIT_DURATION_IN_OPEN_STATE )) {
85
+ builder .waitDurationInOpenState (config .getDuration (WAIT_DURATION_IN_OPEN_STATE ));
86
+ }
87
+
88
+ if (config .hasPath (PERMITTED_NUMBER_OF_CALLS_IN_HALF_OPEN_STATE )) {
89
+ builder .permittedNumberOfCallsInHalfOpenState (
90
+ config .getInt (PERMITTED_NUMBER_OF_CALLS_IN_HALF_OPEN_STATE ));
91
+ }
92
+
93
+ if (config .hasPath (MINIMUM_NUMBER_OF_CALLS )) {
94
+ builder .minimumNumberOfCalls (config .getInt (MINIMUM_NUMBER_OF_CALLS ));
95
+ }
96
+
97
+ return builder .build ();
75
98
}
76
99
77
100
public static CircuitBreakerThresholds buildCircuitBreakerDefaultThresholds () {
78
- return CircuitBreakerThresholds .builder ()
79
- .failureRateThreshold (50f )
80
- .slowCallRateThreshold (50f )
81
- .slowCallDurationThreshold (Duration .ofSeconds (2 ))
82
- .slidingWindowType (CircuitBreakerThresholds .SlidingWindowType .TIME_BASED )
83
- .slidingWindowSize (60 )
84
- .waitDurationInOpenState (Duration .ofSeconds (60 ))
85
- .permittedNumberOfCallsInHalfOpenState (5 )
86
- .minimumNumberOfCalls (10 )
87
- .build ();
101
+ return CircuitBreakerThresholds .builder ().build ();
88
102
}
89
103
90
104
private static CircuitBreakerThresholds .SlidingWindowType getSlidingWindowType (
0 commit comments