11package redis .clients .jedis ;
22
33import java .io .Closeable ;
4+ import java .io .File ;
45import java .io .IOException ;
56import java .net .InetSocketAddress ;
67import java .net .Socket ;
8+ import java .net .SocketAddress ;
79import java .net .SocketException ;
810import java .util .ArrayList ;
911import java .util .List ;
1315import javax .net .ssl .SSLSocket ;
1416import javax .net .ssl .SSLSocketFactory ;
1517
18+ import org .newsclub .net .unix .AFUNIXSocket ;
19+ import org .newsclub .net .unix .AFUNIXSocketAddress ;
1620import redis .clients .jedis .commands .ProtocolCommand ;
1721import redis .clients .jedis .exceptions .JedisConnectionException ;
1822import 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 }
0 commit comments