Issue #6661 - Make ServerConnector socket options setting optional

This commit is contained in:
Joakim Erdfelt 2021-08-24 16:18:34 -05:00
parent e3e630bdf0
commit ec842078a2
1 changed files with 15 additions and 2 deletions

View File

@ -19,6 +19,7 @@ import java.net.InetSocketAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.net.SocketException; import java.net.SocketException;
import java.net.SocketOption;
import java.net.StandardSocketOptions; import java.net.StandardSocketOptions;
import java.nio.channels.Channel; import java.nio.channels.Channel;
import java.nio.channels.SelectableChannel; import java.nio.channels.SelectableChannel;
@ -332,10 +333,10 @@ public class ServerConnector extends AbstractNetworkConnector
{ {
InetSocketAddress bindAddress = getHost() == null ? new InetSocketAddress(getPort()) : new InetSocketAddress(getHost(), getPort()); InetSocketAddress bindAddress = getHost() == null ? new InetSocketAddress(getPort()) : new InetSocketAddress(getHost(), getPort());
serverChannel = ServerSocketChannel.open(); serverChannel = ServerSocketChannel.open();
setSocketOption(serverChannel, StandardSocketOptions.SO_REUSEADDR, getReuseAddress());
setSocketOption(serverChannel, StandardSocketOptions.SO_REUSEPORT, isReusePort());
try try
{ {
serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, getReuseAddress());
serverChannel.setOption(StandardSocketOptions.SO_REUSEPORT, isReusePort());
serverChannel.bind(bindAddress, getAcceptQueueSize()); serverChannel.bind(bindAddress, getAcceptQueueSize());
} }
catch (Throwable e) catch (Throwable e)
@ -348,6 +349,18 @@ public class ServerConnector extends AbstractNetworkConnector
return serverChannel; return serverChannel;
} }
private <T> void setSocketOption(ServerSocketChannel channel, SocketOption<T> name, T value)
{
try
{
channel.setOption(name, value);
}
catch (Throwable t)
{
LOG.warn("Unable to set socket option {} to {}", name, value, t);
}
}
@Override @Override
public void close() public void close()
{ {