11package org .hypertrace .circuitbreaker .grpcutils ;
22
33import com .typesafe .config .Config ;
4- import java .time .Duration ;
54import java .util .Map ;
65import java .util .Set ;
76import java .util .stream .Collectors ;
109@ Slf4j
1110public class CircuitBreakerConfigParser {
1211
13- private static final Set <String > nonThresholdKeys =
14- Set .of ("enabled" , "defaultCircuitBreakerKey" , "defaultThresholds" );
15-
1612 // Percentage of failures to trigger OPEN state
1713 private static final String FAILURE_RATE_THRESHOLD = "failureRateThreshold" ;
1814 // Percentage of slow calls to trigger OPEN state
@@ -32,6 +28,7 @@ public class CircuitBreakerConfigParser {
3228 private static final String SLIDING_WINDOW_TYPE = "slidingWindowType" ;
3329 public static final String ENABLED = "enabled" ;
3430 public static final String DEFAULT_THRESHOLDS = "defaultThresholds" ;
31+ private static final Set <String > NON_THRESHOLD_KEYS = Set .of (ENABLED , DEFAULT_THRESHOLDS );
3532
3633 public static <T > CircuitBreakerConfiguration .CircuitBreakerConfigurationBuilder <T > parseConfig (
3734 Config config ) {
@@ -50,7 +47,7 @@ public static <T> CircuitBreakerConfiguration.CircuitBreakerConfigurationBuilder
5047
5148 Map <String , CircuitBreakerThresholds > circuitBreakerThresholdsMap =
5249 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
5451 .collect (
5552 Collectors .toMap (
5653 key -> key , // Circuit breaker key
@@ -61,30 +58,47 @@ public static <T> CircuitBreakerConfiguration.CircuitBreakerConfigurationBuilder
6158 }
6259
6360 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 ();
7598 }
7699
77100 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 ();
88102 }
89103
90104 private static CircuitBreakerThresholds .SlidingWindowType getSlidingWindowType (
0 commit comments