-
Notifications
You must be signed in to change notification settings - Fork 10
/
GBFeaturesRepository.java
491 lines (413 loc) · 17.9 KB
/
GBFeaturesRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package growthbook.sdk.java;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import lombok.Builder;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.sse.EventSource;
import okhttp3.sse.EventSourceListener;
import okhttp3.sse.EventSources;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
/**
* This class can be created with its `builder()` or constructor.
* It will fetch the features from the endpoint provided.
* Initialize with {@link GBFeaturesRepository#initialize()}
* Get the features JSON with {@link GBFeaturesRepository#getFeaturesJson()}.
* You would provide the features JSON when creating the {@link GBContext}
*/
@Slf4j
public class GBFeaturesRepository implements IGBFeaturesRepository {
@Getter
private final String featuresEndpoint;
@Getter
private final String eventsEndpoint;
@Getter
private FeatureRefreshStrategy refreshStrategy;
@Nullable
@Getter
private final String encryptionKey;
@Getter
private final Integer swrTtlSeconds;
@Getter
private Long expiresAt;
private final OkHttpClient okHttpClient;
@Nullable
private OkHttpClient sseHttpClient;
private final ArrayList<FeatureRefreshCallback> refreshCallbacks = new ArrayList<>();
private Boolean initialized = false;
private Boolean sseAllowed = false;
@Nullable
private Request sseRequest = null;
@Nullable
private EventSource sseEventSource = null;
/**
* Allows you to get the features JSON from the provided {@link GBFeaturesRepository#getFeaturesEndpoint()}.
* You must call {@link GBFeaturesRepository#initialize()} before calling this method
* or your features would not have loaded.
*/
private String featuresJson = "{}";
/**
* Create a new GBFeaturesRepository
*
* @param apiHost The GrowthBook API host (default: <a href="https://cdn.growthbook.io">...</a>)
* @param clientKey Your client ID, e.g. sdk-abc123
* @param encryptionKey optional key for decrypting encrypted payload
* @param swrTtlSeconds How often the cache should be invalidated when using {@link FeatureRefreshStrategy#STALE_WHILE_REVALIDATE} (default: 60)
*/
@Builder
public GBFeaturesRepository(
@Nullable String apiHost,
String clientKey,
@Nullable String encryptionKey,
@Nullable FeatureRefreshStrategy refreshStrategy,
@Nullable Integer swrTtlSeconds
) {
this(apiHost, clientKey, encryptionKey, refreshStrategy, swrTtlSeconds, null);
}
/**
* Create a new GBFeaturesRepository
*
* @param apiHost The GrowthBook API host (default: <a href="https://cdn.growthbook.io">...</a>)
* @param clientKey Your client ID, e.g. sdk-abc123
* @param encryptionKey optional key for decrypting encrypted payload
* @param swrTtlSeconds How often the cache should be invalidated when using {@link FeatureRefreshStrategy#STALE_WHILE_REVALIDATE} (default: 60)
* @param okHttpClient HTTP client (optional)
*/
public GBFeaturesRepository(
@Nullable String apiHost,
String clientKey,
@Nullable String encryptionKey,
@Nullable FeatureRefreshStrategy refreshStrategy,
@Nullable Integer swrTtlSeconds,
@Nullable OkHttpClient okHttpClient
) {
if (clientKey == null) throw new IllegalArgumentException("clientKey cannot be null");
// Set the defaults when the user does not provide them
if (apiHost == null) {
apiHost = "https://cdn.growthbook.io";
}
this.refreshStrategy = refreshStrategy == null ? FeatureRefreshStrategy.STALE_WHILE_REVALIDATE : refreshStrategy;
// Build the endpoints from the apiHost and clientKey
this.featuresEndpoint = apiHost + "/api/features/" + clientKey;
this.eventsEndpoint = apiHost + "/sub/" + clientKey;
this.encryptionKey = encryptionKey;
this.swrTtlSeconds = swrTtlSeconds == null ? 60 : swrTtlSeconds;
this.refreshExpiresAt();
// Use provided OkHttpClient or create a new one
if (okHttpClient == null) {
this.okHttpClient = this.initializeHttpClient();
} else {
// TODO: Check for valid interceptor
this.okHttpClient = okHttpClient;
}
}
public String getFeaturesJson() {
switch (this.refreshStrategy) {
case STALE_WHILE_REVALIDATE:
if (isCacheExpired()) {
this.enqueueFeatureRefreshRequest();
this.refreshExpiresAt();
}
return this.featuresJson;
case SERVER_SENT_EVENTS:
return this.featuresJson;
}
return this.featuresJson;
}
/**
* Subscribe to feature refresh events
* This callback is called when the features are successfully refreshed or there is an error when refreshing.
* This is called even if the features have not changed.
*
* @param callback This callback will be called when features are refreshed
*/
@Override
public void onFeaturesRefresh(FeatureRefreshCallback callback) {
this.refreshCallbacks.add(callback);
}
@Override
public void clearCallbacks() {
this.refreshCallbacks.clear();
}
private void enqueueFeatureRefreshRequest() {
GBFeaturesRepository self = this;
Request request = new Request.Builder()
.url(this.featuresEndpoint)
.build();
this.okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
// OkHttp will auto-retry on failure
self.onRefreshFailed(e);
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) {
try {
self.onSuccess(response);
} catch (FeatureFetchException e) {
log.error(e.getMessage(), e);
}
}
});
}
@Override
public void initialize() throws FeatureFetchException {
initialize(false);
}
@Override
public void initialize(Boolean retryOnFailure) throws FeatureFetchException {
if (this.initialized) return;
switch (this.refreshStrategy) {
case STALE_WHILE_REVALIDATE:
fetchFeatures();
break;
case SERVER_SENT_EVENTS:
fetchFeatures();
initializeSSE(retryOnFailure);
break;
}
this.initialized = true;
}
private void initializeSSE(Boolean retryOnFailure) {
if (!this.sseAllowed) {
log.info("\nFalling back to stale-while-revalidate refresh strategy. 'X-Sse-Support: enabled' not present on resource returned at {}", this.featuresEndpoint);
this.refreshStrategy = FeatureRefreshStrategy.STALE_WHILE_REVALIDATE;
}
createEventSourceListenerAndStartListening(retryOnFailure);
}
/**
* Creates an SSE HTTP client if null.
* Creates and enqueues a new asynchronous request to the events endpoint.
* Assigns a close listener to recreate the connection.
*/
private void createEventSourceListenerAndStartListening(Boolean retryOnFailure) {
this.sseEventSource = null;
this.sseRequest = null;
if (this.sseHttpClient == null) {
this.sseHttpClient = new OkHttpClient.Builder()
.addInterceptor(new GBFeaturesRepositoryRequestInterceptor())
.retryOnConnectionFailure(true)
.connectTimeout(0, TimeUnit.SECONDS)
.readTimeout(0, TimeUnit.SECONDS)
.writeTimeout(0, TimeUnit.SECONDS)
.build();
}
this.sseRequest = new Request.Builder()
.url(this.eventsEndpoint)
.header("Accept", "application/json; q=0.5")
.addHeader("Accept", "text/event-stream")
.build();
GBEventSourceListener gbEventSourceListener =
new GBEventSourceListener(
new GBEventSourceHandler() {
@Override
public void onClose(EventSource eventSource) {
eventSource.cancel();
createEventSourceListenerAndStartListening(retryOnFailure);
}
@Override
public void onFeaturesResponse(String featuresJsonResponse) throws FeatureFetchException {
onResponseJson(featuresJsonResponse);
}
}
) {
@Override
public void onFailure(@NotNull EventSource eventSource, @Nullable Throwable t, @Nullable Response response) {
super.onFailure(eventSource, t, response);
if (retryOnFailure) {
createEventSourceListenerAndStartListening(true);
try {
fetchFeatures();
} catch (FeatureFetchException featureFetchException) {
Logger.getAnonymousLogger()
.throwing(
"GBFeaturesRepository",
"createEventSourceListenerAndStartListening()",
featureFetchException
);
}
}
}
};
this.sseEventSource = EventSources
.createFactory(this.sseHttpClient)
.newEventSource(sseRequest, gbEventSourceListener);
this.sseHttpClient.newCall(sseRequest).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
log.error("SSE connection failed: {}", e.getMessage(), e);
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) {
// We don't do anything with this response
}
});
}
/**
* @return A new {@link OkHttpClient} with an interceptor {@link GBFeaturesRepositoryRequestInterceptor}
*/
private OkHttpClient initializeHttpClient() {
return new OkHttpClient.Builder()
.addInterceptor(new GBFeaturesRepositoryRequestInterceptor())
.retryOnConnectionFailure(true)
.build();
}
private void refreshExpiresAt() {
this.expiresAt = Instant.now().getEpochSecond() + this.swrTtlSeconds;
}
private Boolean isCacheExpired() {
long now = Instant.now().getEpochSecond();
return now >= this.expiresAt;
}
/**
* Performs a network request to fetch the features from the GrowthBook API
* with the provided endpoint.
* If an encryptionKey is provided, it is assumed the features endpoint is using encrypted features.
* This method will attempt to decrypt the encrypted features with the provided encryptionKey.
*/
private void fetchFeatures() throws FeatureFetchException {
if (this.featuresEndpoint == null) {
throw new IllegalArgumentException("features endpoint cannot be null");
}
Request request = new Request.Builder()
.url(this.featuresEndpoint)
.build();
try (Response response = this.okHttpClient.newCall(request).execute()) {
String sseSupportHeader = response.header("x-sse-support");
this.sseAllowed = Objects.equals(sseSupportHeader, "enabled");
this.onSuccess(response);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new FeatureFetchException(
FeatureFetchException.FeatureFetchErrorCode.UNKNOWN,
e.getMessage()
);
}
}
/**
* Reads the response JSON properties `features` or `encryptedFeatures`, and decrypts if necessary
*
* @param responseJsonString JSON response object
*/
private void onResponseJson(String responseJsonString) throws FeatureFetchException {
try {
JsonObject jsonObject = GrowthBookJsonUtils.getInstance()
.gson.fromJson(responseJsonString, JsonObject.class);
// Features will be refreshed as either an encrypted or un-encrypted JSON string
String refreshedFeatures;
if (this.encryptionKey != null) {
// Use encrypted features at responseBody.encryptedFeatures
JsonElement encryptedFeaturesJsonElement = jsonObject.get("encryptedFeatures");
if (encryptedFeaturesJsonElement == null) {
log.error(
"FeatureFetchException: CONFIGURATION_ERROR feature fetch error code: "
+ "encryptionKey provided but endpoint not encrypted");
throw new FeatureFetchException(
FeatureFetchException.FeatureFetchErrorCode.CONFIGURATION_ERROR,
"encryptionKey provided but endpoint not encrypted"
);
}
String encryptedFeaturesJson = encryptedFeaturesJsonElement.getAsString();
refreshedFeatures = DecryptionUtils.decrypt(encryptedFeaturesJson, this.encryptionKey).trim();
} else {
// Use unencrypted features at responseBody.features
JsonElement featuresJsonElement = jsonObject.get("features");
if (featuresJsonElement == null) {
log.error(
"FeatureFetchException: CONFIGURATION_ERROR feature fetch error code: "
+ "No features found");
throw new FeatureFetchException(
FeatureFetchException.FeatureFetchErrorCode.CONFIGURATION_ERROR,
"No features found"
);
}
refreshedFeatures = featuresJsonElement.toString().trim();
}
this.featuresJson = refreshedFeatures;
this.onRefreshSuccess(this.featuresJson);
} catch (DecryptionUtils.DecryptionException e) {
log.error("FeatureFetchException: UNKNOWN feature fetch error code {}",
e.getMessage(), e);
throw new FeatureFetchException(
FeatureFetchException.FeatureFetchErrorCode.UNKNOWN,
e.getMessage()
);
}
}
private void onRefreshSuccess(String featuresJson) {
this.refreshCallbacks.forEach(featureRefreshCallback -> featureRefreshCallback.onRefresh(featuresJson));
}
private void onRefreshFailed(Throwable throwable) {
this.refreshCallbacks.forEach(featureRefreshCallback -> featureRefreshCallback.onError(throwable));
}
/**
* Handles the successful features fetching response
*
* @param response Successful response
*/
private void onSuccess(Response response) throws FeatureFetchException {
try {
ResponseBody responseBody = response.body();
if (responseBody == null) {
log.error("FeatureFetchException: FeatureFetchErrorCode.NO_RESPONSE_ERROR");
throw new FeatureFetchException(
FeatureFetchException.FeatureFetchErrorCode.NO_RESPONSE_ERROR
);
}
onResponseJson(responseBody.string());
} catch (IOException e) {
log.error("FeatureFetchException: UNKNOWN feature fetch error code {}",
e.getMessage(), e);
throw new FeatureFetchException(
FeatureFetchException.FeatureFetchErrorCode.UNKNOWN,
e.getMessage()
);
}
}
private interface GBEventSourceHandler {
void onClose(EventSource eventSource);
void onFeaturesResponse(String featuresJsonResponse) throws FeatureFetchException;
}
private static class GBEventSourceListener extends EventSourceListener {
private final GBEventSourceHandler handler;
public GBEventSourceListener(GBEventSourceHandler handler) {
this.handler = handler;
}
@Override
public void onClosed(@NotNull EventSource eventSource) {
super.onClosed(eventSource);
handler.onClose(eventSource);
}
@Override
public void onEvent(@NotNull EventSource eventSource, @Nullable String id, @Nullable String type, @NotNull String data) {
super.onEvent(eventSource, id, type, data);
if (data.trim().isEmpty()) return;
try {
handler.onFeaturesResponse(data);
} catch (FeatureFetchException e) {
log.error(e.getMessage(), e);
}
}
@Override
public void onFailure(@NotNull EventSource eventSource, @Nullable Throwable t, @Nullable Response response) {
super.onFailure(eventSource, t, response);
}
@Override
public void onOpen(@NotNull EventSource eventSource, @NotNull Response response) {
super.onOpen(eventSource, response);
}
}
}