2
2
3
3
import java .util .Collections ;
4
4
import java .util .List ;
5
+ import java .util .Map ;
5
6
import java .util .Random ;
6
7
import java .util .concurrent .TimeUnit ;
7
- import java .util .function .BiConsumer ;
8
8
import java .util .function .Supplier ;
9
9
10
10
import dev .openfeature .contrib .providers .flagd .FlagdOptions ;
14
14
import dev .openfeature .flagd .grpc .evaluation .Evaluation .EventStreamRequest ;
15
15
import dev .openfeature .flagd .grpc .evaluation .Evaluation .EventStreamResponse ;
16
16
import dev .openfeature .flagd .grpc .evaluation .ServiceGrpc ;
17
+ import dev .openfeature .sdk .internal .TriConsumer ;
17
18
import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
18
19
import io .grpc .ManagedChannel ;
19
20
import io .grpc .stub .StreamObserver ;
@@ -37,7 +38,7 @@ public class GrpcConnector {
37
38
private final long deadline ;
38
39
39
40
private final Cache cache ;
40
- private final BiConsumer <Boolean , List <String >> stateConsumer ;
41
+ private final TriConsumer <Boolean , List <String >, Map < String , Object >> onConnectionEvent ;
41
42
private final Supplier <Boolean > connectedSupplier ;
42
43
43
44
private int eventStreamAttempt = 1 ;
@@ -48,23 +49,23 @@ public class GrpcConnector {
48
49
49
50
/**
50
51
* GrpcConnector creates an abstraction over gRPC communication.
51
- *
52
- * @param options options to build the gRPC channel.
53
- * @param cache cache to use.
54
- * @param stateConsumer lambda to call for setting the state.
52
+ *
53
+ * @param options flagd options
54
+ * @param cache cache to use
55
+ * @param connectedSupplier lambda providing current connection status from caller
56
+ * @param onConnectionEvent lambda which handles changes in the connection/stream
55
57
*/
56
58
public GrpcConnector (final FlagdOptions options , final Cache cache , final Supplier <Boolean > connectedSupplier ,
57
- BiConsumer <Boolean , List <String >> stateConsumer ) {
59
+ TriConsumer <Boolean , List <String >, Map < String , Object >> onConnectionEvent ) {
58
60
this .channel = ChannelBuilder .nettyChannel (options );
59
61
this .serviceStub = ServiceGrpc .newStub (channel );
60
62
this .serviceBlockingStub = ServiceGrpc .newBlockingStub (channel );
61
-
62
63
this .maxEventStreamRetries = options .getMaxEventStreamRetries ();
63
64
this .startEventStreamRetryBackoff = options .getRetryBackoffMs ();
64
65
this .eventStreamRetryBackoff = options .getRetryBackoffMs ();
65
66
this .deadline = options .getDeadline ();
66
67
this .cache = cache ;
67
- this .stateConsumer = stateConsumer ;
68
+ this .onConnectionEvent = onConnectionEvent ;
68
69
this .connectedSupplier = connectedSupplier ;
69
70
}
70
71
@@ -104,7 +105,7 @@ public void shutdown() throws Exception {
104
105
this .channel .awaitTermination (this .deadline , TimeUnit .MILLISECONDS );
105
106
log .warn (String .format ("Unable to shut down channel by %d deadline" , this .deadline ));
106
107
}
107
- this .stateConsumer .accept (false , Collections .emptyList ());
108
+ this .onConnectionEvent .accept (false , Collections .emptyList (), Collections . emptyMap ());
108
109
}
109
110
}
110
111
@@ -124,7 +125,7 @@ public ServiceGrpc.ServiceBlockingStub getResolver() {
124
125
private void observeEventStream () {
125
126
while (this .eventStreamAttempt <= this .maxEventStreamRetries ) {
126
127
final StreamObserver <EventStreamResponse > responseObserver = new EventStreamObserver (sync , this .cache ,
127
- this ::grpcStateConsumer );
128
+ this ::grpconConnectionEvent );
128
129
this .serviceStub .eventStream (EventStreamRequest .getDefaultInstance (), responseObserver );
129
130
130
131
try {
@@ -155,16 +156,16 @@ private void observeEventStream() {
155
156
}
156
157
157
158
log .error ("failed to connect to event stream, exhausted retries" );
158
- this .grpcStateConsumer (false , null );
159
+ this .grpconConnectionEvent (false , Collections . emptyList () );
159
160
}
160
161
161
- private void grpcStateConsumer (final boolean connected , final List <String > changedFlags ) {
162
+ private void grpconConnectionEvent (final boolean connected , final List <String > changedFlags ) {
162
163
// reset reconnection states
163
164
if (connected ) {
164
165
this .eventStreamAttempt = 1 ;
165
166
this .eventStreamRetryBackoff = this .startEventStreamRetryBackoff ;
166
167
}
167
168
// chain to initiator
168
- this .stateConsumer .accept (connected , changedFlags );
169
+ this .onConnectionEvent .accept (connected , changedFlags , Collections . emptyMap () );
169
170
}
170
171
}
0 commit comments