HttpClient timeout error if left idle

826 views
Skip to first unread message

Shanzs

unread,
Feb 10, 2017, 8:19:32 AM2/10/17
to vert.x

We are using vertx 3.3.3 and facing intermittent issues with HttpClient. If left idle for sometime the subsequent call fails with a timeout error.

It works as expected if requests are sent continuously. 

Sharing the code snippets and exception. The httpClient.postAbs is a vertx webserver and is responsive; the exception thrown in HttpClientRequest is a timeout error, that i am finding it hard to understand. Please let us know your suggestions.


We have created an instance of HttpClient in a verticle's start method

public class ConsumerOffers extends BaseVerticle {

HttpClient httpClient;

public void start() {

HttpClientOptions options = new HttpClientOptions().setMaxPoolSize(5).setIdleTimeout(0).setKeepAlive(true).setTcpKeepAlive(true);
httpClient = vertx.createHttpClient(options);

vertx.eventBus().consumer("offer_get", offersHandler);
}


In the offersHandler, we have the below code

HttpClientRequest request = httpClient.postAbs(url, response -> {
System.out.println("In response ");
});

request.exceptionHandler(e -> {
 System.out.println("Received exception: " + e.getMessage());
 e.printStackTrace();
});

request.setTimeout(30000);

request.end(httpClientJsonObject.toString());


Exception - printStackTrace

Received exception: The timeout period of 30000ms has been exceeded
java.util.concurrent.TimeoutException: The timeout period of 30000ms has been exceeded
at io.vertx.core.http.impl.HttpClientRequestBase.timeout(HttpClientRequestBase.java:155)
at io.vertx.core.http.impl.HttpClientRequestBase.handleTimeout(HttpClientRequestBase.java:140)
at io.vertx.core.http.impl.HttpClientRequestBase.lambda$setTimeout$0(HttpClientRequestBase.java:100)
at io.vertx.core.http.impl.HttpClientRequestBase$$Lambda$86/2112965167.handle(Unknown Source)
at io.vertx.core.impl.VertxImpl$InternalTimerHandler.handle(VertxImpl.java:782)
at io.vertx.core.impl.VertxImpl$InternalTimerHandler.handle(VertxImpl.java:753)
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:316)
at io.vertx.core.impl.ContextImpl$$Lambda$26/1763344271.run(Unknown Source)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:418)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:440)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:873)
at java.lang.Thread.run(Thread.java:745)
Feb 10, 2017 5:44:49 PM io.vertx.core.net.impl.ConnectionBase
SEVERE: java.io.IOException: An existing connection was forcibly closed by the remote host
Received exception: An existing connection was forcibly closed by the remote host
java.io.IOException: An existing connection was forcibly closed by the remote host
at sun.nio.ch.SocketDispatcher.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
at sun.nio.ch.IOUtil.read(IOUtil.java:192)
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:379)
at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288)
at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1100)
at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:367)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:118)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:610)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:551)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:465)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:437)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:873)
at java.lang.Thread.run(Thread.java:745)

javadevmtl

unread,
Feb 10, 2017, 3:00:40 PM2/10/17
to vert.x
It's keep-alive, the server eventually closes the connection. If I'm not mistaken HttpClient uses it by default?

Alexander Lehmann

unread,
Feb 10, 2017, 4:53:58 PM2/10/17
to vert.x
The server has its own timeout value, I don't think this would happen after 30s that the client has set. Might still be an issue with the keepalive,I am not sure.

It would be good if you could put up a reproducer project for the issue (including a mock http server if possible) so we can check out the issue

Shanzs

unread,
Feb 13, 2017, 12:07:23 AM2/13/17
to vert.x
Many thanks for your response. I did try removing .setKeepAlive(true).setTcpKeepAlive(true) from options ... it did not resolve the issue

I will share a reproducer shortly

javadevmtl

unread,
Feb 15, 2017, 5:09:23 PM2/15/17
to vert.x
Btw what server are you connecting too?

Shanzs

unread,
Feb 20, 2017, 1:47:08 AM2/20/17
to vert.x
Hi,

I removed Maxpool and keepalive from HttpClientOptions as i observed (using netstat -antp TCP) the server ports are kept live . For the requirement I am working on, the HttpClient connects to different servers for each call and KeepAlive will not be required 

HttpClientOptions options = new HttpClientOptions().setIdleTimeout(0).setKeepAlive(false);

I did not observe the timeout issue after an idle time of 8 hrs

Many thanks to Alexander and javadevmtl 

SHIVAM KATARIA

unread,
Sep 25, 2017, 6:17:31 AM9/25/17
to vert.x
Hi @Shanzs-

Thanks for providing the solution.

Did you still keep request.setTimeout(30000); in your code?

Regards,
Shivam

SHIVAM KATARIA

unread,
Sep 25, 2017, 6:21:53 AM9/25/17
to vert.x
Hi Shanzs,

Did you also remove 
request.setTimeout(30000);

from your code?

shanzs

unread,
Sep 26, 2017, 2:26:21 PM9/26/17
to ve...@googlegroups.com
Hi Shivam,

I retained request.setTimeout .. this is required to handle timeout exceptions on httpClient request

Regards,
Shanzs



Sent with Mailtrack

--
You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/CQ-6f7oOYuo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vertx+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/c519bd91-bdb7-439b-bcf8-15b75a5479f1%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages