From a6cc73b447b97016e1487f90ffc4002e6cd4d473 Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Fri, 31 Jan 2020 14:53:51 +0100 Subject: [PATCH] Artery hostname for client side of TCP connection, #28546 --- akka-remote/src/main/resources/reference.conf | 3 +++ .../main/scala/akka/remote/artery/ArterySettings.scala | 6 ++++++ .../akka/remote/artery/tcp/ArteryTcpTransport.scala | 10 ++++++++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/akka-remote/src/main/resources/reference.conf b/akka-remote/src/main/resources/reference.conf index 683368ef72..90ea5b8798 100644 --- a/akka-remote/src/main/resources/reference.conf +++ b/akka-remote/src/main/resources/reference.conf @@ -1087,6 +1087,9 @@ akka { tcp { # Timeout of establishing outbound connections. connection-timeout = 5 seconds + + # The local address that is used for the client side of the TCP connection. + outbound-client-hostname = "" } diff --git a/akka-remote/src/main/scala/akka/remote/artery/ArterySettings.scala b/akka-remote/src/main/scala/akka/remote/artery/ArterySettings.scala index 8a6a1df1df..d5fa667c81 100644 --- a/akka-remote/src/main/scala/akka/remote/artery/ArterySettings.scala +++ b/akka-remote/src/main/scala/akka/remote/artery/ArterySettings.scala @@ -223,6 +223,12 @@ private[akka] final class ArterySettings private (config: Config) { val ConnectionTimeout: FiniteDuration = config .getMillisDuration("connection-timeout") .requiring(interval => interval > Duration.Zero, "connection-timeout must be more than zero") + val OutboundClientHostname: Option[String] = { + config.getString("outbound-client-hostname") match { + case "" => None + case hostname => Some(hostname) + } + } } } diff --git a/akka-remote/src/main/scala/akka/remote/artery/tcp/ArteryTcpTransport.scala b/akka-remote/src/main/scala/akka/remote/artery/tcp/ArteryTcpTransport.scala index 80727c475f..473576036b 100644 --- a/akka-remote/src/main/scala/akka/remote/artery/tcp/ArteryTcpTransport.scala +++ b/akka-remote/src/main/scala/akka/remote/artery/tcp/ArteryTcpTransport.scala @@ -121,13 +121,17 @@ private[remote] class ArteryTcpTransport( val port = outboundContext.remoteAddress.port.get val remoteAddress = InetSocketAddress.createUnresolved(host, port) - def connectionFlow: Flow[ByteString, ByteString, Future[Tcp.OutgoingConnection]] = + def connectionFlow: Flow[ByteString, ByteString, Future[Tcp.OutgoingConnection]] = { + val localAddress = settings.Advanced.Tcp.OutboundClientHostname match { + case None => None + case Some(clientHostname) => Some(new InetSocketAddress(clientHostname, 0)) + } if (tlsEnabled) { val sslProvider = sslEngineProvider.get Tcp().outgoingConnectionWithTls( remoteAddress, createSSLEngine = () => sslProvider.createClientSSLEngine(host, port), - localAddress = None, + localAddress, options = Nil, connectTimeout = settings.Advanced.Tcp.ConnectionTimeout, idleTimeout = Duration.Inf, @@ -136,9 +140,11 @@ private[remote] class ArteryTcpTransport( } else { Tcp().outgoingConnection( remoteAddress, + localAddress, halfClose = true, // issue https://github.com/akka/akka/issues/24392 if set to false connectTimeout = settings.Advanced.Tcp.ConnectionTimeout) } + } def connectionFlowWithRestart: Flow[ByteString, ByteString, NotUsed] = { val restartCount = new AtomicInteger(0)