gpt4 book ai didi

java - 为什么使用 OkHttp 时会出现 java.net.SocketTimeoutException?

转载 作者:搜寻专家 更新时间:2023-11-01 02:18:54 33 4
gpt4 key购买 nike

这是我的配置:

httpClient = new OkHttpClient.Builder()
.callTimeout(Duration.ofSeconds(60))
.connectTimeout(Duration.ofSeconds(60))
.readTimeout(Duration.ofSeconds(60))
.writeTimeout(Duration.ofSeconds(60))
.build();

我有一个使用此客户端的多线程进程。运行后几秒钟我得到:

java.net.SocketTimeoutException: timeout
at okio.Okio$4.newTimeoutException(Okio.java:232)
at okio.AsyncTimeout.exit(AsyncTimeout.java:286)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:241)
at okio.RealBufferedSource.indexOf(RealBufferedSource.java:358)

如果我将超时设置为 60 秒怎么可能?

编辑:
即使添加自定义调度程序也无济于事:

Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequests(Integer.MAX_VALUE);
dispatcher.setMaxRequestsPerHost(Integer.MAX_VALUE);

技术细节:
与我所说的相反,我在 Linux 机器上同时运行客户端和服务器:

Client's machine: net.ipv4.tcp_keepalive_time = 7200
Server's machine: net.ipv4.tcp_keepalive_time = 7200

最佳答案

您很可能受到操作系统强加的套接字超时的影响。 Java 无法扩展它们,因为套接字由系统管理。根据 this great answer, "Changing TCP Timeouts" section :

Unfortunately since TCP connections are managed on the OS level, Java does not support configuring timeouts on a per-socket level such as in java.net.Socket. I have found some attempts to use Java Native Interface (JNI) to create Java sockets that call native code to configure these options, but none appear to have widespread community adoption or support.

对于 MacOS,您必须查看 sysctl net.inet.tcp 输出并查看系统限制是什么。

关于java - 为什么使用 OkHttp 时会出现 java.net.SocketTimeoutException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57412716/

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