I'm using RestTemplate
to execute concurrent HTTP requests. After some time, I start getting
java.net.SocketException: No buffer space available (maximum connections reached?)
- I know that this is related to sockets in TIME_WAIT status.
- I have tried installing Windows 7 fix, that most sources encourage.
I configured
RestTemplate
to useHttpClient
as follows:val httpClient = HttpClientBuilder.create() .setMaxConnPerRoute(properties.concurrencyLimit) .setMaxConnTotal(properties.concurrencyLimit) .build() return RestTemplate(HttpComponentsClientHttpRequestFactory(httpClient))
I have tried to use billion other
HttpClient
configurations- I have tried different amounts of concurrent requests between 20-100
Just before I was going to press Post your question button, my colleague found a solution, that doesn't make any sense to me:
val httpClient = HttpClientBuilder.create()
.setMaxConnPerRoute(properties.concurrencyLimit * 2)
.setMaxConnTotal(properties.concurrencyLimit * 2)
.build()
return RestTemplate(HttpComponentsClientHttpRequestFactory(httpClient))
Basically, when I set connection pool twice as big as threads number, the whole thing works like a charm.
WHY? WHY DOESN'T FIRST CONFIGURATION WORK AND SECOND DOES?
All dependencies are managed by Spring Boot 1.4.0.RELEASE parent pom.