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

Infinite Socket Timeout can be specified #2028

Merged
merged 5 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ public BinaryJedis(final String host, final int port, final int connectionTimeou
client.setSoTimeout(soTimeout);
}

public BinaryJedis(final String host, final int port, final int connectionTimeout,
final int soTimeout, final int infiniteSoTimeout) {
client = new Client(host, port);
client.setConnectionTimeout(connectionTimeout);
client.setSoTimeout(soTimeout);
client.setInfiniteSoTimeout(infiniteSoTimeout);
}

public BinaryJedis(final String host, final int port, final int connectionTimeout,
final int soTimeout, final boolean ssl) {
client = new Client(host, port, ssl);
Expand All @@ -114,6 +122,16 @@ public BinaryJedis(final String host, final int port, final int connectionTimeou
client.setSoTimeout(soTimeout);
}

public BinaryJedis(final String host, final int port, final int connectionTimeout,
final int soTimeout, final int infiniteSoTimeout, final boolean ssl,
final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters,
final HostnameVerifier hostnameVerifier) {
client = new Client(host, port, ssl, sslSocketFactory, sslParameters, hostnameVerifier);
client.setConnectionTimeout(connectionTimeout);
client.setSoTimeout(soTimeout);
client.setInfiniteSoTimeout(infiniteSoTimeout);
}

public BinaryJedis(final JedisShardInfo shardInfo) {
client = new Client(shardInfo.getHost(), shardInfo.getPort(), shardInfo.getSsl(),
shardInfo.getSslSocketFactory(), shardInfo.getSslParameters(),
Expand Down Expand Up @@ -157,6 +175,15 @@ public BinaryJedis(final URI uri, final int connectionTimeout, final int soTimeo
client.setSoTimeout(soTimeout);
}

public BinaryJedis(final URI uri, final int connectionTimeout, final int soTimeout,
final int infiniteSoTimeout, final SSLSocketFactory sslSocketFactory,
final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) {
initializeClientFromURI(uri, sslSocketFactory, sslParameters, hostnameVerifier);
client.setConnectionTimeout(connectionTimeout);
client.setSoTimeout(soTimeout);
client.setInfiniteSoTimeout(infiniteSoTimeout);
}

public BinaryJedis(final JedisSocketFactory jedisSocketFactory) {
client = new Client(jedisSocketFactory);
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryJedisCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ public BinaryJedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeo
this.maxAttempts = maxAttempts;
}

public BinaryJedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout,
int infiniteSoTimeout, int maxAttempts, String user, String password, String clientName, GenericObjectPoolConfig poolConfig) {
this.connectionHandler = new JedisSlotBasedConnectionHandler(jedisClusterNode, poolConfig,
connectionTimeout, soTimeout, infiniteSoTimeout, user, password, clientName);
this.maxAttempts = maxAttempts;
}

public BinaryJedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, String password, String clientName, GenericObjectPoolConfig poolConfig,
boolean ssl) {
this(jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, password, clientName, poolConfig, ssl, null, null, null, null);
Expand All @@ -92,6 +99,14 @@ public BinaryJedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeo
this.maxAttempts = maxAttempts;
}

public BinaryJedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout,
int infiniteSoTimeout, int maxAttempts, String user, String password, String clientName, GenericObjectPoolConfig poolConfig,
boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) {
this.connectionHandler = new JedisSlotBasedConnectionHandler(jedisClusterNode, poolConfig,
connectionTimeout, soTimeout, infiniteSoTimeout, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap);
this.maxAttempts = maxAttempts;
}

