From 434610b18c3b17be97de20dfa0ad8ca13089ab34 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Fri, 22 Feb 2013 13:45:17 -0800 Subject: [PATCH] Tell what the remote address was when ConnectException occurs. - Fixes #1082 --- .../netty/channel/aio/AbstractAioChannel.java | 23 ++++++++++++------- .../netty/channel/nio/AbstractNioChannel.java | 21 +++++++++++++---- .../netty/channel/oio/AbstractOioChannel.java | 6 +++++ 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/transport/src/main/java/io/netty/channel/aio/AbstractAioChannel.java b/transport/src/main/java/io/netty/channel/aio/AbstractAioChannel.java index bf41b52a29..34ab826b20 100755 --- a/transport/src/main/java/io/netty/channel/aio/AbstractAioChannel.java +++ b/transport/src/main/java/io/netty/channel/aio/AbstractAioChannel.java @@ -40,7 +40,7 @@ public abstract class AbstractAioChannel extends AbstractChannel { */ protected ChannelPromise connectPromise; protected ScheduledFuture connectTimeoutFuture; - private ConnectException connectTimeoutException; + private SocketAddress requestedRemoteAddress; /** * Creates a new instance. @@ -103,6 +103,7 @@ public abstract class AbstractAioChannel extends AbstractChannel { throw new IllegalStateException("connection attempt already made"); } connectPromise = promise; + requestedRemoteAddress = remoteAddress; doConnect(remoteAddress, localAddress, promise); @@ -112,19 +113,21 @@ public abstract class AbstractAioChannel extends AbstractChannel { connectTimeoutFuture = eventLoop().schedule(new Runnable() { @Override public void run() { - if (connectTimeoutException == null) { - connectTimeoutException = new ConnectException("connection timed out"); - } ChannelPromise connectFuture = connectPromise; - if (connectFuture != null && - connectFuture.tryFailure(connectTimeoutException)) { - pipeline().fireExceptionCaught(connectTimeoutException); + ConnectException cause = + new ConnectException("connection timed out: " + remoteAddress); + if (connectFuture != null && connectFuture.tryFailure(cause)) { close(voidFuture()); } } }, connectTimeoutMillis, TimeUnit.MILLISECONDS); } } catch (Throwable t) { + if (t instanceof ConnectException) { + Throwable newT = new ConnectException(t.getMessage() + ": " + remoteAddress); + newT.setStackTrace(t.getStackTrace()); + t = newT; + } promise.setFailure(t); closeIfClosed(); } @@ -139,8 +142,12 @@ public abstract class AbstractAioChannel extends AbstractChannel { } public void connectFailed(Throwable t) { + if (t instanceof ConnectException) { + Throwable newT = new ConnectException(t.getMessage() + ": " + requestedRemoteAddress); + newT.setStackTrace(t.getStackTrace()); + t = newT; + } connectPromise.setFailure(t); - pipeline().fireExceptionCaught(t); closeIfClosed(); } 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 0f999fefad..565a0e945d 100755 --- a/transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java +++ b/transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java @@ -54,7 +54,7 @@ public abstract class AbstractNioChannel extends AbstractChannel { */ private ChannelPromise connectPromise; private ScheduledFuture connectTimeoutFuture; - private ConnectException connectTimeoutException; + private SocketAddress requestedRemoteAddress; /** * Create a new instance @@ -174,6 +174,7 @@ public abstract class AbstractNioChannel extends AbstractChannel { } } else { connectPromise = promise; + requestedRemoteAddress = remoteAddress; // Schedule connect timeout. int connectTimeoutMillis = config().getConnectTimeoutMillis(); @@ -181,11 +182,10 @@ public abstract class AbstractNioChannel extends AbstractChannel { connectTimeoutFuture = eventLoop().schedule(new Runnable() { @Override public void run() { - if (connectTimeoutException == null) { - connectTimeoutException = new ConnectException("connection timed out"); - } ChannelPromise connectPromise = AbstractNioChannel.this.connectPromise; - if (connectPromise != null && connectPromise.tryFailure(connectTimeoutException)) { + ConnectException cause = + new ConnectException("connection timed out: " + remoteAddress); + if (connectPromise != null && connectPromise.tryFailure(cause)) { close(voidFuture()); } } @@ -193,6 +193,11 @@ public abstract class AbstractNioChannel extends AbstractChannel { } } } catch (Throwable t) { + if (t instanceof ConnectException) { + Throwable newT = new ConnectException(t.getMessage() + ": " + remoteAddress); + newT.setStackTrace(t.getStackTrace()); + t = newT; + } promise.setFailure(t); closeIfClosed(); } @@ -218,6 +223,12 @@ public abstract class AbstractNioChannel extends AbstractChannel { pipeline().fireChannelActive(); } } catch (Throwable t) { + if (t instanceof ConnectException) { + Throwable newT = new ConnectException(t.getMessage() + ": " + requestedRemoteAddress); + newT.setStackTrace(t.getStackTrace()); + t = newT; + } + connectPromise.setFailure(t); closeIfClosed(); } finally { diff --git a/transport/src/main/java/io/netty/channel/oio/AbstractOioChannel.java b/transport/src/main/java/io/netty/channel/oio/AbstractOioChannel.java index 6b5258db9f..8a3abd7a71 100644 --- a/transport/src/main/java/io/netty/channel/oio/AbstractOioChannel.java +++ b/transport/src/main/java/io/netty/channel/oio/AbstractOioChannel.java @@ -20,6 +20,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelPromise; import io.netty.channel.EventLoop; +import java.net.ConnectException; import java.net.SocketAddress; /** @@ -69,6 +70,11 @@ public abstract class AbstractOioChannel extends AbstractChannel { pipeline().fireChannelActive(); } } catch (Throwable t) { + if (t instanceof ConnectException) { + Throwable newT = new ConnectException(t.getMessage() + ": " + remoteAddress); + newT.setStackTrace(t.getStackTrace()); + t = newT; + } promise.setFailure(t); closeIfClosed(); }