|
1 | 1 | package org.hypertrace.circuitbreaker.grpcutils;
|
2 | 2 |
|
3 |
| -import io.github.resilience4j.circuitbreaker.CircuitBreaker; |
4 |
| -import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry; |
5 | 3 | import io.grpc.CallOptions;
|
6 | 4 | import io.grpc.Channel;
|
7 | 5 | import io.grpc.ClientCall;
|
8 | 6 | import io.grpc.ClientInterceptor;
|
9 |
| -import io.grpc.ForwardingClientCall; |
10 |
| -import io.grpc.ForwardingClientCallListener; |
11 |
| -import io.grpc.Metadata; |
12 | 7 | import io.grpc.MethodDescriptor;
|
13 |
| -import io.grpc.Status; |
14 |
| -import java.util.concurrent.TimeUnit; |
15 |
| -import lombok.extern.slf4j.Slf4j; |
16 | 8 |
|
17 |
| -@Slf4j |
18 |
| -public class CircuitBreakerInterceptor implements ClientInterceptor { |
19 |
| - |
20 |
| - public static final CallOptions.Key<String> CIRCUIT_BREAKER_KEY = |
21 |
| - CallOptions.Key.createWithDefault("circuitBreakerKey", "default"); |
22 |
| - private final CircuitBreakerRegistry circuitBreakerRegistry; |
23 |
| - private final CircuitBreakerConfigProvider circuitBreakerConfigProvider; |
24 |
| - private final CircuitBreakerMetricsNotifier circuitBreakerMetricsNotifier; |
25 |
| - |
26 |
| - public CircuitBreakerInterceptor( |
27 |
| - CircuitBreakerRegistry circuitBreakerRegistry, |
28 |
| - CircuitBreakerConfigProvider circuitBreakerConfigProvider, |
29 |
| - CircuitBreakerMetricsNotifier circuitBreakerMetricsNotifier) { |
30 |
| - this.circuitBreakerRegistry = circuitBreakerRegistry; |
31 |
| - this.circuitBreakerConfigProvider = circuitBreakerConfigProvider; |
32 |
| - this.circuitBreakerMetricsNotifier = circuitBreakerMetricsNotifier; |
33 |
| - } |
34 |
| - |
35 |
| - // Intercepts the call and applies circuit breaker logic |
| 9 | +public abstract class CircuitBreakerInterceptor implements ClientInterceptor { |
36 | 10 | @Override
|
37 | 11 | public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
|
38 | 12 | MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
|
39 |
| - if (!circuitBreakerConfigProvider.isCircuitBreakerEnabled()) { |
| 13 | + if (!isCircuitBreakerEnabled()) { |
40 | 14 | return next.newCall(method, callOptions);
|
41 | 15 | }
|
42 |
| - |
43 |
| - // Get circuit breaker key from CallOptions |
44 |
| - String circuitBreakerKey = callOptions.getOption(CIRCUIT_BREAKER_KEY); |
45 |
| - CircuitBreaker circuitBreaker = getCircuitBreaker(circuitBreakerKey); |
46 |
| - return new ForwardingClientCall.SimpleForwardingClientCall<>( |
47 |
| - next.newCall(method, callOptions)) { |
48 |
| - @Override |
49 |
| - public void start(Listener<RespT> responseListener, Metadata headers) { |
50 |
| - long startTime = System.nanoTime(); |
51 |
| - |
52 |
| - // Wrap response listener to track failures |
53 |
| - Listener<RespT> wrappedListener = |
54 |
| - new ForwardingClientCallListener.SimpleForwardingClientCallListener<>( |
55 |
| - responseListener) { |
56 |
| - @Override |
57 |
| - public void onClose(Status status, Metadata trailers) { |
58 |
| - long duration = System.nanoTime() - startTime; |
59 |
| - if (status.isOk()) { |
60 |
| - circuitBreaker.onSuccess(duration, TimeUnit.NANOSECONDS); |
61 |
| - } else { |
62 |
| - log.debug( |
63 |
| - "Circuit Breaker '{}' detected failure. Status: {}, Description: {}", |
64 |
| - circuitBreaker.getName(), |
65 |
| - status.getCode(), |
66 |
| - status.getDescription()); |
67 |
| - circuitBreaker.onError( |
68 |
| - duration, TimeUnit.NANOSECONDS, status.asRuntimeException()); |
69 |
| - } |
70 |
| - super.onClose(status, trailers); |
71 |
| - } |
72 |
| - }; |
73 |
| - |
74 |
| - super.start(wrappedListener, headers); |
75 |
| - } |
76 |
| - |
77 |
| - @Override |
78 |
| - public void sendMessage(ReqT message) { |
79 |
| - if (!circuitBreaker.tryAcquirePermission()) { |
80 |
| - handleCircuitBreakerRejection(circuitBreakerKey, circuitBreaker); |
81 |
| - String rejectionReason = |
82 |
| - circuitBreaker.getState() == CircuitBreaker.State.HALF_OPEN |
83 |
| - ? "Circuit Breaker is HALF-OPEN and rejecting excess requests" |
84 |
| - : "Circuit Breaker is OPEN and blocking requests"; |
85 |
| - throw Status.UNAVAILABLE.withDescription(rejectionReason).asRuntimeException(); |
86 |
| - } |
87 |
| - super.sendMessage(message); |
88 |
| - } |
89 |
| - }; |
90 |
| - } |
91 |
| - |
92 |
| - private void handleCircuitBreakerRejection( |
93 |
| - String circuitBreakerKey, CircuitBreaker circuitBreaker) { |
94 |
| - String tenantId = getTenantId(circuitBreakerKey); |
95 |
| - if (circuitBreaker.getState() == CircuitBreaker.State.HALF_OPEN) { |
96 |
| - circuitBreakerMetricsNotifier.incrementCount(tenantId, "circuitbreaker.halfopen.rejected"); |
97 |
| - log.debug( |
98 |
| - "Circuit Breaker '{}' is HALF-OPEN and rejecting excess requests for tenant '{}'.", |
99 |
| - circuitBreakerKey, |
100 |
| - tenantId); |
101 |
| - } else if (circuitBreaker.getState() == CircuitBreaker.State.OPEN) { |
102 |
| - circuitBreakerMetricsNotifier.incrementCount(tenantId, "circuitbreaker.open.blocked"); |
103 |
| - log.debug( |
104 |
| - "Circuit Breaker '{}' is OPEN. Blocking request for tenant '{}'.", |
105 |
| - circuitBreakerKey, |
106 |
| - tenantId); |
107 |
| - } else { |
108 |
| - log.debug( // Added unexpected state handling for safety |
109 |
| - "Unexpected Circuit Breaker state '{}' for '{}'. Blocking request.", |
110 |
| - circuitBreaker.getState(), |
111 |
| - circuitBreakerKey); |
112 |
| - } |
| 16 | + return createInterceptedCall(method, callOptions, next); |
113 | 17 | }
|
114 | 18 |
|
115 |
| - private static String getTenantId(String circuitBreakerKey) { |
116 |
| - if (!circuitBreakerKey.contains(".")) { |
117 |
| - return "Unknown"; |
118 |
| - } |
119 |
| - return circuitBreakerKey.split("\\.", 2)[0]; // Ensures only the first split |
120 |
| - } |
| 19 | + protected abstract boolean isCircuitBreakerEnabled(); |
121 | 20 |
|
122 |
| - /** Retrieve the Circuit Breaker based on the key. */ |
123 |
| - private CircuitBreaker getCircuitBreaker(String circuitBreakerKey) { |
124 |
| - CircuitBreaker circuitBreaker = |
125 |
| - circuitBreakerRegistry.circuitBreaker( |
126 |
| - circuitBreakerKey, circuitBreakerConfigProvider.getConfig(circuitBreakerKey)); |
127 |
| - CircuitBreakerEventListener.attachListeners(circuitBreaker); |
128 |
| - return circuitBreaker; |
129 |
| - } |
| 21 | + protected abstract <ReqT, RespT> ClientCall<ReqT, RespT> createInterceptedCall( |
| 22 | + MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next); |
130 | 23 | }
|
0 commit comments