javamultithreadingnettyasynchttpclient

AsyncHttpClient creates too many AsyncHttpClient-timer threads


I am using AsyncHttpClient 2.3.0 and Default configuration.

I've noticed that AHC created two types of threads (from the thread dump):

1)

AsyncHttpClient-timer-478-1" - Thread t@30390    java.lang.Thread.State: TIMED_WAITING
        at java.lang.Thread.$$YJP$$sleep(Native Method)
        at java.lang.Thread.sleep(Thread.java)
        at io.netty.util.HashedWheelTimer$Worker.waitForNextTick(HashedWheelTimer.java:560)
        at io.netty.util.HashedWheelTimer$Worker.run(HashedWheelTimer.java:459)
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
        at java.lang.Thread.run(Thread.java:748)

2)

AsyncHttpClient-3-4" - Thread t@20320    java.lang.Thread.State: RUNNABLE
        at sun.nio.ch.EPollArrayWrapper.$$YJP$$epollWait(Native Method)
        at sun.nio.ch.EPollArrayWrapper.epollWait(EPollArrayWrapper.java)
        at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
        at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93)
        at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
        - locked <16163575> (a io.netty.channel.nio.SelectedSelectionKeySet)
        - locked <49280039> (a java.util.Collections$UnmodifiableSet)
        - locked <2decd496> (a sun.nio.ch.EPollSelectorImpl)
        at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
        at io.netty.channel.nio.SelectedSelectionKeySetSelector.select(SelectedSelectionKeySetSelector.java:62)
        at io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:753)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:409)
        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:886)
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
        at java.lang.Thread.run(Thread.java:748)

I've expected that AsyncHttpClient uses a few threads under the hood. But after a few days of running, AsyncHttpClient creates ~500 of AsyncHttpClient-timer-xxx-x threads and a few AsyncHttpClient-x-x. It's called not very intensively, probably also ~500 times per this period. Only executeRequest is used (execute request and get on returned future) https://static.javadoc.io/org.asynchttpclient/async-http-client/2.3.0/org/asynchttpclient/AsyncHttpClient.html#executeRequest-org.asynchttpclient.Request-org.asynchttpclient.AsyncHandler-:

<T> ListenableFuture<T> executeRequest(Request request, AsyncHandler<T> handler);

I've seen a page about connection pool configuration (https://github.com/AsyncHttpClient/async-http-client/wiki/Connection-pooling) but nothing about thread pool configuration.

What is the difference between both types of thread and what can cause a large number of threads created? Is there any configuration I should apply?


Solution

  • AHC has two types of threads:

    1. For I/O operation. On your screen, it's AsyncHttpClient-x-x threads. AHC creates 2*core_number of those.
    2. For timeouts. On your screen, it's AsyncHttpClient-timer-1-1 thread. Should be only one.

    Any different number means you’re creating multiple clients.

    Source: issue on GitHub https://github.com/AsyncHttpClient/async-http-client/issues/1658