Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Java NIO AsynchronousSocketChannel connect timeout

Looking at JDK 1.7 API. I cannot seem to be able to set a connection timeout on an AsynchonousSocketChannel. Is there anyway I can setup a connection timeout on such a channel?

Thanks.

like image 799
Joe Lin Avatar asked Dec 23 '13 23:12

Joe Lin


1 Answers

The answer is: you can't.

The first thing to understand is how a TCP connect works. The kernel is sending SYN packets, backing off the time between each retry. This can be tuned via kernel parameters. An article covering this in detail (for linux) can be found here

To give you an idea of what's involved to implement your own shorter timeout for a socket connect is to put the socket in nonblocking mode, throw it in a select() with a timeout, then use getsockopt() to see what happened. This StackOverflow answer shows how this works.

With NIO.2, the connection process is handled for you using threads that you don't have access to. Unfortunately there's no way to tell it you want a shorter timeout on connects; it just calls your completion handler / notifies the Future when the connection either succeeds of fails (including timing out).

You do have the option of calling get(timeout, unit) on the returned Future, then cancelling the Futureif it times out ... but that means if you want the connect to be async you have to add another layer of threading / callbacks and may as well just implement your own async thing with nio.

One last thing worth mentioning since you're looking at async network stuff is that Netty does give this to you (using Nio):

Bootstrap bootstrap = new Bootstrap()
            .group(new NioEventLoopGroup())
            .channel(NioSocketChannel.class)
            .remoteAddress(new InetSocketAddress(remoteAddress, port))
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeout);

ChannelFuture f = bootstrap.connect();

And you can register a listener with that ChannelFuture for a callback.

like image 123
Brian Roach Avatar answered Sep 28 '22 03:09

Brian Roach