33import dev .openfeature .sdk .exceptions .GeneralError ;
44import io .grpc .ConnectivityState ;
55import io .grpc .ManagedChannel ;
6- import lombok .extern .slf4j .Slf4j ;
7-
86import java .util .concurrent .CountDownLatch ;
97import java .util .concurrent .ScheduledExecutorService ;
108import java .util .concurrent .ScheduledFuture ;
119import java .util .concurrent .TimeUnit ;
1210import java .util .concurrent .atomic .AtomicReference ;
13-
11+ import lombok . extern . slf4j . Slf4j ;
1412
1513/**
1614 * A utility class to monitor and manage the connectivity state of a gRPC ManagedChannel.
1715 */
1816@ Slf4j
1917public class ChannelMonitor {
2018
21-
22- private ChannelMonitor () {
23-
24- }
19+ private ChannelMonitor () {}
2520
2621 /**
2722 * Monitors the state of a gRPC channel and triggers the specified callbacks based on state changes.
@@ -31,8 +26,11 @@ private ChannelMonitor() {
3126 * @param onConnectionReady callback invoked when the channel transitions to a READY state.
3227 * @param onConnectionLost callback invoked when the channel transitions to a FAILURE or SHUTDOWN state.
3328 */
34- public static void monitorChannelState (ConnectivityState expectedState , ManagedChannel channel ,
35- Runnable onConnectionReady , Runnable onConnectionLost ) {
29+ public static void monitorChannelState (
30+ ConnectivityState expectedState ,
31+ ManagedChannel channel ,
32+ Runnable onConnectionReady ,
33+ Runnable onConnectionLost ) {
3634 channel .notifyWhenStateChanged (expectedState , () -> {
3735 ConnectivityState currentState = channel .getState (true );
3836 log .info ("Channel state changed to: {}" , currentState );
@@ -47,7 +45,6 @@ public static void monitorChannelState(ConnectivityState expectedState, ManagedC
4745 });
4846 }
4947
50-
5148 /**
5249 * Waits for the channel to reach a desired state within a specified timeout period.
5350 *
@@ -58,21 +55,24 @@ public static void monitorChannelState(ConnectivityState expectedState, ManagedC
5855 * @param unit the time unit of the timeout.
5956 * @throws InterruptedException if the current thread is interrupted while waiting.
6057 */
61- public static void waitForDesiredState (ManagedChannel channel ,
62- ConnectivityState desiredState ,
63- Runnable connectCallback ,
64- long timeout ,
65- TimeUnit unit ) throws InterruptedException {
58+ public static void waitForDesiredState (
59+ ManagedChannel channel ,
60+ ConnectivityState desiredState ,
61+ Runnable connectCallback ,
62+ long timeout ,
63+ TimeUnit unit )
64+ throws InterruptedException {
6665 waitForDesiredState (channel , desiredState , connectCallback , new CountDownLatch (1 ), timeout , unit );
6766 }
6867
69-
70- private static void waitForDesiredState (ManagedChannel channel ,
71- ConnectivityState desiredState ,
72- Runnable connectCallback ,
73- CountDownLatch latch ,
74- long timeout ,
75- TimeUnit unit ) throws InterruptedException {
68+ private static void waitForDesiredState (
69+ ManagedChannel channel ,
70+ ConnectivityState desiredState ,
71+ Runnable connectCallback ,
72+ CountDownLatch latch ,
73+ long timeout ,
74+ TimeUnit unit )
75+ throws InterruptedException {
7676 channel .notifyWhenStateChanged (ConnectivityState .SHUTDOWN , () -> {
7777 try {
7878 ConnectivityState state = channel .getState (true );
@@ -94,12 +94,11 @@ private static void waitForDesiredState(ManagedChannel channel,
9494
9595 // Await the latch or timeout for the state change
9696 if (!latch .await (timeout , unit )) {
97- throw new GeneralError (String .format ("Deadline exceeded. Condition did not complete within the %d "
98- + "deadline" , timeout ));
97+ throw new GeneralError (String .format (
98+ "Deadline exceeded. Condition did not complete within the %d " + "deadline" , timeout ));
9999 }
100100 }
101101
102-
103102 /**
104103 * Polls the state of a gRPC channel at regular intervals and triggers callbacks upon state changes.
105104 *
@@ -109,9 +108,12 @@ private static void waitForDesiredState(ManagedChannel channel,
109108 * @param onConnectionLost callback invoked when the channel transitions to a FAILURE or SHUTDOWN state.
110109 * @param pollIntervalMs the polling interval in milliseconds.
111110 */
112- public static void pollChannelState (ScheduledExecutorService executor , ManagedChannel channel ,
113- Runnable onConnectionReady ,
114- Runnable onConnectionLost , long pollIntervalMs ) {
111+ public static void pollChannelState (
112+ ScheduledExecutorService executor ,
113+ ManagedChannel channel ,
114+ Runnable onConnectionReady ,
115+ Runnable onConnectionLost ,
116+ long pollIntervalMs ) {
115117
116118 AtomicReference <ConnectivityState > lastState = new AtomicReference <>(ConnectivityState .READY );
117119
@@ -132,7 +134,6 @@ public static void pollChannelState(ScheduledExecutorService executor, ManagedCh
132134 executor .scheduleAtFixedRate (pollTask , 0 , pollIntervalMs , TimeUnit .MILLISECONDS );
133135 }
134136
135-
136137 /**
137138 * Polls the channel state at fixed intervals and waits for the channel to reach a desired state within a timeout
138139 * period.
@@ -146,9 +147,14 @@ public static void pollChannelState(ScheduledExecutorService executor, ManagedCh
146147 * @return {@code true} if the desired state was reached within the timeout period, {@code false} otherwise.
147148 * @throws InterruptedException if the current thread is interrupted while waiting.
148149 */
149- public static boolean pollForDesiredState (ScheduledExecutorService executor , ManagedChannel channel ,
150- ConnectivityState desiredState , Runnable connectCallback , long timeout ,
151- TimeUnit unit ) throws InterruptedException {
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 {
152158 CountDownLatch latch = new CountDownLatch (1 );
153159
154160 Runnable waitForStateTask = () -> {
@@ -159,8 +165,8 @@ public static boolean pollForDesiredState(ScheduledExecutorService executor, Man
159165 }
160166 };
161167
162- ScheduledFuture <?> scheduledFuture = executor . scheduleWithFixedDelay ( waitForStateTask , 0 , 100 ,
163- TimeUnit .MILLISECONDS );
168+ ScheduledFuture <?> scheduledFuture =
169+ executor . scheduleWithFixedDelay ( waitForStateTask , 0 , 100 , TimeUnit .MILLISECONDS );
164170
165171 boolean success = latch .await (timeout , unit );
166172 scheduledFuture .cancel (true );
0 commit comments