From 71d034593f8284515452aad2b06afb7c7b041095 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 21 Sep 2020 21:32:20 +0200 Subject: [PATCH] Only create ConnectTimeoutException if really needed (#10595) Motivation: Creating exceptions is expensive so we should only do so if really needed. Modifications: Only create the ConnectTimeoutException if we really need it. Result: Less overhead --- .../java/io/netty/channel/epoll/AbstractEpollChannel.java | 7 ++++--- .../io/netty/channel/kqueue/AbstractKQueueChannel.java | 7 ++++--- .../main/java/io/netty/channel/nio/AbstractNioChannel.java | 6 +++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollChannel.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollChannel.java index 8e0515303a..4a53a8dbac 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollChannel.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollChannel.java @@ -31,6 +31,7 @@ import io.netty.channel.ChannelPromise; import io.netty.channel.ConnectTimeoutException; import io.netty.channel.EventLoop; import io.netty.channel.RecvByteBufAllocator; +import io.netty.channel.nio.AbstractNioChannel; import io.netty.channel.socket.ChannelInputShutdownEvent; import io.netty.channel.socket.ChannelInputShutdownReadComplete; import io.netty.channel.socket.SocketChannelConfig; @@ -572,9 +573,9 @@ abstract class AbstractEpollChannel extends AbstractChannel implements UnixChann @Override public void run() { ChannelPromise connectPromise = AbstractEpollChannel.this.connectPromise; - ConnectTimeoutException cause = - new ConnectTimeoutException("connection timed out: " + remoteAddress); - if (connectPromise != null && connectPromise.tryFailure(cause)) { + if (connectPromise != null && !connectPromise.isDone() + && connectPromise.tryFailure(new ConnectTimeoutException( + "connection timed out: " + remoteAddress))) { close(voidPromise()); } } diff --git a/transport-native-kqueue/src/main/java/io/netty/channel/kqueue/AbstractKQueueChannel.java b/transport-native-kqueue/src/main/java/io/netty/channel/kqueue/AbstractKQueueChannel.java index 02d2a1c28d..d5c221ffa4 100644 --- a/transport-native-kqueue/src/main/java/io/netty/channel/kqueue/AbstractKQueueChannel.java +++ b/transport-native-kqueue/src/main/java/io/netty/channel/kqueue/AbstractKQueueChannel.java @@ -31,6 +31,7 @@ import io.netty.channel.ChannelPromise; import io.netty.channel.ConnectTimeoutException; import io.netty.channel.EventLoop; import io.netty.channel.RecvByteBufAllocator; +import io.netty.channel.nio.AbstractNioChannel; import io.netty.channel.socket.ChannelInputShutdownEvent; import io.netty.channel.socket.ChannelInputShutdownReadComplete; import io.netty.channel.socket.SocketChannelConfig; @@ -558,9 +559,9 @@ abstract class AbstractKQueueChannel extends AbstractChannel implements UnixChan @Override public void run() { ChannelPromise connectPromise = AbstractKQueueChannel.this.connectPromise; - ConnectTimeoutException cause = - new ConnectTimeoutException("connection timed out: " + remoteAddress); - if (connectPromise != null && connectPromise.tryFailure(cause)) { + if (connectPromise != null && !connectPromise.isDone() + && connectPromise.tryFailure(new ConnectTimeoutException( + "connection timed out: " + remoteAddress))) { close(voidPromise()); } } diff --git a/transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java b/transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java index a1c4520344..2db6b7c6ee 100644 --- a/transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java +++ b/transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java @@ -258,9 +258,9 @@ public abstract class AbstractNioChannel extends AbstractChannel { @Override public void run() { ChannelPromise connectPromise = AbstractNioChannel.this.connectPromise; - ConnectTimeoutException cause = - new ConnectTimeoutException("connection timed out: " + remoteAddress); - if (connectPromise != null && connectPromise.tryFailure(cause)) { + if (connectPromise != null && !connectPromise.isDone() + && connectPromise.tryFailure(new ConnectTimeoutException( + "connection timed out: " + remoteAddress))) { close(voidPromise()); } }