gpt4 book ai didi

java - 休息模板 - 没有可用的缓冲区空间(已达到最大连接数?)

转载 作者:行者123 更新时间:2023-11-30 07:09:05 24 4
gpt4 key购买 nike

我正在使用 RestTemplate 执行并发 HTTP 请求。一段时间后,我开始得到

java.net.SocketException: No buffer space available (maximum connections reached?)

  1. 我知道这与处于 TIME_WAIT 状态的套接字有关。
  2. 我已尝试安装 Windows 7 修复程序,大多数消息来源都鼓励这样做。
  3. 我将 RestTemplate 配置为使用 HttpClient,如下所示:

    val httpClient = HttpClientBuilder.create()
    .setMaxConnPerRoute(properties.concurrencyLimit)
    .setMaxConnTotal(properties.concurrencyLimit)
    .build()
    return RestTemplate(HttpComponentsClientHttpRequestFactory(httpClient))
  4. 我尝试过使用十亿个其他 HttpClient 配置

  5. 我尝试过 20-100 之间不同数量的并发请求

就在我要按发布您的问题按钮之前,我的同事找到了一个解决方案,但对我来说没有任何意义:

 val httpClient = HttpClientBuilder.create()
.setMaxConnPerRoute(properties.concurrencyLimit * 2)
.setMaxConnTotal(properties.concurrencyLimit * 2)
.build()
return RestTemplate(HttpComponentsClientHttpRequestFactory(httpClient))

基本上,当我将连接池设置为线程数的两倍时,整个事情就像一个魅力。

为什么?为什么第一个配置不起作用而第二个配置起作用?

所有依赖项均由 Spring Boot 1.4.0.RELEASE 父 pom 管理。

最佳答案

连接根据路由进行池化。由于您至少有 2 条路由,因此连接总数应至少为每条路由最大连接数的 2 倍。另请参阅其他相关post是的,如果您一个接一个地处理批处理或者存在某种异步性质,我建议让每个死记硬背的 conenctionPool 多于线程数。(不太确定如何使用 concurrencyLimit)

参见 here 除外:

PoolingHttpClientConnectionManager is a more complex implementation that manages a pool of client connections and is able to service connection requests from multiple execution threads. Connections are pooled on a per route basis. A request for a route for which the manager already has a persistent connection available in the pool will be serviced by leasing a connection from the pool rather than creating a brand new connection. PoolingHttpClientConnectionManager maintains a maximum limit of connections on a per route basis and in total. Per default this implementation will create no more than 2 concurrent connections per given route and no more 20 connections in total. For many real-world applications these limits may prove too constraining, especially if they use HTTP as a transport protocol for their services.

关于java - 休息模板 - 没有可用的缓冲区空间(已达到最大连接数?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39508090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com