-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircuitBreakerConfigParser.java
111 lines (91 loc) · 4.41 KB
/
CircuitBreakerConfigParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package org.hypertrace.circuitbreaker.grpcutils;
import com.typesafe.config.Config;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class CircuitBreakerConfigParser {
// Percentage of failures to trigger OPEN state
private static final String FAILURE_RATE_THRESHOLD = "failureRateThreshold";
// Percentage of slow calls to trigger OPEN state
private static final String SLOW_CALL_RATE_THRESHOLD = "slowCallRateThreshold";
// Define what a "slow" call is
private static final String SLOW_CALL_DURATION_THRESHOLD = "slowCallDurationThreshold";
// Number of calls to consider in the sliding window
private static final String SLIDING_WINDOW_SIZE = "slidingWindowSize";
// Time before retrying after OPEN state
private static final String WAIT_DURATION_IN_OPEN_STATE = "waitDurationInOpenState";
// Minimum calls before evaluating failure rate
private static final String MINIMUM_NUMBER_OF_CALLS = "minimumNumberOfCalls";
// Calls allowed in HALF_OPEN state before deciding to
// CLOSE or OPEN again
private static final String PERMITTED_NUMBER_OF_CALLS_IN_HALF_OPEN_STATE =
"permittedNumberOfCallsInHalfOpenState";
private static final String SLIDING_WINDOW_TYPE = "slidingWindowType";
public static final String ENABLED = "enabled";
public static final String DEFAULT_THRESHOLDS = "defaultThresholds";
private static final Set<String> NON_THRESHOLD_KEYS = Set.of(ENABLED, DEFAULT_THRESHOLDS);
public static <T> CircuitBreakerConfiguration.CircuitBreakerConfigurationBuilder<T> parseConfig(
Config config) {
CircuitBreakerConfiguration.CircuitBreakerConfigurationBuilder<T> builder =
CircuitBreakerConfiguration.builder();
if (config.hasPath(ENABLED)) {
builder.enabled(config.getBoolean(ENABLED));
}
Map<String, CircuitBreakerThresholds> circuitBreakerThresholdsMap =
config.root().keySet().stream()
.filter(key -> !NON_THRESHOLD_KEYS.contains(key)) // Filter out non-threshold keys
.collect(
Collectors.toMap(
key -> key, // Circuit breaker key
key -> buildCircuitBreakerThresholds(config.getConfig(key))));
builder.defaultThresholds(
config.hasPath(DEFAULT_THRESHOLDS)
? buildCircuitBreakerThresholds(config.getConfig(DEFAULT_THRESHOLDS))
: buildCircuitBreakerDefaultThresholds());
builder.circuitBreakerThresholdsMap(circuitBreakerThresholdsMap);
log.debug("Loaded circuit breaker configs: {}", builder);
return builder;
}
private static CircuitBreakerThresholds buildCircuitBreakerThresholds(Config config) {
CircuitBreakerThresholds.CircuitBreakerThresholdsBuilder builder =
CircuitBreakerThresholds.builder();
if (config.hasPath(FAILURE_RATE_THRESHOLD)) {
builder.failureRateThreshold((float) config.getDouble(FAILURE_RATE_THRESHOLD));
}
if (config.hasPath(SLOW_CALL_RATE_THRESHOLD)) {
builder.slowCallRateThreshold((float) config.getDouble(SLOW_CALL_RATE_THRESHOLD));
}
if (config.hasPath(SLOW_CALL_DURATION_THRESHOLD)) {
builder.slowCallDurationThreshold(config.getDuration(SLOW_CALL_DURATION_THRESHOLD));
}
if (config.hasPath(SLIDING_WINDOW_TYPE)) {
builder.slidingWindowType(getSlidingWindowType(config.getString(SLIDING_WINDOW_TYPE)));
}
if (config.hasPath(SLIDING_WINDOW_SIZE)) {
builder.slidingWindowSize(config.getInt(SLIDING_WINDOW_SIZE));
}
if (config.hasPath(WAIT_DURATION_IN_OPEN_STATE)) {
builder.waitDurationInOpenState(config.getDuration(WAIT_DURATION_IN_OPEN_STATE));
}
if (config.hasPath(PERMITTED_NUMBER_OF_CALLS_IN_HALF_OPEN_STATE)) {
builder.permittedNumberOfCallsInHalfOpenState(
config.getInt(PERMITTED_NUMBER_OF_CALLS_IN_HALF_OPEN_STATE));
}
if (config.hasPath(MINIMUM_NUMBER_OF_CALLS)) {
builder.minimumNumberOfCalls(config.getInt(MINIMUM_NUMBER_OF_CALLS));
}
if (config.hasPath(ENABLED)) {
builder.enabled(config.getBoolean(ENABLED));
}
return builder.build();
}
public static CircuitBreakerThresholds buildCircuitBreakerDefaultThresholds() {
return CircuitBreakerThresholds.builder().build();
}
private static CircuitBreakerThresholds.SlidingWindowType getSlidingWindowType(
String slidingWindowType) {
return CircuitBreakerThresholds.SlidingWindowType.valueOf(slidingWindowType);
}
}