15

Can someone please explain to me what setMaxPerRoute(max) and setMaxTotal(max) do in reference to HttpComponents PoolingHttpClientConnectionManager?

james
  • 1,035
  • 2
  • 14
  • 33

1 Answers1

40

These settings control connection pool size.

  • setMaxTotal(max) defines the overal connection limit for a conneciton pool.
  • setMaxPerRoute(max) defines a connection limit per one HTTP route. In simple cases you can understand this as a per target host limit. Under the hood things are a bit more interesting: HttpClient maintains a couple of HttpRoute objects, which represent a chain of hosts each, like proxy1 -> proxy2 -> targetHost. Connections are pooled on per-route basis. In simple cases, when you're using default route-building mechanism and provide no proxy suport, your routes are likely to include target host only, so per-route connection pool limit effectively becomes per-host limit.

Example:

Suppose you have setMaxPerRoute(5) and setMaxTotal(20). That means you can simultameously use up to 5 connections for every target host: 5 connections with google.com, another 5 connections with oracle.com and so on. The total amount of open connections can't however exceed 20 regardless of the number of hosts you're communicating with.

Jk1
  • 11,233
  • 9
  • 54
  • 64
  • Thank you, that was very helpful. Is there a limit to the maxPerRoute? As I will be trying to request a lot of URL's from the same website (tens of thousands) using roughly 150 threads. – james Oct 13 '13 at 16:28
  • 1
    Not in the HttpClient itself, it's perfectly ok for the client to have 150 connections to a single host. But keep in mind, that server may not allow so many connections from a single client, that depends on the server configuration. – Jk1 Oct 13 '13 at 16:37
  • Sorry to bug you, but do you use HttpComponents yourself? – james Oct 13 '13 at 17:10
  • I do, my current project utilizes it heavily. – Jk1 Oct 13 '13 at 17:11
  • Do you mind me asking you some questions regard it and best practices? – james Oct 13 '13 at 17:18
  • @James, feel free to ask – Jk1 Oct 14 '13 at 05:35
  • @Jk1 Thanks for the explanation. Obviously the official document is a little bit vague. If the HttpClient only connect to one internal host all the way, is it ok to set the value of `setMaxPerRoute` and `setMaxTotal` to be equal, let say they are all 200? – macemers Jun 30 '15 at 03:54
  • I suppose it is, as long as your internal host is able to handle up to 200 connections. – Jk1 Jun 30 '15 at 08:49
  • when the app is in this state [total available: 5; route allocated: 50 of 50; total allocated: 18 of 100] starts getting timeout, it has CloseableHttpClient, PoolingHttpClientConnectionManager with MaxTotal 100, DefaultMaxPerRoute 50 configured, also a ConnectionKeepAliveStrategy with 20 secs, what can be the error ? – Tiago Medici Jan 27 '23 at 11:09