3

How to limit the number of connection/request for java.net.http.HttpClient?

I see I can set an Executor to HttpClient but I don't know: if I limit the number of thread in pool of Executor, is it limit also the number of connection?

Another way to limit the number of connection?

lecogiteur
  • 307
  • 1
  • 7
  • 16
  • 1
    Out of curiosity: why do you need to limit the connections for java.net.http.HttpClient? – Thomas Kläger Feb 15 '22 at 16:11
  • Because the HTTP server authorize only 10 connections max. So I want to be sure to execute only 10 request simultaneous and put next "in a queue" – lecogiteur Feb 15 '22 at 16:25
  • I wonder if I set an executor like new ThreadPoolExecutor(0, 10, 60, TimeUnit.SECONDS, new SynchronousQueue<>()); I have the warranty I don't exceed the connection limit (10). – lecogiteur Feb 15 '22 at 21:50
  • Maybe this SO thread can be of some help to you https://stackoverflow.com/questions/64302936/java-11-httpclient-what-is-optimum-ratio-of-httpclients-to-concurrent-httprequ . – Maurice Feb 16 '22 at 00:14
  • Use a bounded blocking queue when building the Executor, and a fixed size thread pool. – user207421 Feb 16 '22 at 00:48
  • I read the page @Maurice. An I read the code but I 'm not sure. I think if I have a thread pool executor with maximum 10 threads, I can have more than 10 connections because there is some compose on computable future used in sendAsync (but I don't know if exchange is atomic (connection/request/receipt response), I want to say in one thread. – lecogiteur Feb 16 '22 at 05:16

1 Answers1

5

To limit the number of concurrent requests made through the HttpClient you will have to do so at the application level. The HttpClient is purely asynchronous so limiting the number of threads in the HttpClient executor will not work. Similarly, limiting the number of connections in the keep-alive pool will not work, since the pool contains only idle connections.

It should be very easy to limit the number of concurrent requests at the application level by guarding the code that sends requests with a simple semaphore (see java.util.concurrent.Semaphore). To limit the number of concurrent requests to e.g. 10 requests, just create a semaphore with 10 permits, acquire the semaphore before sending a request and release it when the response has arrived.

daniel
  • 2,665
  • 1
  • 8
  • 18