Skip to content

Commit

Permalink
retryOnFailure argument was added
Browse files Browse the repository at this point in the history
  • Loading branch information
Bohdan-Kim committed May 11, 2024
1 parent fff17ac commit f809d8e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
49 changes: 34 additions & 15 deletions lib/src/main/java/growthbook/sdk/java/GBFeaturesRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,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 +196,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 +237,34 @@ 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(retryOnFailure);
}
}
};

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

0 comments on commit f809d8e

Please sign in to comment.