Skip to content

Commit bfbe185

Browse files
committed
Add Unix Domain Socket support
UDS addrss should be given as an absolute path
1 parent 48af512 commit bfbe185

File tree

5 files changed

+81
-8
lines changed

5 files changed

+81
-8
lines changed

Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,18 @@ cluster-enabled yes
202202
cluster-config-file /tmp/redis_cluster_node6.conf
203203
endef
204204

205+
# UDS REDIS NODES
206+
define REDIS_UDS
207+
daemonize yes
208+
port 0
209+
pidfile /tmp/redis_uds.pid
210+
logfile /tmp/redis_uds.log
211+
unixsocket /tmp/redis_6379.sock
212+
unixsocketperm 777
213+
save ""
214+
appendonly no
215+
endef
216+
205217
#STUNNEL
206218
define STUNNEL_CONF
207219
cert = src/test/resources/private.pem
@@ -228,6 +240,7 @@ export REDIS_CLUSTER_NODE3_CONF
228240
export REDIS_CLUSTER_NODE4_CONF
229241
export REDIS_CLUSTER_NODE5_CONF
230242
export REDIS_CLUSTER_NODE6_CONF
243+
export REDIS_UDS
231244
export STUNNEL_CONF
232245
export STUNNEL_BIN
233246

@@ -258,6 +271,7 @@ start: stunnel cleanup
258271
echo "$$REDIS_CLUSTER_NODE4_CONF" | redis-server -
259272
echo "$$REDIS_CLUSTER_NODE5_CONF" | redis-server -
260273
echo "$$REDIS_CLUSTER_NODE6_CONF" | redis-server -
274+
echo "$$REDIS_UDS" | redis-server -
261275

262276
cleanup:
263277
- rm -vf /tmp/redis_cluster_node*.conf 2>/dev/null
@@ -285,6 +299,7 @@ stop:
285299
kill `cat /tmp/redis_cluster_node4.pid` || true
286300
kill `cat /tmp/redis_cluster_node5.pid` || true
287301
kill `cat /tmp/redis_cluster_node6.pid` || true
302+
kill `cat /tmp/redis_uds.pid` || true
288303
kill `cat /tmp/stunnel.pid` || true
289304
rm -f /tmp/sentinel1.conf
290305
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

@@ -66,6 +67,11 @@
6667
<type>jar</type>
6768
<scope>compile</scope>
6869
</dependency>
70+
<dependency>
71+
<groupId>com.kohlschutter.junixsocket</groupId>
72+
<artifactId>junixsocket-native-common</artifactId>
73+
<version>2.0.4</version>
74+
</dependency>
6975
</dependencies>
7076

7177
<distributionManagement>

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

Lines changed: 23 additions & 8 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.io.Closeable;
45
import java.io.IOException;
56
import java.net.InetSocketAddress;
@@ -8,6 +9,9 @@
89
import java.util.ArrayList;
910
import java.util.List;
1011