@Override
public void close() {
if (connectionHandler != null) {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/redis/clients/jedis/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class Connection implements Closeable {
private Socket socket;
private RedisOutputStream outputStream;
private RedisInputStream inputStream;
private int infiniteSoTimeout = 0;
private boolean broken = false;

public Connection() {
Expand Down Expand Up @@ -76,12 +77,16 @@ public void setSoTimeout(int soTimeout) {
jedisSocketFactory.setSoTimeout(soTimeout);
}

public void setInfiniteSoTimeout(int infiniteSoTimeout) {
this.infiniteSoTimeout = infiniteSoTimeout;
}

public void setTimeoutInfinite() {
try {
if (!isConnected()) {
connect();
}
socket.setSoTimeout(0);
socket.setSoTimeout(infiniteSoTimeout);
} catch (SocketException ex) {
broken = true;
throw new JedisConnectionException(ex);
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public Jedis(final String host, final int port, final int connectionTimeout, fin
super(host, port, connectionTimeout, soTimeout);
}

public Jedis(final String host, final int port, final int connectionTimeout, final int soTimeout,
final int infiniteSoTimeout) {
super(host, port, connectionTimeout, soTimeout, infiniteSoTimeout);
}

public Jedis(final String host, final int port, final int connectionTimeout, final int soTimeout,
final boolean ssl) {
super(host, port, connectionTimeout, soTimeout, ssl);
Expand All @@ -93,6 +98,13 @@ public Jedis(final String host, final int port, final int connectionTimeout, fin
hostnameVerifier);
}

public Jedis(final String host, final int port, final int connectionTimeout, final int soTimeout,
final int infiniteSoTimeout, final boolean ssl, final SSLSocketFactory sslSocketFactory,
final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) {
super(host, port, connectionTimeout, soTimeout, infiniteSoTimeout, ssl, sslSocketFactory,
sslParameters, hostnameVerifier);
}

public Jedis(JedisShardInfo shardInfo) {
super(shardInfo);
}
Expand Down Expand Up @@ -125,6 +137,12 @@ public Jedis(final URI uri, final int connectionTimeout, final int soTimeout,
super(uri, connectionTimeout, soTimeout, sslSocketFactory, sslParameters, hostnameVerifier);
}

public Jedis(final URI uri, final int connectionTimeout, final int soTimeout,
final int infiniteSoTimeout, final SSLSocketFactory sslSocketFactory,
final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) {
super(uri, connectionTimeout, soTimeout, infiniteSoTimeout, sslSocketFactory, sslParameters, hostnameVerifier);
}

public Jedis(final JedisSocketFactory jedisSocketFactory) {
super(jedisSocketFactory);
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/redis/clients/jedis/JedisCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ public JedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, in
super(jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, user, password, clientName, poolConfig);
}

public JedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout,
int infiniteSoTimeout, int maxAttempts, String user, String password, String clientName, final GenericObjectPoolConfig poolConfig) {
super(jedisClusterNode, connectionTimeout, soTimeout, infiniteSoTimeout, maxAttempts, user, password, clientName, poolConfig);
}

public JedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout,
int maxAttempts, String password, String clientName, final GenericObjectPoolConfig poolConfig,
boolean ssl) {
Expand All @@ -164,6 +169,14 @@ public JedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, in
ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap);
}

public JedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout, int infiniteSoTimeout,
int maxAttempts, String password, String clientName, final GenericObjectPoolConfig poolConfig,
boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters,
HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) {
this(jedisClusterNode, connectionTimeout, soTimeout, infiniteSoTimeout, maxAttempts, null, password,
clientName, poolConfig, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap);
}

public JedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout,
int maxAttempts, String user, String password, String clientName, final GenericObjectPoolConfig poolConfig,
boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters,
Expand All @@ -172,6 +185,14 @@ public JedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, in
ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap);
}

public JedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout, int infiniteSoTimeout,
int maxAttempts, String user, String password, String clientName, final GenericObjectPoolConfig poolConfig,
boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters,
HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) {
super(jedisClusterNode, connectionTimeout, soTimeout, infiniteSoTimeout, maxAttempts, user, password,
clientName, poolConfig, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap);
}

