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 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();
}

View File

@ -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 {

View File

@ -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();
}