12+
import org.newsclub.net.unix.AFUNIXSocket;
13+
import org.newsclub.net.unix.AFUNIXSocketAddress;
14+
1115
import javax.net.ssl.HostnameVerifier;
1216
import javax.net.ssl.SSLParameters;
1317
import javax.net.ssl.SSLSocket;
@@ -38,6 +42,11 @@ public class Connection implements Closeable {
3842
private SSLParameters sslParameters;
3943
private HostnameVerifier hostnameVerifier;
4044

45+
private boolean isUDSConnection(String host) {
46+
File udsFile = new File(host);
47+
return udsFile.isAbsolute() && udsFile.exists();
48+
}
49+
4150
public Connection() {
4251
}
4352

@@ -167,19 +176,24 @@ public void setPort(final int port) {
167176
public void connect() {
168177
if (!isConnected()) {
169178
try {
170-
socket = new Socket();
171-
// ->@wjw_add
172-
socket.setReuseAddress(true);
179+
if (isUDSConnection(host)) {
180+
socket = AFUNIXSocket.newStrictInstance();
181+
socket.connect(new AFUNIXSocketAddress(new File(host)), connectionTimeout);
182+
} else {
183+
socket = new Socket();
184+
socket.connect(new InetSocketAddress(host, port), connectionTimeout);
185+
// ->@wjw_add
186+
socket.setReuseAddress(true);
187+
socket.setTcpNoDelay(true); // Socket buffer Whetherclosed, to
188+
// ensure timely delivery of data
189+
}
173190
socket.setKeepAlive(true); // Will monitor the TCP connection is
174191
// valid
175-
socket.setTcpNoDelay(true); // Socket buffer Whetherclosed, to
176-
// ensure timely delivery of data
177192
socket.setSoLinger(true, 0); // Control calls close () method,
178193
// the underlying socket is closed
179194
// immediately
180195
// <-@wjw_add
181196

182-
socket.connect(new InetSocketAddress(host, port), connectionTimeout);
183197
socket.setSoTimeout(soTimeout);
184198

185199
if (ssl) {
@@ -227,8 +241,9 @@ public void disconnect() {
227241
}
228242

229243
public boolean isConnected() {
230-
return socket != null && socket.isBound() && !socket.isClosed() && socket.isConnected()
231-
&& !socket.isInputShutdown() && !socket.isOutputShutdown();
244+
return socket != null && (socket instanceof AFUNIXSocket ? true : socket.isBound()) &&
245+
!socket.isClosed() && socket.isConnected() && !socket.isInputShutdown() &&
246+
!socket.isOutputShutdown();
232247
}
233248

234249
public String getStatusCodeReply() {

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

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

33
import java.util.ArrayList;
4+
import java.util.Arrays;
45
import java.util.List;
56

67
import redis.clients.jedis.HostAndPort;
@@ -10,6 +11,7 @@ public 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<String> redisUDSList = new ArrayList<String>();
1315

1416
static {
1517
redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT));
@@ -32,13 +34,17 @@ public class HostAndPortUtil {
3234
clusterHostAndPortList.add(new HostAndPort("localhost", 7383));
3335
clusterHostAndPortList.add(new HostAndPort("localhost", 7384));
3436

37+
redisUDSList.add(new String("/tmp/redis_6379.sock"));
38+
3539
String envRedisHosts = System.getProperty("redis-hosts");
3640
String envSentinelHosts = System.getProperty("sentinel-hosts");
3741
String envClusterHosts = System.getProperty("cluster-hosts");
42+
String envUDSHosts = System.getProperty("uds-hosts");
3843

3944
redisHostAndPortList = parseHosts(envRedisHosts, redisHostAndPortList);
4045
sentinelHostAndPortList = parseHosts(envSentinelHosts, sentinelHostAndPortList);
4146
clusterHostAndPortList = parseHosts(envClusterHosts, clusterHostAndPortList);
47+
redisUDSList = parseUDSHosts(envUDSHosts, redisUDSList);
4248
}
4349

4450
public static List<HostAndPort> parseHosts(String envHosts,
@@ -76,6 +82,18 @@ public static List<HostAndPort> parseHosts(String envHosts,
7682
return existingHostsAndPorts;
7783
}
7884

85+
public static List<String> parseUDSHosts(String envHosts, List<String> existingUDSHosts) {
86+
if (null != envHosts && 0 < envHosts.length()) {
87+
88+
String[] hostDefs = envHosts.split(",");
89+
90+
if (null != hostDefs) {
91+
return Arrays.asList(hostDefs);
92+
}
93+
}
94+
return existingUDSHosts;
95+
}
96+
7997
public static List<HostAndPort> getRedisServers() {
8098
return redisHostAndPortList;
8199
}
@@ -87,4 +105,8 @@ public static List<HostAndPort> getSentinelServers() {
87105
public static List<HostAndPort> getClusterServers() {
88106
return clusterHostAndPortList;
89107
}
108+
109+
public static List<String> getUDSServers() {
110+
return redisUDSList;
111+
}
90112
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package redis.clients.jedis.tests;
2+
3+
import org.junit.Test;
4+
import static org.junit.Assert.assertEquals;
5+
import redis.clients.jedis.Jedis;
6+
7+
public class UDSTest {
8+
protected static String udsHost = HostAndPortUtil.getUDSServers().get(0);
9+
@Test
10+
public void testCompareTo() {
11+
Jedis jedis = new Jedis(udsHost);
12+
assertEquals("PONG", jedis.ping());
13+
jedis.close();
14+
}
15+
}

0 commit comments

Comments
 (0)