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

Custom connection pool to MultiClusterPooledConnectionProvider #3801

13 changes: 13 additions & 0 deletions src/main/java/redis/clients/jedis/MultiClusterClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,19 @@ public static class ClusterConfig {
private int priority;
private HostAndPort hostAndPort;
private JedisClientConfig clientConfig;
private ConnectionPoolConfig connectionPoolConfig;
thachlp marked this conversation as resolved.
Show resolved Hide resolved

public ClusterConfig(HostAndPort hostAndPort, JedisClientConfig clientConfig) {
this.hostAndPort = hostAndPort;
this.clientConfig = clientConfig;
connectionPoolConfig = new ConnectionPoolConfig();
sazzad16 marked this conversation as resolved.
Show resolved Hide resolved
thachlp marked this conversation as resolved.
Show resolved Hide resolved
}

public ClusterConfig(HostAndPort hostAndPort, JedisClientConfig clientConfig,
ConnectionPoolConfig connectionPoolConfig) {
thachlp marked this conversation as resolved.
Show resolved Hide resolved
this.hostAndPort = hostAndPort;
this.clientConfig = clientConfig;
this.connectionPoolConfig = connectionPoolConfig;
}

public int getPriority() {
Expand All @@ -198,6 +207,10 @@ public HostAndPort getHostAndPort() {
public JedisClientConfig getJedisClientConfig() {
return clientConfig;
}

public ConnectionPoolConfig getConnectionPoolConfig() {
return connectionPoolConfig;
}
}

public static class Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public MultiClusterPooledConnectionProvider(MultiClusterClientConfig multiCluste

ClusterConfig[] clusterConfigs = multiClusterClientConfig.getClusterConfigs();
for (ClusterConfig config : clusterConfigs) {
ConnectionPoolConfig poolConfig = config.getConnectionPoolConfig();

String clusterId = "cluster:" + config.getPriority() + ":" + config.getHostAndPort();

Expand All @@ -132,7 +133,7 @@ public MultiClusterPooledConnectionProvider(MultiClusterClientConfig multiCluste

multiClusterMap.put(config.getPriority(),
new Cluster(new ConnectionPool(config.getHostAndPort(),
config.getJedisClientConfig()), retry, circuitBreaker));
config.getJedisClientConfig(), poolConfig), retry, circuitBreaker));
sazzad16 marked this conversation as resolved.
Show resolved Hide resolved
}

/// --- ///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ public class MultiClusterPooledConnectionProviderTest {

@Before
public void setUp() {
ConnectionPoolConfig poolConfig = new ConnectionPoolConfig();
poolConfig.setMaxTotal(8);
poolConfig.setMaxIdle(4);
poolConfig.setMinIdle(1);

ClusterConfig[] clusterConfigs = new ClusterConfig[2];
clusterConfigs[0] = new ClusterConfig(hostAndPort1, DefaultJedisClientConfig.builder().build());
clusterConfigs[1] = new ClusterConfig(hostAndPort2, DefaultJedisClientConfig.builder().build());
clusterConfigs[0] = new ClusterConfig(hostAndPort1, DefaultJedisClientConfig.builder().build(), poolConfig);
clusterConfigs[1] = new ClusterConfig(hostAndPort2, DefaultJedisClientConfig.builder().build(), poolConfig);
sazzad16 marked this conversation as resolved.
Show resolved Hide resolved

provider = new MultiClusterPooledConnectionProvider(new MultiClusterClientConfig.Builder(clusterConfigs).build());
}
Expand Down Expand Up @@ -131,4 +135,13 @@ public void testSetActiveMultiClusterIndexOutOfRange() {
provider.setActiveMultiClusterIndex(3); // Should throw an exception
}

@Test
public void testConnectionPoolConfigApplied() {
MultiClusterPooledConnectionProvider.Cluster activeCluster = provider.getCluster();
ConnectionPool connectionPool = activeCluster.getConnectionPool();

assertEquals(8, connectionPool.getMaxTotal());
assertEquals(4, connectionPool.getMaxIdle());
assertEquals(1, connectionPool.getMinIdle());
}
}