Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #52 #55

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 46 additions & 15 deletions lib/src/main/java/growthbook/sdk/java/GBFeaturesRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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.
Expand Down Expand Up @@ -182,6 +183,11 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO

@Override
public void initialize() throws FeatureFetchException {
initialize(false);
}

@Override
public void initialize(Boolean retryOnFailure) throws FeatureFetchException {
if (this.initialized) return;

switch (this.refreshStrategy) {
Expand All @@ -191,28 +197,28 @@ public void initialize() throws FeatureFetchException {

case SERVER_SENT_EVENTS:
fetchFeatures();
initializeSSE();
initializeSSE(retryOnFailure);
break;
}

this.initialized = true;
}

private void initializeSSE() {
private void initializeSSE(Boolean retryOnFailure) {
if (!this.sseAllowed) {
System.out.printf("\nFalling back to stale-while-revalidate refresh strategy. 'X-Sse-Support: enabled' not present on resource returned at %s", this.featuresEndpoint);
this.refreshStrategy = FeatureRefreshStrategy.STALE_WHILE_REVALIDATE;
}

createEventSourceListenerAndStartListening();
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() {
private void createEventSourceListenerAndStartListening(Boolean retryOnFailure) {
this.sseEventSource = null;
this.sseRequest = null;

Expand All @@ -232,20 +238,45 @@ private void createEventSourceListenerAndStartListening() {
.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, new GBEventSourceListener(new GBEventSourceHandler() {
@Override
public void onClose(EventSource eventSource) {
eventSource.cancel();
createEventSourceListenerAndStartListening();
}
.newEventSource(sseRequest, gbEventSourceListener);

@Override
public void onFeaturesResponse(String featuresJsonResponse) throws FeatureFetchException {
onResponseJson(featuresJsonResponse);
}
}));
this.sseHttpClient.newCall(sseRequest).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
interface IGBFeaturesRepository {
void initialize() throws FeatureFetchException;
void initialize(Boolean retryOnFailure) throws FeatureFetchException;

/**
* Required implementation to get the featuresJson
Expand Down