1
1
package dev .openfeature .contrib .providers .flagd .resolver .grpc ;
2
2
3
3
import com .google .protobuf .Value ;
4
+ import dev .openfeature .contrib .providers .flagd .resolver .common .ConnectionEvent ;
4
5
import dev .openfeature .contrib .providers .flagd .resolver .grpc .cache .Cache ;
5
6
import dev .openfeature .flagd .grpc .evaluation .Evaluation .EventStreamResponse ;
6
7
import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
10
11
import java .util .List ;
11
12
import java .util .Map ;
12
13
import java .util .function .BiConsumer ;
14
+ import java .util .function .Consumer ;
13
15
import lombok .extern .slf4j .Slf4j ;
14
16
15
17
/**
20
22
@ SuppressFBWarnings (justification = "cache needs to be read and write by multiple objects" )
21
23
class EventStreamObserver implements StreamObserver <EventStreamResponse > {
22
24
23
- /**
24
- * A consumer to handle connection events with a flag indicating success and a list of changed flags.
25
- */
26
- private final BiConsumer <Boolean , List <String >> onConnectionEvent ;
27
25
28
- /**
29
- * The cache to update based on received events.
30
- */
31
- private final Cache cache ;
26
+ private final Consumer <List <String >> onConfigurationChange ;
27
+ private final Consumer <ConnectionEvent > onReady ;
32
28
33
29
/**
34
30
* Constructs a new {@code EventStreamObserver} instance.
35
31
*
36
- * @param cache the cache to update based on received events
37
32
* @param onConnectionEvent a consumer to handle connection events with a boolean and a list of changed flags
38
33
*/
39
- EventStreamObserver (Cache cache , BiConsumer < Boolean , List <String >> onConnectionEvent ) {
40
- this .cache = cache ;
41
- this .onConnectionEvent = onConnectionEvent ;
34
+ EventStreamObserver (Consumer < List <String >> onConfigurationChange , Consumer < ConnectionEvent > onReady ) {
35
+ this .onConfigurationChange = onConfigurationChange ;
36
+ this .onReady = onReady ;
42
37
}
43
38
44
39
/**
@@ -60,27 +55,14 @@ public void onNext(EventStreamResponse value) {
60
55
}
61
56
}
62
57
63
- /**
64
- * Called when an error occurs in the stream.
65
- *
66
- * @param throwable the error that occurred
67
- */
68
58
@ Override
69
59
public void onError (Throwable throwable ) {
70
- if (this .cache .getEnabled ().equals (Boolean .TRUE )) {
71
- this .cache .clear ();
72
- }
60
+
73
61
}
74
62
75
- /**
76
- * Called when the stream is completed.
77
- */
78
63
@ Override
79
64
public void onCompleted () {
80
- if (this .cache .getEnabled ().equals (Boolean .TRUE )) {
81
- this .cache .clear ();
82
- }
83
- this .onConnectionEvent .accept (false , Collections .emptyList ());
65
+
84
66
}
85
67
86
68
/**
@@ -90,33 +72,22 @@ public void onCompleted() {
90
72
*/
91
73
private void handleConfigurationChangeEvent (EventStreamResponse value ) {
92
74
List <String > changedFlags = new ArrayList <>();
93
- boolean cachingEnabled = this .cache .getEnabled ();
94
75
95
76
Map <String , Value > data = value .getData ().getFieldsMap ();
96
77
Value flagsValue = data .get (Constants .FLAGS_KEY );
97
- if (flagsValue == null ) {
98
- if (cachingEnabled ) {
99
- this .cache .clear ();
100
- }
101
- } else {
78
+ if (flagsValue != null ) {
102
79
Map <String , Value > flags = flagsValue .getStructValue ().getFieldsMap ();
103
- for (String flagKey : flags .keySet ()) {
104
- changedFlags .add (flagKey );
105
- if (cachingEnabled ) {
106
- this .cache .remove (flagKey );
107
- }
108
- }
80
+ changedFlags .addAll (flags .keySet ());
109
81
}
110
82
111
- this . onConnectionEvent . accept (true , changedFlags );
83
+ onConfigurationChange . accept (changedFlags );
112
84
}
113
85
114
86
/**
115
87
* Handles provider readiness events by clearing the cache (if enabled) and notifying listeners of readiness.
116
88
*/
117
89
private void handleProviderReadyEvent () {
118
- if (this .cache .getEnabled ().equals (Boolean .TRUE )) {
119
- this .cache .clear ();
120
- }
90
+ log .info ("Received provider ready event" );
91
+ onReady .accept (new ConnectionEvent (true ));
121
92
}
122
93
}
0 commit comments