okhttp3 中的 connectTimeout 会减慢响应时间 [英] connectTimeout in okhttp3 slows down responnse time

查看:253
本文介绍了okhttp3 中的 connectTimeout 会减慢响应时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我对 okhttp 版本 4.9.0 有一个奇怪的问题.它似乎增加了响应时间( client.newCall(request).execute() ).如果我将 connectTimeout 增加到 5 秒,响应时间增加到 6 如果我增加到 15 秒响应时间增加到 16 秒.以下是我如何使用构建器.

 client = new OkHttpClient.Builder().connectionPool(new ConnectionPool(2, 2, TimeUnit.SECONDS)).缓存(空).connectTimeout(2, TimeUnit.SECONDS).readTimeout(25, TimeUnit.SECONDS).build();

感谢您的任何提示!

解决方案

很可能有两条或更多条路由,并且在第二条工作之前无法连接到第一个.您很可能遇到网络问题或类似问题,应该解决该问题.

确认添加调试EventListener

https://square.github.io/okhttp/events/

由于您没有提供有用的复制品,我们可以使用额外的路线进行模拟以进行测试.

 val client = OkHttpClient.Builder().dns(对象:DNS {覆盖乐趣查找(主机名:字符串):列表{return listOf(InetAddress.getByName(198.51.100.0")) + Dns.SYSTEM.lookup(httpbin.org")}}).eventListenerFactory(LoggingEventListener.Factory()).建造()val response = client.newCall(Request.Builder().url("https://httpbin.org/get").build()).execute()println(response.body!!.string())

输出将如下所示,不是第一次连接需要 10 秒(连接超时),因为它击中了一个虚拟 IP,然后后续连接将成功尝试总共 5 个可用 IP 地址中的第二个,并且请求将继续.

2020 年 11 月 23 日上午 7:44:01 okhttp3.internal.platform.Platform 日志信息:[0 毫秒] callStart:请求{method=GET, url=https://httpbin.org/get}2020 年 11 月 23 日上午 7:44:01 okhttp3.internal.platform.Platform 日志信息:[34 毫秒] proxySelectStart:https://httpbin.org/2020 年 11 月 23 日上午 7:44:01 okhttp3.internal.platform.Platform 日志信息:[35 毫秒] 代理选择结束:[直接]2020 年 11 月 23 日上午 7:44:01 okhttp3.internal.platform.Platform 日志信息:[36 毫秒] dnsStart:httpbin.org2020 年 11 月 23 日上午 7:44:01 okhttp3.internal.platform.Platform 日志信息:[150 毫秒] dnsEnd:[/198.51.100.0,httpbin.org/3.211.1.78,httpbin.org/35.170.225.136,httpbin.org/34.198.212.59,httpbin.org/52.6.6]342020 年 11 月 23 日上午 7:44:01 okhttp3.internal.platform.Platform 日志信息:[153 毫秒] 连接开始:/198.51.100.0:443 直接2020 年 11 月 23 日上午 7:44:11 okhttp3.internal.platform.Platform 日志信息:[10164 毫秒] 连接失败:空 java.net.SocketTimeoutException:连接超时2020 年 11 月 23 日上午 7:44:11 okhttp3.internal.platform.Platform 日志信息:[10165 毫秒] 连接开始:httpbin.org/3.211.1.78:443 直接2020 年 11 月 23 日上午 7:44:11 okhttp3.internal.platform.Platform 日志信息:[10263 毫秒] 安全连接开始2020 年 11 月 23 日上午 7:44:12 okhttp3.internal.platform.Platform 日志信息:[10663 ms] secureConnectEnd:握手{tlsVersion=TLS_1_2 cipherSuite=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 peerCertificates=[CN=httpbin.org, CN=Amazon, OU=Server CA 1B, O=Amazon, C=Root CA, CN=AmazonO=亚马逊,C=美国] localCertificates=[]}2020 年 11 月 23 日上午 7:44:12 okhttp3.internal.platform.Platform 日志信息:[10676 毫秒] 连接结束:h22020 年 11 月 23 日上午 7:44:12 okhttp3.internal.platform.Platform 日志信息:[10678 ms] connectionAcquired:连接{httpbin.org:443,proxy=DIRECT hostAddress=httpbin.org/3.211.1.78:443 cipherSuite=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 protocol=h2}2020 年 11 月 23 日上午 7:44:12 okhttp3.internal.platform.Platform 日志信息:[10679 毫秒] requestHeadersStart2020 年 11 月 23 日上午 7:44:12 okhttp3.internal.platform.Platform 日志信息:[10683 毫秒] requestHeadersEnd2020 年 11 月 23 日上午 7:44:12 okhttp3.internal.platform.Platform 日志信息:[10903 毫秒] responseHeadersStart2020 年 11 月 23 日上午 7:44:12 okhttp3.internal.platform.Platform 日志信息:[10904 ms] responseHeadersEnd:响应{protocol=h2,code=200,message=,url=https://httpbin.org/get}2020 年 11 月 23 日上午 7:44:12 okhttp3.internal.platform.Platform 日志信息:[10907 毫秒] responseBodyStart2020 年 11 月 23 日上午 7:44:12 okhttp3.internal.platform.Platform 日志信息:[10908 毫秒] responseBodyEnd:byteCount=2722020 年 11 月 23 日上午 7:44:12 okhttp3.internal.platform.Platform 日志信息:[10908 ms] connectionReleased2020 年 11 月 23 日上午 7:44:12 okhttp3.internal.platform.Platform 日志信息:[10908 毫秒] callEnd{参数":{},标题":{接受编码":gzip",主机":httpbin.org",用户代理":okhttp/4.10.0-RC1",X-Amzn-Trace-Id":Root=1-5fbb684d-0b7fa68f2cce945f25b5f1bf"},起源":81.100.210.134",网址":https://httpbin.org/get"}

Ho All, I have a odd issue with okhttp version 4.9.0. It seems to increase the response time ( client.newCall(request).execute() ). If I increase the connectTimeout to 5 seconds the response time increases to 6 if I increase it to 15 seconds the response time increases to 16 seconds. Below is how I am using the builder.

 client = new OkHttpClient.Builder()
                .connectionPool(new ConnectionPool(2, 2, TimeUnit.SECONDS))
                .cache(null)
                .connectTimeout(2, TimeUnit.SECONDS)
                .readTimeout(25, TimeUnit.SECONDS).build();

Thanks for any hints!

解决方案

It is likely getting two or more routes and failing to connect to the first before the second one works. Most likely you have anetwork problem or similar and should fix that.

To confirm add a debugging EventListener

https://square.github.io/okhttp/events/

Since you haven't provided a useful reproduction, we can simulate using an additional route to test against.

  val client = OkHttpClient.Builder()
    .dns(object: Dns {
      override fun lookup(hostname: String): List<InetAddress> {
        return listOf<InetAddress>(InetAddress.getByName("198.51.100.0")) + Dns.SYSTEM.lookup("httpbin.org")
      }
    })
    .eventListenerFactory(LoggingEventListener.Factory())
    .build()

  val response = client.newCall(Request.Builder().url("https://httpbin.org/get").build()).execute()
  println(response.body!!.string())

Output will be like the following, not that the first connect takes 10 seconds (the connect timeouts) as it's hitting a dummy IP and then subsequent connections will try the second of the total 5 available IP addresses successfully and the request will continue.

Nov 23, 2020 7:44:01 AM okhttp3.internal.platform.Platform log
INFO: [0 ms] callStart: Request{method=GET, url=https://httpbin.org/get}
Nov 23, 2020 7:44:01 AM okhttp3.internal.platform.Platform log
INFO: [34 ms] proxySelectStart: https://httpbin.org/
Nov 23, 2020 7:44:01 AM okhttp3.internal.platform.Platform log
INFO: [35 ms] proxySelectEnd: [DIRECT]
Nov 23, 2020 7:44:01 AM okhttp3.internal.platform.Platform log
INFO: [36 ms] dnsStart: httpbin.org
Nov 23, 2020 7:44:01 AM okhttp3.internal.platform.Platform log
INFO: [150 ms] dnsEnd: [/198.51.100.0, httpbin.org/3.211.1.78, httpbin.org/35.170.225.136, httpbin.org/34.198.212.59, httpbin.org/52.6.34.179]
Nov 23, 2020 7:44:01 AM okhttp3.internal.platform.Platform log
INFO: [153 ms] connectStart: /198.51.100.0:443 DIRECT
Nov 23, 2020 7:44:11 AM okhttp3.internal.platform.Platform log
INFO: [10164 ms] connectFailed: null java.net.SocketTimeoutException: Connect timed out
Nov 23, 2020 7:44:11 AM okhttp3.internal.platform.Platform log
INFO: [10165 ms] connectStart: httpbin.org/3.211.1.78:443 DIRECT
Nov 23, 2020 7:44:11 AM okhttp3.internal.platform.Platform log
INFO: [10263 ms] secureConnectStart
Nov 23, 2020 7:44:12 AM okhttp3.internal.platform.Platform log
INFO: [10663 ms] secureConnectEnd: Handshake{tlsVersion=TLS_1_2 cipherSuite=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 peerCertificates=[CN=httpbin.org, CN=Amazon, OU=Server CA 1B, O=Amazon, C=US, CN=Amazon Root CA 1, O=Amazon, C=US] localCertificates=[]}
Nov 23, 2020 7:44:12 AM okhttp3.internal.platform.Platform log
INFO: [10676 ms] connectEnd: h2
Nov 23, 2020 7:44:12 AM okhttp3.internal.platform.Platform log
INFO: [10678 ms] connectionAcquired: Connection{httpbin.org:443, proxy=DIRECT hostAddress=httpbin.org/3.211.1.78:443 cipherSuite=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 protocol=h2}
Nov 23, 2020 7:44:12 AM okhttp3.internal.platform.Platform log
INFO: [10679 ms] requestHeadersStart
Nov 23, 2020 7:44:12 AM okhttp3.internal.platform.Platform log
INFO: [10683 ms] requestHeadersEnd
Nov 23, 2020 7:44:12 AM okhttp3.internal.platform.Platform log
INFO: [10903 ms] responseHeadersStart
Nov 23, 2020 7:44:12 AM okhttp3.internal.platform.Platform log
INFO: [10904 ms] responseHeadersEnd: Response{protocol=h2, code=200, message=, url=https://httpbin.org/get}
Nov 23, 2020 7:44:12 AM okhttp3.internal.platform.Platform log
INFO: [10907 ms] responseBodyStart
Nov 23, 2020 7:44:12 AM okhttp3.internal.platform.Platform log
INFO: [10908 ms] responseBodyEnd: byteCount=272
Nov 23, 2020 7:44:12 AM okhttp3.internal.platform.Platform log
INFO: [10908 ms] connectionReleased
Nov 23, 2020 7:44:12 AM okhttp3.internal.platform.Platform log
INFO: [10908 ms] callEnd
{
  "args": {}, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Host": "httpbin.org", 
    "User-Agent": "okhttp/4.10.0-RC1", 
    "X-Amzn-Trace-Id": "Root=1-5fbb684d-0b7fa68f2cce945f25b5f1bf"
  }, 
  "origin": "81.100.210.134", 
  "url": "https://httpbin.org/get"
}

这篇关于okhttp3 中的 connectTimeout 会减慢响应时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