/** * Create a delay strategy that applies an exponentially-increasing delay as long as the criteria is met. As soon as * the criteria is not met, the delay resets to zero. * * @param initialDelayInMilliseconds the initial delay; must be positive * @param maxDelayInMilliseconds the maximum delay; must be greater than the initial delay * @return the strategy; never null */ public static DelayStrategy exponential(long initialDelayInMilliseconds, long maxDelayInMilliseconds) { return exponential(initialDelayInMilliseconds, maxDelayInMilliseconds, 2.0); }
/** * Attempt to sleep when the specified criteria is met. * * @param criteria {@code true} if this method should sleep, or {@code false} if there is no need to sleep * @return {@code true} if this invocation caused the thread to sleep, or {@code false} if this method did not sleep */ default boolean sleepWhen(BooleanSupplier criteria) { return sleepWhen(criteria.getAsBoolean()); }
/** * @param config the configuration */ public ConnectionContext(Configuration config) { this.config = config; this.useHostsAsSeeds = config.getBoolean(MongoDbConnectorConfig.AUTO_DISCOVER_MEMBERS); final String username = config.getString(MongoDbConnectorConfig.USER); final String password = config.getString(MongoDbConnectorConfig.PASSWORD); final String adminDbName = ReplicaSetDiscovery.ADMIN_DATABASE_NAME; final boolean useSSL = config.getBoolean(MongoDbConnectorConfig.SSL_ENABLED); final boolean sslAllowInvalidHostnames = config.getBoolean(MongoDbConnectorConfig.SSL_ALLOW_INVALID_HOSTNAMES); // Set up the client pool so that it ... MongoClients.Builder clientBuilder = MongoClients.create(); if (username != null || password != null) { clientBuilder.withCredential(MongoCredential.createCredential(username, adminDbName, password.toCharArray())); } if (useSSL) { clientBuilder.options().sslEnabled(true).sslInvalidHostNameAllowed(sslAllowInvalidHostnames); } pool = clientBuilder.build(); this.replicaSets = ReplicaSets.parse(hosts()); final int initialDelayInMs = config.getInteger(MongoDbConnectorConfig.CONNECT_BACKOFF_INITIAL_DELAY_MS); final long maxDelayInMs = config.getLong(MongoDbConnectorConfig.CONNECT_BACKOFF_MAX_DELAY_MS); this.primaryBackoffStrategy = DelayStrategy.exponential(initialDelayInMs, maxDelayInMs); }
primaryBackoffStrategy.sleepWhen(true); continue;
/** * Create a delay strategy that applies an exponentially-increasing delay as long as the criteria is met. As soon as * the criteria is not met, the delay resets to zero. * * @param initialDelayInMilliseconds the initial delay; must be positive * @param maxDelayInMilliseconds the maximum delay; must be greater than the initial delay * @return the strategy; never null */ public static DelayStrategy exponential(long initialDelayInMilliseconds, long maxDelayInMilliseconds) { return exponential(initialDelayInMilliseconds, maxDelayInMilliseconds, 2.0); }
/** * Attempt to sleep when the specified criteria is met. * * @param criteria {@code true} if this method should sleep, or {@code false} if there is no need to sleep * @return {@code true} if this invocation caused the thread to sleep, or {@code false} if this method did not sleep */ default boolean sleepWhen(BooleanSupplier criteria) { return sleepWhen(criteria.getAsBoolean()); }