Skip to content

Commit 0c35deb

Browse files
mina-ashamMina Asham
authored andcommitted
Add Unix domain socket support
1 parent bf21239 commit 0c35deb

File tree

9 files changed

+120
-15
lines changed

9 files changed

+120
-15
lines changed

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,19 @@ cluster-enabled yes
211211
cluster-config-file /tmp/redis_cluster_node5.conf
212212
endef
213213

214+
# UDS REDIS NODES
215+
define REDIS_UDS
216+
daemonize yes
217+
protected-mode no
218+
port 0
219+
pidfile /tmp/redis_uds.pid
220+
logfile /tmp/redis_uds.log
221+
unixsocket /tmp/redis_6379.sock
222+
unixsocketperm 777
223+
save ""
224+
appendonly no
225+
endef
226+
214227
#STUNNEL
215228
define STUNNEL_CONF
216229
cert = src/test/resources/private.pem
@@ -236,6 +249,7 @@ export REDIS_CLUSTER_NODE2_CONF
236249
export REDIS_CLUSTER_NODE3_CONF
237250
export REDIS_CLUSTER_NODE4_CONF
238251
export REDIS_CLUSTER_NODE5_CONF
252+
export REDIS_UDS
239253
export STUNNEL_CONF
240254
export STUNNEL_BIN
241255

@@ -265,6 +279,7 @@ start: stunnel cleanup
265279
echo "$$REDIS_CLUSTER_NODE3_CONF" | redis-server -
266280
echo "$$REDIS_CLUSTER_NODE4_CONF" | redis-server -
267281
echo "$$REDIS_CLUSTER_NODE5_CONF" | redis-server -
282+
echo "$$REDIS_UDS" | redis-server -
268283

269284
cleanup:
270285
- rm -vf /tmp/redis_cluster_node*.conf 2>/dev/null
@@ -291,6 +306,7 @@ stop:
291306
kill `cat /tmp/redis_cluster_node3.pid` || true
292307
kill `cat /tmp/redis_cluster_node4.pid` || true
293308
kill `cat /tmp/redis_cluster_node5.pid` || true
309+
kill `cat /tmp/redis_uds.pid` || true
294310
kill `cat /tmp/stunnel.pid` || true
295311
rm -f /tmp/sentinel1.conf
296312
rm -f /tmp/sentinel2.conf

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<redis-hosts>localhost:6379,localhost:6380,localhost:6381,localhost:6382,localhost:6383,localhost:6384,localhost:6385</redis-hosts>
4949
<sentinel-hosts>localhost:26379,localhost:26380,localhost:26381</sentinel-hosts>
5050
<cluster-hosts>localhost:7379,localhost:7380,localhost:7381,localhost:7382,localhost:7383,localhost:7384,localhost:7385</cluster-hosts>
51+
<uds-hosts>/tmp/redis_6379.sock</uds-hosts>
5152
<github.global.server>github</github.global.server>
5253
</properties>
5354

@@ -64,6 +65,11 @@
6465
<type>jar</type>
6566
<scope>compile</scope>
6667
</dependency>
68+
<dependency>
69+
<groupId>com.kohlschutter.junixsocket</groupId>
70+
<artifactId>junixsocket-native-common</artifactId>
71+
<version>2.0.4</version>
72+
</dependency>
6773

6874
<dependency>
6975
<groupId>junit</groupId>

src/main/java/redis/clients/jedis/BinaryClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import static redis.clients.jedis.Protocol.Keyword.STORE;
1414
import static redis.clients.jedis.Protocol.Keyword.WITHSCORES;
1515

16+
import java.io.File;
1617
import java.util.ArrayList;
1718
import java.util.List;
1819
import java.util.Map;
@@ -49,6 +50,10 @@ public BinaryClient(final String host) {
4950
super(host);
5051
}
5152

53+
public BinaryClient(final File unixDomainSocket) {
54+
super(unixDomainSocket);
55+
}
56+
5257
public BinaryClient(final String host, final int port) {
5358
super(host, port);
5459
}

src/main/java/redis/clients/jedis/BinaryJedis.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static redis.clients.jedis.Protocol.toByteArray;
44

55
import java.io.Closeable;
6+
import java.io.File;
67
import java.io.Serializable;
78
import java.net.URI;
89
import java.util.AbstractMap;
@@ -56,6 +57,10 @@ public BinaryJedis(final String host) {
5657
}
5758
}
5859

