Lee Packham

Software Engineer, UX Designer, Opinionated, Husband

Making HTTP Client Calls With Finagle

I was trying to make HTTP calls using Finagle today and all I would get was this traceback from my logs:

1
2
3
4
5
6
7
8
9
10
11
12
13
FAT [20121215-15:49:46.803] HttpServer: A server service client threw an exception
FAT [20121215-15:49:46.803] HttpServer: com.twitter.finagle.WriteException$$anon$1: java.net.ConnectException: connection timed out
FAT [20121215-15:49:46.803] HttpServer:     at org.jboss.netty.channel.socket.nio.NioClientSocketPipelineSink$Boss.processConnectTimeout(NioClientSocketPipelineSink.java:391)
FAT [20121215-15:49:46.803] HttpServer:     at org.jboss.netty.channel.socket.nio.NioClientSocketPipelineSink$Boss.run(NioClientSocketPipelineSink.java:289)
FAT [20121215-15:49:46.803] HttpServer:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
FAT [20121215-15:49:46.803] HttpServer:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
FAT [20121215-15:49:46.803] HttpServer:     at java.lang.Thread.run(Thread.java:722)
FAT [20121215-15:49:46.803] HttpServer: Caused by java.net.ConnectException: connection timed out
FAT [20121215-15:49:46.803] HttpServer:     at org.jboss.netty.channel.socket.nio.NioClientSocketPipelineSink$Boss.processConnectTimeout(NioClientSocketPipelineSink.java:391)
FAT [20121215-15:49:46.803] HttpServer:     at org.jboss.netty.channel.socket.nio.NioClientSocketPipelineSink$Boss.run(NioClientSocketPipelineSink.java:289)
FAT [20121215-15:49:46.803] HttpServer:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
FAT [20121215-15:49:46.803] HttpServer:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
FAT [20121215-15:49:46.803] HttpServer:     at java.lang.Thread.run(Thread.java:722)

It turns out I needed to set up my ClientBuilder a little differently:

1
2
3
4
5
6
7
  val client = ClientBuilder()
    .codec(Http())
    .hosts("localhost:80")
    .tcpConnectTimeout(2.seconds)
    .requestTimeout(55.seconds)
    .hostConnectionLimit(5)
    .build()

The important bits, that don’t appear well documented are tcpConnectTimeout and requestTimeout. The normal ‘timeout’ usually used on the ClientBuilder is not what you want.

This was more a note for me - but figured people Googling might find it useful also.