okhttpclient需要注意

我司使用okhttpclient调用大模型服务,流量发现响应很慢,结合日志分析,发现是请求发生了阻塞,经过排查发现了问题。

okhttpclient使用很简单,一般都是这样使用的

    private final OkHttpClient client = new OkHttpClient.Builder()
        .connectTimeout(30, TimeUnit.SECONDS)
        .readTimeout(30, TimeUnit.SECONDS)
        .writeTimeout(30, TimeUnit.SECONDS)
        .retryOnConnectionFailure(true)
        .build();

但这么简单的使用,是有坑的
okhttpclient有两个默认的参数maxRequests和maxRequestsPerHost,默认值分别是64和5
这两个参数的释义可以参考:

  • maxRequests
The maximum number of requests to execute concurrently. Above this requests queue in memory, waiting for the running calls to complete.
If more than maxRequests requests are in flight when this is invoked, those requests will remain in flight.
  • maxRequestsPerHost
The maximum number of requests for each host to execute concurrently. This limits requests by the URL's host name. Note that concurrent requests to a single IP address may still exceed this limit: multiple hostnames may share an IP address or be routed through the same HTTP proxy.
If more than maxRequestsPerHost requests are in flight when this is invoked, those requests will remain in flight.
WebSocket connections to hosts do not count against this limit.

如果使用默认的配置很容易就会出现请求超过并发阈值,导致排队。
因此需要结合实际情况,注意这两个配置

//	例如
client.dispatcher().setMaxRequestsPerHost(30);
client.dispatcher().setMaxRequests(100);

你可能感兴趣的:(java)