Skip to content

Commit

Permalink
feat: deprecating init without timeout (#264)
Browse files Browse the repository at this point in the history
**Requirements**

- [ ] I have added test coverage for new or changed functionality - N/A

- [x] I have followed the repository's [pull request submission
guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests)

- [ ] I have validated my changes against all supported platform
versions - N/A, no platform dependencies changed and CI tests have
sufficient coverage.

**Related issues**
238624

BEGIN_COMMIT_OVERRIDE
feat: deprecating init without timeout
END_COMMIT_OVERRIDE
  • Loading branch information
tanderson-ld committed Apr 25, 2024
1 parent 30c0e0b commit 1e46f0d
Showing 1 changed file with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* context's {@code key}; see {@link LDConfig.Builder#generateAnonymousKeys(boolean)}.
*/
public class LDClient implements LDClientInterface, Closeable {

// A map of each LDClient (one per environment), or null if `init` hasn't been called yet.
// Will only be set once, during initialization, and the map is considered immutable.
static volatile Map<String, LDClient> instances = null;
Expand All @@ -59,6 +60,9 @@ public class LDClient implements LDClientInterface, Closeable {
private final EventProcessor eventProcessor;
private final ConnectivityManager connectivityManager;
private final LDLogger logger;
// If 15 seconds or more is passed as a timeout to init, we will log a warning.
private static final int EXCESSIVE_INIT_WAIT_SECONDS = 15;


/**
* Initializes the singleton/primary instance. The result is a {@link Future} which
Expand All @@ -77,7 +81,11 @@ public class LDClient implements LDClientInterface, Closeable {
* @return a {@link Future} which will complete once the client has been initialized
* @see #init(Application, LDConfig, LDContext, int)
* @since 3.0.0
* @deprecated Initializing the {@link LDClient} without a timeout is no longer permitted to help prevent
* consumers from blocking their application execution by mistake when connectivity is poor. Please use
* {@link #init(Application, LDConfig, LDContext, int)} and specify a timeout instead.
*/
@Deprecated()
public static Future<LDClient> init(@NonNull Application application,
@NonNull LDConfig config,
@NonNull LDContext context) {
Expand Down Expand Up @@ -211,22 +219,31 @@ public void onError(Throwable e) {
* @param context the initial evaluation context; see {@link LDClient} for more
* information about setting the context and optionally requesting a
* unique key for it
* @param startWaitSeconds maximum number of seconds to wait for the client to initialize
* @param startWaitSeconds maximum number of seconds to wait for the client to initialize. Determines when this
* function will return if no flags have been fetched. If you use a large timeout, then
* any network delays could cause your application to wait a long time before continuing
* execution.
*
* @return the primary LDClient instance
* @see #init(Application, LDConfig, LDContext)
* @since 3.0.0
*/
public static LDClient init(Application application, LDConfig config, LDContext context, int startWaitSeconds) {
initSharedLogger(config);
getSharedLogger().info("Initializing Client and waiting up to {} for initialization to complete", startWaitSeconds);
if (startWaitSeconds >= EXCESSIVE_INIT_WAIT_SECONDS) {
getSharedLogger().warn("LDClient.init called with start wait time parameter of {} seconds. We recommend a timeout of less than {} seconds.", startWaitSeconds, EXCESSIVE_INIT_WAIT_SECONDS);
}

Future<LDClient> initFuture = init(application, config, context);
try {
return initFuture.get(startWaitSeconds, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException e) {
getSharedLogger().error("Exception during Client initialization: {}", LogValues.exceptionSummary(e));
getSharedLogger().debug(LogValues.exceptionTrace(e));
} catch (TimeoutException e) {
getSharedLogger().warn("Client did not successfully initialize within {} seconds. It could be taking longer than expected to start up", startWaitSeconds);
getSharedLogger().warn("Client did not successfully initialize within {} seconds. It could be taking longer than expected to fetch data." +
" Client can be used immediately and will continue retrying in the background.", startWaitSeconds);
}
return instances.get(LDConfig.primaryEnvironmentName);
}
Expand Down

0 comments on commit 1e46f0d

Please sign in to comment.