Skip to content
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
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,19 @@ cluster-enabled yes
cluster-config-file /tmp/redis_cluster_node5.conf
endef

# UDS REDIS NODES
define REDIS_UDS
daemonize yes
protected-mode no
port 0
pidfile /tmp/redis_uds.pid
logfile /tmp/redis_uds.log
unixsocket /tmp/redis_uds.sock
unixsocketperm 777
save ""
appendonly no
endef

#STUNNEL
define STUNNEL_CONF
cert = src/test/resources/private.pem
Expand Down Expand Up @@ -262,6 +275,7 @@ export REDIS_CLUSTER_NODE2_CONF
export REDIS_CLUSTER_NODE3_CONF
export REDIS_CLUSTER_NODE4_CONF
export REDIS_CLUSTER_NODE5_CONF
export REDIS_UDS
export STUNNEL_CONF
export STUNNEL_BIN

Expand Down Expand Up @@ -292,6 +306,7 @@ start: stunnel cleanup
echo "$$REDIS_CLUSTER_NODE3_CONF" | redis-server -
echo "$$REDIS_CLUSTER_NODE4_CONF" | redis-server -
echo "$$REDIS_CLUSTER_NODE5_CONF" | redis-server -
echo "$$REDIS_UDS" | redis-server -

cleanup:
- rm -vf /tmp/redis_cluster_node*.conf 2>/dev/null
Expand Down Expand Up @@ -319,6 +334,7 @@ stop:
kill `cat /tmp/redis_cluster_node3.pid` || true
kill `cat /tmp/redis_cluster_node4.pid` || true
kill `cat /tmp/redis_cluster_node5.pid` || true
kill `cat /tmp/redis_uds.pid` || true
kill `cat /tmp/stunnel.pid` || true
rm -f /tmp/sentinel1.conf
rm -f /tmp/sentinel2.conf
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@
<version>2.11.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.kohlschutter.junixsocket</groupId>
<artifactId>junixsocket-core</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<distributionManagement>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public BinaryClient(final String host, final int port, final boolean ssl,
super(host, port, ssl, sslSocketFactory, sslParameters, hostnameVerifier);
}

public BinaryClient(final JedisSocketFactory jedisSocketFactory) {
super(jedisSocketFactory);
}