60+
public BinaryJedis(final File unixDomainSocket) {
61+
client = new Client(unixDomainSocket);
62+
}
63+
5964
public BinaryJedis(final HostAndPort hp) {
6065
this(hp.getHost(), hp.getPort());
6166
}

src/main/java/redis/clients/jedis/Client.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static redis.clients.jedis.Protocol.toByteArray;
44

5+
import java.io.File;
56
import java.util.ArrayList;
67
import java.util.HashMap;
78
import java.util.List;
@@ -30,6 +31,10 @@ public Client(final String host) {
3031
super(host);
3132
}
3233

34+
public Client(final File unixDomainSocket) {
35+
super(unixDomainSocket);
36+
}
37+
3338
public Client(final String host, final int port) {
3439
super(host, port);
3540
}

src/main/java/redis/clients/jedis/Connection.java

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package redis.clients.jedis;
22

33
import java.io.Closeable;
4+
import java.io.File;
45
import java.io.IOException;
56
import java.net.InetSocketAddress;
67
import java.net.Socket;
8+
import java.net.SocketAddress;
79
import java.net.SocketException;
810
import java.util.ArrayList;
911
import java.util.List;
@@ -13,6 +15,8 @@
1315
import javax.net.ssl.SSLSocket;
1416
import javax.net.ssl.SSLSocketFactory;
1517

