Merge pull request #4858 from lorban/jetty-10.0.x-4857-setReuseAddress

Jetty 10.0.x 4857 set reuse address
This commit is contained in:
Simone Bordet 2020-05-08 18:02:34 +02:00 committed by GitHub
commit aa52d67dbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 1 deletions

View File

@ -21,6 +21,7 @@ package org.eclipse.jetty.io;
import java.io.IOException;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.StandardSocketOptions;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
@ -57,6 +58,7 @@ public class ClientConnector extends ContainerLifeCycle
private Duration connectTimeout = Duration.ofSeconds(5);
private Duration idleTimeout = Duration.ofSeconds(30);
private SocketAddress bindAddress;
private boolean reuseAddress = true;
public Executor getExecutor()
{
@ -164,6 +166,16 @@ public class ClientConnector extends ContainerLifeCycle
this.bindAddress = bindAddress;
}
public boolean getReuseAddress()
{
return reuseAddress;
}
public void setReuseAddress(boolean reuseAddress)
{
this.reuseAddress = reuseAddress;
}
@Override
protected void doStart() throws Exception
{
@ -218,8 +230,10 @@ public class ClientConnector extends ContainerLifeCycle
SocketAddress bindAddress = getBindAddress();
if (bindAddress != null)
{
boolean reuseAddress = getReuseAddress();
if (LOG.isDebugEnabled())
LOG.debug("Binding to {} to connect to {}", bindAddress, address);
LOG.debug("Binding to {} to connect to {}{}", bindAddress, address, (reuseAddress ? " reusing address" : ""));
channel.setOption(StandardSocketOptions.SO_REUSEADDR, reuseAddress);
channel.bind(bindAddress);
}
configure(channel);

View File

@ -243,6 +243,7 @@ public class ConnectorServer extends AbstractLifeCycle
if (_sslContextFactory == null)
{
ServerSocket server = new ServerSocket();
server.setReuseAddress(true);
server.bind(new InetSocketAddress(address, port));
return server;
}