From 1d424a304bc848e44cb8676bcb36ef218babb2d4 Mon Sep 17 00:00:00 2001 From: Ludovic Orban Date: Fri, 8 May 2020 14:45:38 +0200 Subject: [PATCH] Add setReuseAddress() to ClientConnector Signed-off-by: Ludovic Orban --- .../org/eclipse/jetty/io/ClientConnector.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/ClientConnector.java b/jetty-io/src/main/java/org/eclipse/jetty/io/ClientConnector.java index e5d8730b10c..76fbcff5cd3 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/ClientConnector.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/ClientConnector.java @@ -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);