Tell what the remote address was when ConnectException occurs.

- Fixes #1082
This commit is contained in:
Trustin Lee 2013-02-22 13:45:17 -08:00
parent 58c72acd42
commit 434610b18c
3 changed files with 37 additions and 13 deletions

View File

@ -40,7 +40,7 @@ public abstract class AbstractAioChannel extends AbstractChannel {
*/ */
protected ChannelPromise connectPromise; protected ChannelPromise connectPromise;
protected ScheduledFuture<?> connectTimeoutFuture; protected ScheduledFuture<?> connectTimeoutFuture;
private ConnectException connectTimeoutException; private SocketAddress requestedRemoteAddress;
/** /**
* Creates a new instance. * Creates a new instance.
@ -103,6 +103,7 @@ public abstract class AbstractAioChannel extends AbstractChannel {
throw new IllegalStateException("connection attempt already made"); throw new IllegalStateException("connection attempt already made");
} }
connectPromise = promise; connectPromise = promise;
requestedRemoteAddress = remoteAddress;
doConnect(remoteAddress, localAddress, promise); doConnect(remoteAddress, localAddress, promise);
@ -112,19 +113,21 @@ public abstract class AbstractAioChannel extends AbstractChannel {
connectTimeoutFuture = eventLoop().schedule(new Runnable() { connectTimeoutFuture = eventLoop().schedule(new Runnable() {
@Override @Override
public void run() { public void run() {
if (connectTimeoutException == null) {
connectTimeoutException = new ConnectException("connection timed out");
}
ChannelPromise connectFuture = connectPromise; ChannelPromise connectFuture = connectPromise;
if (connectFuture != null && ConnectException cause =
connectFuture.tryFailure(connectTimeoutException)) { new ConnectException("connection timed out: " + remoteAddress);
pipeline().fireExceptionCaught(connectTimeoutException); if (connectFuture != null && connectFuture.tryFailure(cause)) {
close(voidFuture()); close(voidFuture());
} }
} }
}, connectTimeoutMillis, TimeUnit.MILLISECONDS); }, connectTimeoutMillis, TimeUnit.MILLISECONDS);
} }
} catch (Throwable t) { } catch (Throwable t) {
if (t instanceof ConnectException) {
Throwable newT = new ConnectException(t.getMessage() + ": " + remoteAddress);
newT.setStackTrace(t.getStackTrace());
t = newT;
}
promise.setFailure(t); promise.setFailure(t);
closeIfClosed(); closeIfClosed();
} }
@ -139,8 +142,12 @@ public abstract class AbstractAioChannel extends AbstractChannel {
} }
public void connectFailed(Throwable t) { 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); connectPromise.setFailure(t);
pipeline().fireExceptionCaught(t);
closeIfClosed(); closeIfClosed();
} }

View File

@ -54,7 +54,7 @@ public abstract class AbstractNioChannel extends AbstractChannel {
*/ */
private ChannelPromise connectPromise; private ChannelPromise connectPromise;
private ScheduledFuture<?> connectTimeoutFuture; private ScheduledFuture<?> connectTimeoutFuture;
private ConnectException connectTimeoutException; private SocketAddress requestedRemoteAddress;
/** /**
* Create a new instance * Create a new instance
@ -174,6 +174,7 @@ public abstract class AbstractNioChannel extends AbstractChannel {
} }
} else { } else {
connectPromise = promise; connectPromise = promise;
requestedRemoteAddress = remoteAddress;
// Schedule connect timeout. // Schedule connect timeout.
int connectTimeoutMillis = config().getConnectTimeoutMillis(); int connectTimeoutMillis = config().getConnectTimeoutMillis();
@ -181,11 +182,10 @@ public abstract class AbstractNioChannel extends AbstractChannel {
connectTimeoutFuture = eventLoop().schedule(new Runnable() { connectTimeoutFuture = eventLoop().schedule(new Runnable() {
@Override @Override
public void run() { public void run() {
if (connectTimeoutException == null) {
connectTimeoutException = new ConnectException("connection timed out");
}
ChannelPromise connectPromise = AbstractNioChannel.this.connectPromise; 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()); close(voidFuture());
} }
} }
@ -193,6 +193,11 @@ public abstract class AbstractNioChannel extends AbstractChannel {
} }
} }
} catch (Throwable t) { } catch (Throwable t) {
if (t instanceof ConnectException) {
Throwable newT = new ConnectException(t.getMessage() + ": " + remoteAddress);
newT.setStackTrace(t.getStackTrace());
t = newT;
}
promise.setFailure(t); promise.setFailure(t);
closeIfClosed(); closeIfClosed();
} }
@ -218,6 +223,12 @@ public abstract class AbstractNioChannel extends AbstractChannel {
pipeline().fireChannelActive(); pipeline().fireChannelActive();
} }
} catch (Throwable t) { } catch (Throwable t) {
if (t instanceof ConnectException) {
Throwable newT = new ConnectException(t.getMessage() + ": " + requestedRemoteAddress);
newT.setStackTrace(t.getStackTrace());
t = newT;
}
connectPromise.setFailure(t); connectPromise.setFailure(t);
closeIfClosed(); closeIfClosed();
} finally { } finally {

View File

@ -20,6 +20,7 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelPromise; import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoop; import io.netty.channel.EventLoop;
import java.net.ConnectException;
import java.net.SocketAddress; import java.net.SocketAddress;
/** /**
@ -69,6 +70,11 @@ public abstract class AbstractOioChannel extends AbstractChannel {
pipeline().fireChannelActive(); pipeline().fireChannelActive();
} }
} catch (Throwable t) { } catch (Throwable t) {
if (t instanceof ConnectException) {
Throwable newT = new ConnectException(t.getMessage() + ": " + remoteAddress);
newT.setStackTrace(t.getStackTrace());
t = newT;
}
promise.setFailure(t); promise.setFailure(t);
closeIfClosed(); closeIfClosed();
} }