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

feat: deprecating init without timeout #264

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Changes from all 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
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