Skip to content

Commit

Permalink
Add support to the use of JedisSocketFactory using a pool
Browse files Browse the repository at this point in the history
- Support for JedisSocketFactory has already been added to the lowest level Jedis to support adding any custom socket factory (e.g. UDS), this propagates the support in the JedisPool too
  • Loading branch information
mina-asham committed Nov 28, 2020
1 parent 72dc12e commit 8fbfdca
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
59 changes: 28 additions & 31 deletions src/main/java/redis/clients/jedis/JedisFactory.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package redis.clients.jedis;

import java.net.URI;
import java.util.concurrent.atomic.AtomicReference;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLParameters;
Expand All @@ -19,17 +18,19 @@
* PoolableObjectFactory custom impl.
*/
class JedisFactory implements PooledObjectFactory<Jedis> {
private final AtomicReference<HostAndPort> hostAndPort = new AtomicReference<>();
private final int connectionTimeout;
private final int soTimeout;
private final JedisSocketFactory jedisSocketFactory;
private final String user;
private final String password;
private final int database;
private final String clientName;
private final boolean ssl;
private final SSLSocketFactory sslSocketFactory;
private final SSLParameters sslParameters;
private final HostnameVerifier hostnameVerifier;

JedisFactory(final JedisSocketFactory jedisSocketFactory, final String user, final String password, final int database, final String clientName) {
this.jedisSocketFactory = jedisSocketFactory;
this.user = user;
this.password = password;
this.database = database;
this.clientName = clientName;
}

JedisFactory(final String host, final int port, final int connectionTimeout,
final int soTimeout, final String password, final int database, final String clientName) {
Expand All @@ -55,17 +56,8 @@ class JedisFactory implements PooledObjectFactory<Jedis> {
final int soTimeout, final String user, final String password, final int database, final String clientName,
final boolean ssl, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters,
final HostnameVerifier hostnameVerifier) {
this.hostAndPort.set(new HostAndPort(host, port));
this.connectionTimeout = connectionTimeout;
this.soTimeout = soTimeout;
this.user = user;
this.password = password;
this.database = database;
this.clientName = clientName;
this.ssl = ssl;
this.sslSocketFactory = sslSocketFactory;
this.sslParameters = sslParameters;
this.hostnameVerifier = hostnameVerifier;
this(new DefaultJedisSocketFactory(host, port, connectionTimeout, soTimeout, ssl,
sslSocketFactory, sslParameters, hostnameVerifier), user, password, database, clientName);
}

JedisFactory(final URI uri, final int connectionTimeout, final int soTimeout,
Expand All @@ -81,21 +73,20 @@ class JedisFactory implements PooledObjectFactory<Jedis> {
"Cannot open Redis connection due invalid URI. %s", uri.toString()));
}

this.hostAndPort.set(new HostAndPort(uri.getHost(), uri.getPort()));
this.connectionTimeout = connectionTimeout;
this.soTimeout = soTimeout;
this.jedisSocketFactory = new DefaultJedisSocketFactory(
uri.getHost(), uri.getPort(), connectionTimeout, soTimeout,
JedisURIHelper.isRedisSSLScheme(uri), sslSocketFactory,
sslParameters, hostnameVerifier
);
this.user = JedisURIHelper.getUser(uri);
this.password = JedisURIHelper.getPassword(uri);
this.database = JedisURIHelper.getDBIndex(uri);
this.clientName = clientName;
this.ssl = JedisURIHelper.isRedisSSLScheme(uri);
this.sslSocketFactory = sslSocketFactory;
this.sslParameters = sslParameters;
this.hostnameVerifier = hostnameVerifier;
}

public void setHostAndPort(final HostAndPort hostAndPort) {
this.hostAndPort.set(hostAndPort);
jedisSocketFactory.setHost(hostAndPort.getHost());
jedisSocketFactory.setPort(hostAndPort.getPort());
}

@Override
Expand Down Expand Up @@ -123,9 +114,7 @@ public void destroyObject(PooledObject<Jedis> pooledJedis) throws Exception {

@Override
public PooledObject<Jedis> makeObject() throws Exception {
final HostAndPort hp = this.hostAndPort.get();
final Jedis jedis = new Jedis(hp.getHost(), hp.getPort(), connectionTimeout, soTimeout,
ssl, sslSocketFactory, sslParameters, hostnameVerifier);
final Jedis jedis = getJedis();
try {
jedis.connect();
if (user != null) {
Expand All @@ -147,6 +136,10 @@ public PooledObject<Jedis> makeObject() throws Exception {
return new DefaultPooledObject<>(jedis);
}

private Jedis getJedis() {
return new Jedis(jedisSocketFactory);
}

@Override
public void passivateObject(PooledObject<Jedis> pooledJedis) throws Exception {
// TODO maybe should select db 0? Not sure right now.
Expand All @@ -156,7 +149,7 @@ public void passivateObject(PooledObject<Jedis> pooledJedis) throws Exception {
public boolean validateObject(PooledObject<Jedis> pooledJedis) {
final BinaryJedis jedis = pooledJedis.getObject();
try {
HostAndPort hostAndPort = this.hostAndPort.get();
HostAndPort hostAndPort = getHostAndPort();

String connectionHost = jedis.getClient().getHost();
int connectionPort = jedis.getClient().getPort();
Expand All @@ -168,4 +161,8 @@ public boolean validateObject(PooledObject<Jedis> pooledJedis) {
return false;
}
}

private HostAndPort getHostAndPort() {
return new HostAndPort(jedisSocketFactory.getHost(), jedisSocketFactory.getPort());
}
}
13 changes: 13 additions & 0 deletions src/main/java/redis/clients/jedis/JedisPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,19 @@ public JedisPool(final GenericObjectPoolConfig poolConfig, final URI uri,
sslParameters, hostnameVerifier));
}

public JedisPool(final GenericObjectPoolConfig poolConfig, final JedisSocketFactory jedisSocketFactory,
final String user, final String password, final int database, final String clientName) {
super(poolConfig, new JedisFactory(jedisSocketFactory, user, password, database, clientName));
}

public JedisPool(final GenericObjectPoolConfig poolConfig, final JedisSocketFactory jedisSocketFactory) {
super(poolConfig, new JedisFactory(jedisSocketFactory, null, null, Protocol.DEFAULT_DATABASE, null));
}

public JedisPool(final JedisSocketFactory jedisSocketFactory) {
super(new GenericObjectPoolConfig(), new JedisFactory(jedisSocketFactory, null, null, Protocol.DEFAULT_DATABASE, null));
}

@Override
public Jedis getResource() {
Jedis jedis = super.getResource();
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/redis/clients/jedis/tests/UdsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.newsclub.net.unix.AFUNIXSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisSocketFactory;
import redis.clients.jedis.Protocol;

Expand All @@ -22,6 +23,14 @@ public void testConnectsToUds() {
}
}

@Test
public void testConnectsToUdsWithPool() {
try (JedisPool jedisPool = new JedisPool(new UdsJedisSocketFactory());
Jedis jedis = jedisPool.getResource()) {
assertEquals("PONG", jedis.ping());
}
}

private static class UdsJedisSocketFactory implements JedisSocketFactory {

private static final File UDS_SOCKET = new File("/tmp/redis_uds.sock");
Expand Down

0 comments on commit 8fbfdca

Please sign in to comment.