Created
March 26, 2020 21:50
-
-
Save x0a1b/3c0a77aedddeefd03586034d5d6ce147 to your computer and use it in GitHub Desktop.
Lettuce Config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.lettuce.core.TimeoutOptions; | |
import io.lettuce.core.cluster.ClusterClientOptions; | |
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions; | |
import java.time.Duration; | |
public class LettuceConfigFactory { | |
public static final int DEFAULT_TIMEOUT_MILLIS = 1000; | |
public static final int DEFAULT_REFRESH_TRIGGERS_RECONNECT_ATTEMPTS = 3; | |
public static final int DEFAULT_PERIODIC_REFRESH_MILLIS = 15000; | |
private static final String TIMEOUT_MILLIS_ENV = "LETTUCE_TIMEOUT_MILLIS"; | |
private static final String REFRESH_TRIGGERS_RECONNECT_ATTEMPTS_ENV = "LETTUCE_REFRESH_TRIGGERS_RECONNECT_ATTEMPTS"; | |
private static final String PERIODIC_REFRESH_MILLIS_ENV = "LETTUCE_PERIODIC_REFRESH_MILLIS"; | |
public static ClusterClientOptions clusterConfig( | |
int timeoutMillis, | |
int periodicRefreshMillis, | |
int refreshTriggersReconnectAttempts | |
) { | |
TimeoutOptions timeoutOptions = TimeoutOptions.builder() | |
.timeoutCommands(true) | |
.connectionTimeout() | |
.fixedTimeout(Duration.ofMillis(timeoutMillis)) | |
.build(); | |
ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder() | |
.enableAllAdaptiveRefreshTriggers() | |
.refreshTriggersReconnectAttempts(refreshTriggersReconnectAttempts) | |
.enablePeriodicRefresh(Duration.ofMillis(periodicRefreshMillis)) | |
.build(); | |
return ClusterClientOptions.builder() | |
.timeoutOptions(timeoutOptions) | |
.topologyRefreshOptions(clusterTopologyRefreshOptions) | |
.build(); | |
} | |
public static ClusterClientOptions clusterConfig( | |
int timeoutMillis | |
) { | |
return clusterConfig( | |
timeoutMillis, | |
getEnvironmentOrDefault( | |
PERIODIC_REFRESH_MILLIS_ENV, | |
DEFAULT_PERIODIC_REFRESH_MILLIS | |
), | |
getEnvironmentOrDefault( | |
REFRESH_TRIGGERS_RECONNECT_ATTEMPTS_ENV, | |
DEFAULT_REFRESH_TRIGGERS_RECONNECT_ATTEMPTS | |
) | |
); | |
} | |
public static ClusterClientOptions clusterConfig() { | |
return clusterConfig( | |
getEnvironmentOrDefault( | |
TIMEOUT_MILLIS_ENV, | |
DEFAULT_TIMEOUT_MILLIS | |
) | |
); | |
} | |
private static int getEnvironmentOrDefault(String name, int defaultValue) { | |
try { | |
String env = System.getProperty(name); | |
if (env == null) { | |
return defaultValue; | |
} | |
return Integer.parseInt(env); | |
} catch (Exception e) { | |
return defaultValue; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment