|
4 | 4 | import io.grpc.ConnectivityState; |
5 | 5 | import io.grpc.ManagedChannel; |
6 | 6 | import java.util.concurrent.CountDownLatch; |
7 | | -import java.util.concurrent.ScheduledExecutorService; |
8 | | -import java.util.concurrent.ScheduledFuture; |
9 | 7 | import java.util.concurrent.TimeUnit; |
10 | | -import java.util.concurrent.atomic.AtomicReference; |
11 | 8 | import lombok.extern.slf4j.Slf4j; |
12 | 9 |
|
13 | 10 | /** |
@@ -98,78 +95,4 @@ private static void waitForDesiredState( |
98 | 95 | "Deadline exceeded. Condition did not complete within the %d " + "deadline", timeout)); |
99 | 96 | } |
100 | 97 | } |
101 | | - |
102 | | - /** |
103 | | - * Polls the state of a gRPC channel at regular intervals and triggers callbacks upon state changes. |
104 | | - * |
105 | | - * @param executor the ScheduledExecutorService used for polling. |
106 | | - * @param channel the ManagedChannel to monitor. |
107 | | - * @param onConnectionReady callback invoked when the channel transitions to a READY state. |
108 | | - * @param onConnectionLost callback invoked when the channel transitions to a FAILURE or SHUTDOWN state. |
109 | | - * @param pollIntervalMs the polling interval in milliseconds. |
110 | | - */ |
111 | | - public static void pollChannelState( |
112 | | - ScheduledExecutorService executor, |
113 | | - ManagedChannel channel, |
114 | | - Runnable onConnectionReady, |
115 | | - Runnable onConnectionLost, |
116 | | - long pollIntervalMs) { |
117 | | - |
118 | | - AtomicReference<ConnectivityState> lastState = new AtomicReference<>(ConnectivityState.READY); |
119 | | - |
120 | | - Runnable pollTask = () -> { |
121 | | - ConnectivityState currentState = channel.getState(true); |
122 | | - if (currentState != lastState.get()) { |
123 | | - if (currentState == ConnectivityState.READY) { |
124 | | - log.debug("gRPC connection became READY"); |
125 | | - onConnectionReady.run(); |
126 | | - } else if (currentState == ConnectivityState.TRANSIENT_FAILURE |
127 | | - || currentState == ConnectivityState.SHUTDOWN) { |
128 | | - log.debug("gRPC connection became TRANSIENT_FAILURE"); |
129 | | - onConnectionLost.run(); |
130 | | - } |
131 | | - lastState.set(currentState); |
132 | | - } |
133 | | - }; |
134 | | - executor.scheduleAtFixedRate(pollTask, 0, pollIntervalMs, TimeUnit.MILLISECONDS); |
135 | | - } |
136 | | - |
137 | | - /** |
138 | | - * Polls the channel state at fixed intervals and waits for the channel to reach a desired state within a timeout |
139 | | - * period. |
140 | | - * |
141 | | - * @param executor the ScheduledExecutorService used for polling. |
142 | | - * @param channel the ManagedChannel to monitor. |
143 | | - * @param desiredState the ConnectivityState to wait for. |
144 | | - * @param connectCallback callback invoked when the desired state is reached. |
145 | | - * @param timeout the maximum amount of time to wait. |
146 | | - * @param unit the time unit of the timeout. |
147 | | - * @return {@code true} if the desired state was reached within the timeout period, {@code false} otherwise. |
148 | | - * @throws InterruptedException if the current thread is interrupted while waiting. |
149 | | - */ |
150 | | - public static boolean pollForDesiredState( |
151 | | - ScheduledExecutorService executor, |
152 | | - ManagedChannel channel, |
153 | | - ConnectivityState desiredState, |
154 | | - Runnable connectCallback, |
155 | | - long timeout, |
156 | | - TimeUnit unit) |
157 | | - throws InterruptedException { |
158 | | - CountDownLatch latch = new CountDownLatch(1); |
159 | | - |
160 | | - Runnable waitForStateTask = () -> { |
161 | | - ConnectivityState currentState = channel.getState(true); |
162 | | - if (currentState == desiredState) { |
163 | | - connectCallback.run(); |
164 | | - latch.countDown(); |
165 | | - } |
166 | | - }; |
167 | | - |
168 | | - ScheduledFuture<?> scheduledFuture = |
169 | | - executor.scheduleWithFixedDelay(waitForStateTask, 0, 100, TimeUnit.MILLISECONDS); |
170 | | - |
171 | | - boolean success = latch.await(timeout, unit); |
172 | | - scheduledFuture.cancel(true); |
173 | | - return success; |
174 | | - } |
175 | 98 | } |
0 commit comments