How to detect connection timeout on client side?

Trustin Lee (이희승) trustin at gmail.com
Mon Sep 21 21:37:10 EDT 2009


Hi XuQing,

Your research is indeed correct.  Nice work. :)

You will also be notified with the 'exceptionCaught' event as you
already might have noticed.

The ConnectException is not raised when you call 'connect()'
immediately because it might take longer than expected.  Please note
that all operations in Netty are *non-blocking* and *asynchronous*.
You will find it very useful when making massive amount of connection
attempt simultaneously because you can do that with only *one* thread.

HTH

— Trustin Lee, http://gleamynode.net/

On Tue, Sep 22, 2009 at 10:26 AM, XuQing Tan <missedone at gmail.com> wrote:
> Hi, folks,
>
> I'm recently investigating some NIO framework, and I find that Netty is
> awesome, I'd like to adopt it into our project.
> But here's my first question: how to detect the conenction timeout on client
> side?
> As the secureChat example shown, client connects to host via code:
>
> ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
>
> a ChannelFuture returned immediately, which tells the connection status,
> e.g. canceled or done, but no TIMEOUT.
>
> by digging some codes, I got this:
>
> org.jboss.netty.channel.socket.nio.NioClientSocketPipelineSink.Boss.processConnectTimeout(Set<SelectionKey>,
> long)
>
>         private void processConnectTimeout(Set<SelectionKey> keys, long
> currentTimeNanos) {
>             ConnectException cause = null;
>             for (SelectionKey k: keys) {
>                 if (!k.isValid()) {
>                     continue;
>                 }
>
>                 NioClientSocketChannel ch = (NioClientSocketChannel)
> k.attachment();
>                 if (ch.connectDeadlineNanos > 0 &&
>                         currentTimeNanos >= ch.connectDeadlineNanos) {
>
>                     if (cause == null) {
>                         cause = new ConnectException("connection timed
> out");
>                     }
>
>                     ch.connectFuture.setFailure(cause);
>                     fireExceptionCaught(ch, cause);
>                     close(k);
>                 }
>             }
>         }
>
> so, from the view above, maybe we can test the timeout status int the
> ChannelFutureListener via:
>
> new ChannelFutureListener() {
>
> public void operationComplete(ChannelFuture future) {
>
> if (future.getCause() instanceof ConnectException) {
> // deal with timeout
> ....
> }
>
> }
> }
>
> is that correct? but a little weird, I think.
>
>
>
> Thanks & Best Regards!
>
>               ///
>              (. .)
>  --------ooO--(_)--Ooo--------
>  |         Nick Tan          |
>  -----------------------------
>
>
> _______________________________________________
> netty-users mailing list
> netty-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/netty-users
>
>



More information about the netty-users mailing list