Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #4857 - add setReuseAddress() to ClientConnector #4858

Merged
merged 2 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion jetty-io/src/main/java/org/eclipse/jetty/io/ClientConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -164,6 +166,16 @@ public void setBindAddress(SocketAddress bindAddress)
this.bindAddress = bindAddress;
}

public boolean getReuseAddress()
{
return reuseAddress;
}

public void setReuseAddress(boolean reuseAddress)
{
this.reuseAddress = reuseAddress;
}

@Override
protected void doStart() throws Exception
{
Expand Down Expand Up @@ -218,8 +230,10 @@ public void connect(SocketAddress address, Map<String, Object> context)
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ private ServerSocket createServerSocket(InetAddress address, int port) throws IO
if (_sslContextFactory == null)
{
ServerSocket server = new ServerSocket();
server.setReuseAddress(true);
server.bind(new InetSocketAddress(address, port));
return server;
}
Expand Down