34
34
import oteldemo .Demo .Ad ;
35
35
import oteldemo .Demo .AdRequest ;
36
36
import oteldemo .Demo .AdResponse ;
37
+ import oteldemo .problempattern .GarbageCollectionTrigger ;
37
38
import dev .openfeature .contrib .providers .flagd .FlagdOptions ;
38
39
import dev .openfeature .contrib .providers .flagd .FlagdProvider ;
39
40
import dev .openfeature .sdk .Client ;
@@ -127,6 +128,9 @@ private enum AdResponseType {
127
128
128
129
private static class AdServiceImpl extends oteldemo .AdServiceGrpc .AdServiceImplBase {
129
130
131
+ private static final String ADSERVICE_FAILURE = "adServiceFailure" ;
132
+ private static final String ADSERVICE_MANUAL_GC_FEATURE_FLAG = "adServiceManualGc" ;
133
+
130
134
private AdServiceImpl () {}
131
135
132
136
/**
@@ -177,8 +181,14 @@ public void getAds(AdRequest req, StreamObserver<AdResponse> responseObserver) {
177
181
Attributes .of (
178
182
adRequestTypeKey , adRequestType .name (), adResponseTypeKey , adResponseType .name ()));
179
183
180
- if (checkAdFailure ()) {
181
- throw new StatusRuntimeException (Status .RESOURCE_EXHAUSTED );
184
+ if (getFeatureFlagEnabled (ADSERVICE_FAILURE )) {
185
+ throw new StatusRuntimeException (Status .UNAVAILABLE );
186
+ }
187
+
188
+ if (getFeatureFlagEnabled (ADSERVICE_MANUAL_GC_FEATURE_FLAG )) {
189
+ logger .warn ("Feature Flag " + ADSERVICE_MANUAL_GC_FEATURE_FLAG + " enabled, performing a manual gc now" );
190
+ GarbageCollectionTrigger gct = new GarbageCollectionTrigger ();
191
+ gct .doExecute ();
182
192
}
183
193
184
194
AdResponse reply = AdResponse .newBuilder ().addAllAds (allAds ).build ();
@@ -193,12 +203,18 @@ public void getAds(AdRequest req, StreamObserver<AdResponse> responseObserver) {
193
203
}
194
204
}
195
205
196
- boolean checkAdFailure () {
206
+ /**
207
+ * Retrieves the status of a feature flag from the Feature Flag service.
208
+ *
209
+ * @param ff The name of the feature flag to retrieve.
210
+ * @return {@code true} if the feature flag is enabled, {@code false} otherwise or in case of errors.
211
+ */
212
+ boolean getFeatureFlagEnabled (String ff ) {
197
213
Client client = OpenFeatureAPI .getInstance ().getClient ();
198
214
// TODO: Plumb the actual session ID from the frontend via baggage?
199
215
UUID uuid = UUID .randomUUID ();
200
216
client .setEvaluationContext (new MutableContext ().add ("session" , uuid .toString ()));
201
- Boolean boolValue = client .getBooleanValue ("adServiceFailure" , false );
217
+ Boolean boolValue = client .getBooleanValue (ff , false );
202
218
return boolValue ;
203
219
}
204
220
}
0 commit comments