18+
import org.newsclub.net.unix.AFUNIXSocket;
19+
import org.newsclub.net.unix.AFUNIXSocketAddress;
1620
import redis.clients.jedis.commands.ProtocolCommand;
1721
import redis.clients.jedis.exceptions.JedisConnectionException;
1822
import redis.clients.jedis.exceptions.JedisDataException;
@@ -37,6 +41,7 @@ public class Connection implements Closeable {
3741
private SSLSocketFactory sslSocketFactory;
3842
private SSLParameters sslParameters;
3943
private HostnameVerifier hostnameVerifier;
44+
private File unixDomainSocket;
4045

4146
public Connection() {
4247
}
@@ -45,6 +50,10 @@ public Connection(final String host) {
4550
this.host = host;
4651
}
4752

53+
public Connection(final File unixDomainSocket) {
54+
this.unixDomainSocket = unixDomainSocket;
55+
}
56+
4857
public Connection(final String host, final int port) {
4958
this.host = host;
5059
this.port = port;
@@ -166,19 +175,27 @@ public void setPort(final int port) {
166175
public void connect() {
167176
if (!isConnected()) {
168177
try {
169-
socket = new Socket();
170-
// ->@wjw_add
171-
socket.setReuseAddress(true);
172-
socket.setKeepAlive(true); // Will monitor the TCP connection is
173-
// valid
174-
socket.setTcpNoDelay(true); // Socket buffer Whetherclosed, to
175-
// ensure timely delivery of data
176-
socket.setSoLinger(true, 0); // Control calls close () method,
177-
// the underlying socket is closed
178-
// immediately
179-
// <-@wjw_add
180-
181-
socket.connect(new InetSocketAddress(host, port), connectionTimeout);
178+
SocketAddress socketAddress;
179+
if (unixDomainSocket == null) {
180+
socket = new Socket();
181+
// ->@wjw_add
182+
socket.setReuseAddress(true);
183+
socket.setKeepAlive(true); // Will monitor the TCP connection is
184+
// valid
185+
socket.setTcpNoDelay(true); // Socket buffer Whetherclosed, to
186+
// ensure timely delivery of data
187+
socket.setSoLinger(true, 0); // Control calls close () method,
188+
// the underlying socket is closed
189+
// immediately
190+
// <-@wjw_add
191+
socketAddress = new InetSocketAddress(host, port);
192+
} else {
193+
// unix domain socket doesn't support above options
194+
socket = AFUNIXSocket.newStrictInstance();
195+
socketAddress = new AFUNIXSocketAddress(unixDomainSocket);
196+
}
197+
198+
socket.connect(socketAddress, connectionTimeout);
182199
socket.setSoTimeout(soTimeout);
183200

184201
if (ssl) {
@@ -201,8 +218,13 @@ public void connect() {
201218
inputStream = new RedisInputStream(socket.getInputStream());
202219
} catch (IOException ex) {
203220
broken = true;
204-
throw new JedisConnectionException("Failed connecting to host "
205-
+ host + ":" + port, ex);
221+
if (unixDomainSocket == null) {
222+
throw new JedisConnectionException("Failed connecting to host "
223+
+ host + ":" + port, ex);
224+
} else {
225+
throw new JedisConnectionException("Failed connecting to socket "
226+
+ unixDomainSocket, ex);
227+
}
206228
}
207229
}
208230
}

src/main/java/redis/clients/jedis/Jedis.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package redis.clients.jedis;
22

3+
import java.io.File;
34
import java.net.URI;
45
import java.util.AbstractMap;
56
import java.util.ArrayList;
@@ -42,6 +43,10 @@ public Jedis(final String host) {
4243
super(host);
4344
}
4445

46+
public Jedis(final File unixDomainSocket) {
47+
super(unixDomainSocket);
48+
}
49+
4550
public Jedis(final HostAndPort hp) {
4651
super(hp);
4752
}

src/test/java/redis/clients/jedis/tests/HostAndPortUtil.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package redis.clients.jedis.tests;
22

3+
import java.io.File;
34
import java.util.ArrayList;
45
import java.util.List;
56

@@ -10,6 +11,7 @@ public final class HostAndPortUtil {
1011
private static List<HostAndPort> redisHostAndPortList = new ArrayList<HostAndPort>();
1112
private static List<HostAndPort> sentinelHostAndPortList = new ArrayList<HostAndPort>();
1213
private static List<HostAndPort> clusterHostAndPortList = new ArrayList<HostAndPort>();
14+
private static List<File> redisUDSList = new ArrayList<File>();
1315

1416
private HostAndPortUtil(){
1517
throw new InstantiationError( "Must not instantiate this class" );
@@ -36,13 +38,17 @@ private HostAndPortUtil(){
3638
clusterHostAndPortList.add(new HostAndPort("localhost", 7383));
3739
clusterHostAndPortList.add(new HostAndPort("localhost", 7384));
3840

41+
redisUDSList.add(new File("/tmp/redis_6379.sock"));
42+
3943
String envRedisHosts = System.getProperty("redis-hosts");
4044
String envSentinelHosts = System.getProperty("sentinel-hosts");
4145
String envClusterHosts = System.getProperty("cluster-hosts");
46+
String envUDSHosts = System.getProperty("uds-hosts");
4247

4348
redisHostAndPortList = parseHosts(envRedisHosts, redisHostAndPortList);
4449
sentinelHostAndPortList = parseHosts(envSentinelHosts, sentinelHostAndPortList);
4550
clusterHostAndPortList = parseHosts(envClusterHosts, clusterHostAndPortList);
51+
redisUDSList = parseUDSHosts(envUDSHosts, redisUDSList);
4652
}
4753

4854
public static List<HostAndPort> parseHosts(String envHosts,
@@ -80,6 +86,19 @@ public static List<HostAndPort> parseHosts(String envHosts,
8086
return existingHostsAndPorts;
8187
}
8288

89+
public static List<File> parseUDSHosts(String envHosts, List<File> existingUDSHosts) {
90+
if (null != envHosts && 0 < envHosts.length()) {
91+
92+
String[] hostDefs = envHosts.split(",");
93+
94+
List<File> envUDSHosts = new ArrayList<>();
95+
for (String hostDef : hostDefs) {
96+
envUDSHosts.add(new File(hostDef));
97+
}
98+
}
99+
return existingUDSHosts;
100+
}
101+
83102
public static List<HostAndPort> getRedisServers() {
84103
return redisHostAndPortList;
85104
}
@@ -91,4 +110,8 @@ public static List<HostAndPort> getSentinelServers() {
91110
public static List<HostAndPort> getClusterServers() {
92111
return clusterHostAndPortList;
93112
}
113+
114+
public static List<File> getUDSServers() {
115+
return redisUDSList;
116+
}
94117
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package redis.clients.jedis.tests;
2+
3+
import org.junit.Test;
4+
import redis.clients.jedis.Jedis;
5+
6+
import java.io.File;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class UDSTest {
11+
protected static File udsHost = HostAndPortUtil.getUDSServers().get(0);
12+
@Test
13+
public void testCompareTo() {
14+
Jedis jedis = new Jedis(udsHost);
15+
assertEquals("PONG", jedis.ping());
16+
jedis.close();
17+
}
18+
}

0 commit comments

Comments
 (0)