Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/src/main/java/growthbook/sdk/java/GrowthBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import growthbook.sdk.java.model.FeatureResult;
import growthbook.sdk.java.model.GBContext;
import growthbook.sdk.java.model.RequestBodyForRemoteEval;
import growthbook.sdk.java.repository.GBFeaturesRepository;
import growthbook.sdk.java.repository.PrefetchOptions;
import growthbook.sdk.java.remoteeval.RemoteEvalCache;
import growthbook.sdk.java.remoteeval.RemoteEvalCacheKey;
import growthbook.sdk.java.remoteeval.RemoteEvalOptionsValidator;
Expand Down Expand Up @@ -96,6 +98,20 @@ public GrowthBook(GBContext context) {
this.pluginRegistry.initAll();
}

/**
* Warms the process-shared feature payload cache for the given {@code (apiHost, clientKey)} so a
* later {@link GBFeaturesRepository} configured with the same key and {@code sharedCacheEnabled(true)}
* serves its first request from cache with no network round trip. Boot-time warm-up and
* thundering-herd protection for the single-context path where many short-lived instances share a
* key (per-request / serverless). Delegates to {@link GBFeaturesRepository#prefetchPayload(PrefetchOptions)}.
*
* @param options identifies the payload to warm; {@code clientKey} is required
* @throws FeatureFetchException when the warm-up fetch fails
*/
public static void prefetchPayload(PrefetchOptions options) throws FeatureFetchException {
GBFeaturesRepository.prefetchPayload(options);
}

/**
* No-args constructor. A {@link GBContext} with default values is created.
* It's recommended to create your own context with {@link GBContext#builder()} or the {@link GBContext} constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,57 @@
import java.util.concurrent.atomic.AtomicBoolean;

/**
* Owns refresh execution mode selection and background refresh scheduling.
* Executes feature-refresh operations, either synchronously on the caller's thread
* ({@link #runNow}) or on a shared background daemon executor ({@link #runInBackground}).
*
* <p>Repositories provide the actual refresh operation. This class decides
* whether the operation should run synchronously or in the background refresh
* executor based on {@link RefreshMode}.
* <p>Execution mode (now vs background) and cache semantics ({@link RefreshMode}) are independent:
* the caller chooses the method for the former and passes the latter through to its action. The
* scheduler never derives one from the other — deciding, for example, that a forced refresh should
* run in the background is the caller's policy, not this class's.
*/
@Slf4j
public final class FeatureRefreshScheduler {
private final AtomicBoolean shuttingDown = new AtomicBoolean(false);
private ExecutorService featureRefreshExecutor;

/**
* @deprecated Execution mode and {@link RefreshMode} are separate concerns. Choose explicitly with
* {@link #runNow(RefreshMode, FeatureRefreshAction)} or
* {@link #runInBackground(RefreshMode, FeatureRefreshAction)} instead.
*/
@Deprecated
public void requestRefresh(RefreshMode refreshMode, FeatureRefreshAction refreshAction) {
FeatureRefreshAction resolvedRefreshAction = Objects.requireNonNull(refreshAction, "refreshAction");
RefreshMode resolvedRefreshMode = refreshMode == null ? RefreshMode.DEFAULT : refreshMode;
if (resolvedRefreshMode == RefreshMode.FORCE) {
refreshInBackground(resolvedRefreshMode, resolvedRefreshAction);
runInBackground(resolvedRefreshMode, refreshAction);
return;
}

refreshNow(resolvedRefreshMode, resolvedRefreshAction);
runNow(resolvedRefreshMode, refreshAction);
}

public synchronized void shutdown() {
this.shuttingDown.set(true);
if (this.featureRefreshExecutor != null) {
this.featureRefreshExecutor.shutdownNow();
this.featureRefreshExecutor = null;
log.info("Feature refresh executor shut down");
}
}

private void refreshNow(RefreshMode refreshMode, FeatureRefreshAction refreshAction) {
/**
* Runs {@code action} synchronously on the calling thread with the given cache semantics. A
* {@link FeatureFetchException} is logged and swallowed so a refresh failure cannot propagate out
* of a fire-and-forget request.
*/
public void runNow(RefreshMode refreshMode, FeatureRefreshAction action) {
RefreshMode mode = refreshMode == null ? RefreshMode.DEFAULT : refreshMode;
FeatureRefreshAction resolvedAction = Objects.requireNonNull(action, "action");
try {
refreshAction.refresh(refreshMode);
resolvedAction.refresh(mode);
} catch (FeatureFetchException e) {
log.error("Refreshing wasn't successful. Message is: {}", e.getMessage(), e);
}
}

private void refreshInBackground(RefreshMode refreshMode, FeatureRefreshAction refreshAction) {
/**
* Runs {@code action} on the background refresh executor with the given cache semantics. Skips
* quietly while shutting down; failures are logged, never propagated.
*/
public void runInBackground(RefreshMode refreshMode, FeatureRefreshAction action) {
RefreshMode mode = refreshMode == null ? RefreshMode.DEFAULT : refreshMode;
FeatureRefreshAction resolvedAction = Objects.requireNonNull(action, "action");
if (this.shuttingDown.get()) {
log.warn("Skipping background feature refresh because the repository is shutting down.");
return;
Expand All @@ -65,7 +76,7 @@ private void refreshInBackground(RefreshMode refreshMode, FeatureRefreshAction r
}

try {
refreshAction.refresh(refreshMode);
resolvedAction.refresh(mode);
} catch (FeatureFetchException e) {
log.error("Background feature refresh failed. Message is: {}", e.getMessage(), e);
} catch (RuntimeException e) {
Expand All @@ -77,6 +88,22 @@ private void refreshInBackground(RefreshMode refreshMode, FeatureRefreshAction r
}
}

/**
* Convenience for background execution with {@link RefreshMode#DEFAULT} (ETag-aware) semantics.
*/
public void runInBackground(FeatureRefreshAction action) {
runInBackground(RefreshMode.DEFAULT, action);
}

public synchronized void shutdown() {
this.shuttingDown.set(true);
if (this.featureRefreshExecutor != null) {
this.featureRefreshExecutor.shutdownNow();
this.featureRefreshExecutor = null;
log.info("Feature refresh executor shut down");
}
}

private synchronized ExecutorService getFeatureRefreshExecutor() {
if (this.shuttingDown.get()) {
throw new RejectedExecutionException("Feature refresh scheduler is shutting down.");
Expand Down
Loading
Loading