Add AbstractUnsafe.annotateConnectException()
Motivation: JDK's exception messages triggered by a connection attempt failure do not contain the related remote address in its message. We currently append the remote address to ConnectException's message, but I found that we need to cover more exception types such as SocketException. Modifications: - Add AbstractUnsafe.annotateConnectException() to de-duplicate the code that appends the remote address Result: - Less duplication - A transport implementor can annotate connection attempt failure message more easily
This commit is contained in:
parent
7f81227d61
commit
e4ab108a10
@ -549,13 +549,8 @@ public final class EpollSocketChannel extends AbstractEpollChannel implements So
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
if (t instanceof ConnectException) {
|
|
||||||
Throwable newT = new ConnectException(t.getMessage() + ": " + remoteAddress);
|
|
||||||
newT.setStackTrace(t.getStackTrace());
|
|
||||||
t = newT;
|
|
||||||
}
|
|
||||||
closeIfClosed();
|
closeIfClosed();
|
||||||
promise.tryFailure(t);
|
promise.tryFailure(annotateConnectException(t, remoteAddress));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -607,13 +602,7 @@ public final class EpollSocketChannel extends AbstractEpollChannel implements So
|
|||||||
}
|
}
|
||||||
fulfillConnectPromise(connectPromise, wasActive);
|
fulfillConnectPromise(connectPromise, wasActive);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
if (t instanceof ConnectException) {
|
fulfillConnectPromise(connectPromise, annotateConnectException(t, requestedRemoteAddress));
|
||||||
Throwable newT = new ConnectException(t.getMessage() + ": " + requestedRemoteAddress);
|
|
||||||
newT.setStackTrace(t.getStackTrace());
|
|
||||||
t = newT;
|
|
||||||
}
|
|
||||||
|
|
||||||
fulfillConnectPromise(connectPromise, t);
|
|
||||||
} finally {
|
} finally {
|
||||||
if (!connectStillInProgress) {
|
if (!connectStillInProgress) {
|
||||||
// Check for null as the connectTimeoutFuture is only created if a connectTimeoutMillis > 0 is used
|
// Check for null as the connectTimeoutFuture is only created if a connectTimeoutMillis > 0 is used
|
||||||
|
@ -24,12 +24,14 @@ import io.netty.util.internal.PlatformDependent;
|
|||||||
import io.netty.util.internal.logging.InternalLogger;
|
import io.netty.util.internal.logging.InternalLogger;
|
||||||
import io.netty.util.internal.logging.InternalLoggerFactory;
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
||||||
|
|
||||||
|
import java.net.ConnectException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.NoRouteToHostException;
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
|
import java.net.SocketException;
|
||||||
import java.nio.channels.ClosedChannelException;
|
import java.nio.channels.ClosedChannelException;
|
||||||
import java.nio.channels.NotYetConnectedException;
|
import java.nio.channels.NotYetConnectedException;
|
||||||
import java.util.concurrent.RejectedExecutionException;
|
import java.util.concurrent.RejectedExecutionException;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A skeletal {@link Channel} implementation.
|
* A skeletal {@link Channel} implementation.
|
||||||
@ -847,6 +849,27 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
|
|||||||
logger.warn("Can't invoke task later as EventLoop rejected it", e);
|
logger.warn("Can't invoke task later as EventLoop rejected it", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends the remote address to the message of the exceptions caused by connection attempt failure.
|
||||||
|
*/
|
||||||
|
protected final Throwable annotateConnectException(Throwable cause, SocketAddress remoteAddress) {
|
||||||
|
if (cause instanceof ConnectException) {
|
||||||
|
Throwable newT = new ConnectException(cause.getMessage() + ": " + remoteAddress);
|
||||||
|
newT.setStackTrace(cause.getStackTrace());
|
||||||
|
cause = newT;
|
||||||
|
} else if (cause instanceof NoRouteToHostException) {
|
||||||
|
Throwable newT = new NoRouteToHostException(cause.getMessage() + ": " + remoteAddress);
|
||||||
|
newT.setStackTrace(cause.getStackTrace());
|
||||||
|
cause = newT;
|
||||||
|
} else if (cause instanceof SocketException) {
|
||||||
|
Throwable newT = new SocketException(cause.getMessage() + ": " + remoteAddress);
|
||||||
|
newT.setStackTrace(cause.getStackTrace());
|
||||||
|
cause = newT;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cause;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -34,7 +34,6 @@ import io.netty.util.internal.logging.InternalLogger;
|
|||||||
import io.netty.util.internal.logging.InternalLoggerFactory;
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ConnectException;
|
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
import java.nio.channels.CancelledKeyException;
|
import java.nio.channels.CancelledKeyException;
|
||||||
import java.nio.channels.SelectableChannel;
|
import java.nio.channels.SelectableChannel;
|
||||||
@ -228,12 +227,7 @@ public abstract class AbstractNioChannel extends AbstractChannel {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
if (t instanceof ConnectException) {
|
promise.tryFailure(annotateConnectException(t, remoteAddress));
|
||||||
Throwable newT = new ConnectException(t.getMessage() + ": " + remoteAddress);
|
|
||||||
newT.setStackTrace(t.getStackTrace());
|
|
||||||
t = newT;
|
|
||||||
}
|
|
||||||
promise.tryFailure(t);
|
|
||||||
closeIfClosed();
|
closeIfClosed();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -282,13 +276,7 @@ public abstract class AbstractNioChannel extends AbstractChannel {
|
|||||||
doFinishConnect();
|
doFinishConnect();
|
||||||
fulfillConnectPromise(connectPromise, wasActive);
|
fulfillConnectPromise(connectPromise, wasActive);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
if (t instanceof ConnectException) {
|
fulfillConnectPromise(connectPromise, annotateConnectException(t, requestedRemoteAddress));
|
||||||
Throwable newT = new ConnectException(t.getMessage() + ": " + requestedRemoteAddress);
|
|
||||||
newT.setStackTrace(t.getStackTrace());
|
|
||||||
t = newT;
|
|
||||||
}
|
|
||||||
|
|
||||||
fulfillConnectPromise(connectPromise, t);
|
|
||||||
} finally {
|
} finally {
|
||||||
// Check for null as the connectTimeoutFuture is only created if a connectTimeoutMillis > 0 is used
|
// Check for null as the connectTimeoutFuture is only created if a connectTimeoutMillis > 0 is used
|
||||||
// See https://github.com/netty/netty/issues/1770
|
// See https://github.com/netty/netty/issues/1770
|
||||||
|
@ -21,7 +21,6 @@ import io.netty.channel.ChannelPromise;
|
|||||||
import io.netty.channel.EventLoop;
|
import io.netty.channel.EventLoop;
|
||||||
import io.netty.channel.ThreadPerChannelEventLoop;
|
import io.netty.channel.ThreadPerChannelEventLoop;
|
||||||
|
|
||||||
import java.net.ConnectException;
|
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -75,12 +74,7 @@ public abstract class AbstractOioChannel extends AbstractChannel {
|
|||||||
pipeline().fireChannelActive();
|
pipeline().fireChannelActive();
|
||||||
}
|
}
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
if (t instanceof ConnectException) {
|
safeSetFailure(promise, annotateConnectException(t, remoteAddress));
|
||||||
Throwable newT = new ConnectException(t.getMessage() + ": " + remoteAddress);
|
|
||||||
newT.setStackTrace(t.getStackTrace());
|
|
||||||
t = newT;
|
|
||||||
}
|
|
||||||
safeSetFailure(promise, t);
|
|
||||||
closeIfClosed();
|
closeIfClosed();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user