@Override
public String set(final String key, final String value) {
return new JedisClusterCommand<String>(connectionHandler, maxAttempts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ public JedisClusterConnectionHandler(Set<HostAndPort> nodes, GenericObjectPoolCo
this(nodes, poolConfig, connectionTimeout, soTimeout, null, password, clientName);
}

public JedisClusterConnectionHandler(Set<HostAndPort> nodes, GenericObjectPoolConfig poolConfig,
public JedisClusterConnectionHandler(Set<HostAndPort> nodes, final GenericObjectPoolConfig poolConfig,
int connectionTimeout, int soTimeout, String user, String password, String clientName) {
this(nodes, poolConfig, connectionTimeout, soTimeout, user, password, clientName, false, null, null, null, null);
this(nodes, poolConfig, connectionTimeout, soTimeout, 0, user, password, clientName);
}

public JedisClusterConnectionHandler(Set<HostAndPort> nodes, final GenericObjectPoolConfig poolConfig,
int connectionTimeout, int soTimeout, int infiniteSoTimeout, String user, String password, String clientName) {
this(nodes, poolConfig, connectionTimeout, soTimeout, infiniteSoTimeout, user, password, clientName, false, null, null, null, null);
}

public JedisClusterConnectionHandler(Set<HostAndPort> nodes, GenericObjectPoolConfig poolConfig,
Expand All @@ -40,9 +45,17 @@ public JedisClusterConnectionHandler(Set<HostAndPort> nodes, GenericObjectPoolCo
int connectionTimeout, int soTimeout, String user, String password, String clientName,
boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters,
HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap portMap) {
this.cache = new JedisClusterInfoCache(poolConfig, connectionTimeout, soTimeout, user, password, clientName,
ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap);
initializeSlotsCache(nodes, connectionTimeout, soTimeout, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier);
this(nodes, poolConfig, connectionTimeout, soTimeout, 0, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap);
}

public JedisClusterConnectionHandler(Set<HostAndPort> nodes, final GenericObjectPoolConfig poolConfig,
int connectionTimeout, int soTimeout, int infiniteSoTimeout, String user, String password, String clientName,
boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters,
HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap portMap) {
this.cache = new JedisClusterInfoCache(poolConfig, connectionTimeout, soTimeout, infiniteSoTimeout,
user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap);
initializeSlotsCache(nodes, connectionTimeout, soTimeout, infiniteSoTimeout,
null, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier);
}

abstract Jedis getConnection();
Expand All @@ -58,11 +71,13 @@ public Map<String, JedisPool> getNodes() {
}

private void initializeSlotsCache(Set<HostAndPort> startNodes,
int connectionTimeout, int soTimeout, String user, String password, String clientName,
int connectionTimeout, int soTimeout, int infiniteSoTimeout, String user, String password, String clientName,
boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier) {
for (HostAndPort hostAndPort : startNodes) {
try (Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort(),
connectionTimeout, soTimeout, ssl, sslSocketFactory, sslParameters, hostnameVerifier)) {

try (Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort(), connectionTimeout,
soTimeout, infiniteSoTimeout, ssl, sslSocketFactory, sslParameters, hostnameVerifier)) {

if (user != null) {
jedis.auth(user, password);
} else if (password != null) {
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/redis/clients/jedis/JedisClusterInfoCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class JedisClusterInfoCache {

private int connectionTimeout;
private int soTimeout;
private int infiniteSoTimeout;
private String user;
private String password;
private String clientName;
Expand All @@ -50,25 +51,45 @@ public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig,
this(poolConfig, connectionTimeout, soTimeout, null, password, clientName);
}

public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout,
final int soTimeout, final int infiniteSoTimeout, final String password, final String clientName) {
this(poolConfig, connectionTimeout, soTimeout, infiniteSoTimeout, null, password, clientName);
}

public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig,
final int connectionTimeout, final int soTimeout, final String user, final String password, final String clientName) {
this(poolConfig, connectionTimeout, soTimeout, user, password, clientName, false, null, null, null, null);
}

public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig,
final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout,
final String user, final String password, final String clientName) {
this(poolConfig, connectionTimeout, soTimeout, infiniteSoTimeout, user, password, clientName, false, null, null, null, null);
}

public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout,
final int soTimeout, final String password, final String clientName,
boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters,
HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) {
this(poolConfig, connectionTimeout, soTimeout, null, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap);
this(poolConfig, connectionTimeout, soTimeout, null, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap);
}

public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout,
final int soTimeout, final String user, final String password, final String clientName,
boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters,
HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) {
this(poolConfig, connectionTimeout, soTimeout, 0, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap);
}

public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig,
final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout,
final String user, final String password, final String clientName, boolean ssl,
SSLSocketFactory sslSocketFactory, SSLParameters sslParameters,
HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) {
this.poolConfig = poolConfig;
this.connectionTimeout = connectionTimeout;
this.soTimeout = soTimeout;
this.infiniteSoTimeout = infiniteSoTimeout;
this.user = user;
this.password = password;
this.clientName = clientName;
Expand Down Expand Up @@ -202,7 +223,7 @@ public JedisPool setupNodeIfNotExist(HostAndPort node) {
if (existingPool != null) return existingPool;

JedisPool nodePool = new JedisPool(poolConfig, node.getHost(), node.getPort(),
connectionTimeout, soTimeout, user, password, 0, clientName,
connectionTimeout, soTimeout, infiniteSoTimeout, user, password, 0, clientName,
ssl, sslSocketFactory, sslParameters, hostnameVerifier);
nodes.put(nodeKey, nodePool);
return nodePool;
Expand Down
Loading