public boolean isInMulti() {
return isInMulti;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ public BinaryJedis(final URI uri, final int connectionTimeout, final int soTimeo
client.setSoTimeout(soTimeout);
}

public BinaryJedis(final JedisSocketFactory jedisSocketFactory) {
client = new Client(jedisSocketFactory);
}

private void initializeClientFromURI(URI uri) {
initializeClientFromURI(uri, null, null, null);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/redis/clients/jedis/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public Client(final String host, final int port, final boolean ssl,
super(host, port, ssl, sslSocketFactory, sslParameters, hostnameVerifier);
}

public Client(final JedisSocketFactory jedisSocketFactory) {
super(jedisSocketFactory);
}

@Override
public void ping(final String message) {
ping(SafeEncoder.encode(message));
Expand Down
86 changes: 23 additions & 63 deletions src/main/java/redis/clients/jedis/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

import redis.clients.jedis.commands.ProtocolCommand;
Expand All @@ -25,66 +23,57 @@ public class Connection implements Closeable {

private static final byte[][] EMPTY_ARGS = new byte[0][];

private String host = Protocol.DEFAULT_HOST;
private int port = Protocol.DEFAULT_PORT;
private JedisSocketFactory jedisSocketFactory;
private Socket socket;
private RedisOutputStream outputStream;
private RedisInputStream inputStream;
private int connectionTimeout = Protocol.DEFAULT_TIMEOUT;
private int soTimeout = Protocol.DEFAULT_TIMEOUT;
private boolean broken = false;
private boolean ssl;
private SSLSocketFactory sslSocketFactory;
private SSLParameters sslParameters;
private HostnameVerifier hostnameVerifier;

public Connection() {
this(Protocol.DEFAULT_HOST);
}

public Connection(final String host) {
this.host = host;
this(host, Protocol.DEFAULT_PORT);
}

public Connection(final String host, final int port) {
this.host = host;
this.port = port;
this(host, port, false);
}

public Connection(final String host, final int port, final boolean ssl) {
this.host = host;
this.port = port;
this.ssl = ssl;
this(host, port, ssl, null, null, null);
}

public Connection(final String host, final int port, final boolean ssl,
SSLSocketFactory sslSocketFactory, SSLParameters sslParameters,
HostnameVerifier hostnameVerifier) {
this.host = host;
this.port = port;
this.ssl = ssl;
this.sslSocketFactory = sslSocketFactory;
this.sslParameters = sslParameters;
this.hostnameVerifier = hostnameVerifier;
this(new DefaultJedisSocketFactory(host, port, Protocol.DEFAULT_TIMEOUT,
Protocol.DEFAULT_TIMEOUT, ssl, sslSocketFactory, sslParameters, hostnameVerifier));
}

public Connection(final JedisSocketFactory jedisSocketFactory) {
this.jedisSocketFactory = jedisSocketFactory;
}

public Socket getSocket() {
return socket;
}

public int getConnectionTimeout() {
return connectionTimeout;
return jedisSocketFactory.getConnectionTimeout();
}

public int getSoTimeout() {
return soTimeout;
return jedisSocketFactory.getSoTimeout();
}

public void setConnectionTimeout(int connectionTimeout) {
this.connectionTimeout = connectionTimeout;
jedisSocketFactory.setConnectionTimeout(connectionTimeout);
}

public void setSoTimeout(int soTimeout) {
this.soTimeout = soTimeout;
jedisSocketFactory.setSoTimeout(soTimeout);
}

public void setTimeoutInfinite() {
Expand All @@ -101,7 +90,7 @@ public void setTimeoutInfinite() {

public void rollbackTimeout() {
try {
socket.setSoTimeout(soTimeout);
socket.setSoTimeout(jedisSocketFactory.getSoTimeout());
} catch (SocketException ex) {
broken = true;
throw new JedisConnectionException(ex);
Expand Down Expand Up @@ -148,61 +137,32 @@ public void sendCommand(final ProtocolCommand cmd, final byte[]... args) {
}

public String getHost() {
return host;
return jedisSocketFactory.getHost();
}

public void setHost(final String host) {
this.host = host;
jedisSocketFactory.setHost(host);
}

public int getPort() {
return port;
return jedisSocketFactory.getPort();
}

public void setPort(final int port) {
this.port = port;
jedisSocketFactory.setPort(port);
}

public void connect() {
if (!isConnected()) {
try {
socket = new Socket();
// ->@wjw_add
socket.setReuseAddress(true);
socket.setKeepAlive(true); // Will monitor the TCP connection is
// valid
socket.setTcpNoDelay(true); // Socket buffer Whetherclosed, to
// ensure timely delivery of data
socket.setSoLinger(true, 0); // Control calls close () method,
// the underlying socket is closed
// immediately
// <-@wjw_add

socket.connect(new InetSocketAddress(host, port), connectionTimeout);
socket.setSoTimeout(soTimeout);

if (ssl) {
if (null == sslSocketFactory) {
sslSocketFactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
}
socket = sslSocketFactory.createSocket(socket, host, port, true);
if (null != sslParameters) {
((SSLSocket) socket).setSSLParameters(sslParameters);
}
if ((null != hostnameVerifier) &&
(!hostnameVerifier.verify(host, ((SSLSocket) socket).getSession()))) {
String message = String.format(
"The connection to '%s' failed ssl/tls hostname verification.", host);
throw new JedisConnectionException(message);
}
}
socket = jedisSocketFactory.createSocket();

outputStream = new RedisOutputStream(socket.getOutputStream());
inputStream = new RedisInputStream(socket.getInputStream());
} catch (IOException ex) {
broken = true;
throw new JedisConnectionException("Failed connecting to host "
+ host + ":" + port, ex);
throw new JedisConnectionException("Failed connecting to "
+ jedisSocketFactory.getDescription(), ex);
}
}
}
Expand Down
124 changes: 124 additions & 0 deletions src/main/java/redis/clients/jedis/DefaultJedisSocketFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package redis.clients.jedis;

import redis.clients.jedis.exceptions.JedisConnectionException;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

public class DefaultJedisSocketFactory implements JedisSocketFactory {

private String host;
private int port;
private int connectionTimeout;
private int soTimeout;
private boolean ssl;
private SSLSocketFactory sslSocketFactory;
private SSLParameters sslParameters;
private HostnameVerifier hostnameVerifier;

public DefaultJedisSocketFactory(String host, int port, int connectionTimeout, int soTimeout,
boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters,
HostnameVerifier hostnameVerifier) {
this.host = host;
this.port = port;
this.connectionTimeout = connectionTimeout;
this.soTimeout = soTimeout;
this.ssl = ssl;
this.sslSocketFactory = sslSocketFactory;
this.sslParameters = sslParameters;
this.hostnameVerifier = hostnameVerifier;
}

@Override
public Socket createSocket() throws IOException {
Socket socket = null;
try {
socket = new Socket();
// ->@wjw_add
socket.setReuseAddress(true);
socket.setKeepAlive(true); // Will monitor the TCP connection is
// valid
socket.setTcpNoDelay(true); // Socket buffer Whetherclosed, to
// ensure timely delivery of data
socket.setSoLinger(true, 0); // Control calls close () method,
// the underlying socket is closed
// immediately
// <-@wjw_add

socket.connect(new InetSocketAddress(getHost(), getPort()), getConnectionTimeout());
socket.setSoTimeout(getSoTimeout());

if (ssl) {
if (null == sslSocketFactory) {
sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
}
socket = sslSocketFactory.createSocket(socket, getHost(), getPort(), true);
if (null != sslParameters) {
((SSLSocket) socket).setSSLParameters(sslParameters);
}
if ((null != hostnameVerifier)
&& (!hostnameVerifier.verify(getHost(), ((SSLSocket) socket).getSession()))) {
String message = String.format(
"The connection to '%s' failed ssl/tls hostname verification.", getHost());
throw new JedisConnectionException(message);
}
}
return socket;
} catch (Exception ex) {
if (socket != null) {
socket.close();
}
throw ex;
}
}

@Override
public String getDescription() {
return host + ":" + port;
}

@Override
public String getHost() {
return host;
}

@Override
public void setHost(String host) {
this.host = host;
}

@Override
public int getPort() {
return port;
}

@Override
public void setPort(int port) {
this.port = port;
}

@Override
public int getConnectionTimeout() {
return connectionTimeout;
}

@Override
public void setConnectionTimeout(int connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}

@Override
public int getSoTimeout() {
return soTimeout;
}

@Override
public void setSoTimeout(int soTimeout) {
this.soTimeout = soTimeout;
}
}
Loading