@@ -215,7 +215,7 @@ public void connect(
215215 socket .bind (localAddress );
216216 }
217217 conn .bind (socket );
218- configureSocket (socket , socketConfig );
218+ configureSocket (socket , socketConfig , true );
219219 socket .connect (remoteAddress , TimeValue .isPositive (connectTimeout ) ? connectTimeout .toMillisecondsIntBound () : 0 );
220220 conn .bind (socket );
221221 onAfterSocketConnect (context , endpointHost );
@@ -288,7 +288,7 @@ private void connectToUnixDomainSocket(
288288 final Socket socket = unixDomainSocketFactory .connectSocket (newSocket , unixDomainSocket ,
289289 connectTimeout );
290290 conn .bind (socket );
291- configureSocket (socket , socketConfig );
291+ configureSocket (socket , socketConfig , false );
292292 onAfterSocketConnect (context , endpointHost );
293293 if (LOG .isDebugEnabled ()) {
294294 LOG .debug ("{} {} connected to {}" , ConnPoolSupport .getId (conn ), endpointHost , unixDomainSocket );
@@ -312,14 +312,15 @@ private void connectToUnixDomainSocket(
312312 }
313313
314314 @ SuppressWarnings ("Since15" )
315- private static void configureSocket (final Socket socket , final SocketConfig socketConfig ) throws IOException {
315+ private static void configureSocket (
316+ final Socket socket ,
317+ final SocketConfig socketConfig ,
318+ final boolean isTcp
319+ ) throws IOException {
316320 final Timeout socketTimeout = socketConfig .getSoTimeout ();
317321 if (socketTimeout != null ) {
318322 socket .setSoTimeout (socketTimeout .toMillisecondsIntBound ());
319323 }
320- socket .setReuseAddress (socketConfig .isSoReuseAddress ());
321- socket .setTcpNoDelay (socketConfig .isTcpNoDelay ());
322- socket .setKeepAlive (socketConfig .isSoKeepAlive ());
323324 if (socketConfig .getRcvBufSize () > 0 ) {
324325 socket .setReceiveBufferSize (socketConfig .getRcvBufSize ());
325326 }
@@ -331,15 +332,21 @@ private static void configureSocket(final Socket socket, final SocketConfig sock
331332 if (linger >= 0 ) {
332333 socket .setSoLinger (true , linger );
333334 }
334- if (SUPPORTS_KEEPALIVE_OPTIONS ) {
335- if (socketConfig .getTcpKeepIdle () > 0 ) {
336- Sockets .setOption (socket , ExtendedSocketOptions .TCP_KEEPIDLE , socketConfig .getTcpKeepIdle ());
337- }
338- if (socketConfig .getTcpKeepInterval () > 0 ) {
339- Sockets .setOption (socket , ExtendedSocketOptions .TCP_KEEPINTERVAL , socketConfig .getTcpKeepInterval ());
340- }
341- if (socketConfig .getTcpKeepCount () > 0 ) {
342- Sockets .setOption (socket , ExtendedSocketOptions .TCP_KEEPCOUNT , socketConfig .getTcpKeepCount ());
335+ if (isTcp ) {
336+ socket .setReuseAddress (socketConfig .isSoReuseAddress ());
337+ socket .setTcpNoDelay (socketConfig .isTcpNoDelay ());
338+ socket .setKeepAlive (socketConfig .isSoKeepAlive ());
339+ if (SUPPORTS_KEEPALIVE_OPTIONS ) {
340+ if (socketConfig .getTcpKeepIdle () > 0 ) {
341+ Sockets .setOption (socket , ExtendedSocketOptions .TCP_KEEPIDLE , socketConfig .getTcpKeepIdle ());
342+ }
343+ if (socketConfig .getTcpKeepInterval () > 0 ) {
344+ Sockets .setOption (socket , ExtendedSocketOptions .TCP_KEEPINTERVAL ,
345+ socketConfig .getTcpKeepInterval ());
346+ }
347+ if (socketConfig .getTcpKeepCount () > 0 ) {
348+ Sockets .setOption (socket , ExtendedSocketOptions .TCP_KEEPCOUNT , socketConfig .getTcpKeepCount ());
349+ }
343350 }
344351 }
345352 }
0 commit comments