简体   繁体   English

okhttp 客户端超时和 apache 超时之间的区别

[英]Difference between okhttp client timeouts and apache timeouts

In the past I used the http client of apache.过去我用的是apache的http客户端。 I had settings for:我有以下设置:

  • Connection Request timeout连接请求超时
  • Connect timeout连接超时
  • Read/socket timeout读取/套接字超时
  • ConnectionPool size连接池大小

I am migrating to the OkHttp client and it has different timeouts:我正在迁移到 OkHttp 客户端,它有不同的超时:

  • connect-timeout连接超时
  • call-timeout通话超时
  • read-timeout读超时
  • write-timeout写超时
  • max-idle-connections (connection pool setting) max-idle-connections(连接池设置)
  • keep-alive-duration-minutes (connection pool setting) keep-alive-duration-minutes(连接池设置)

How do they map to each other?他们如何互相 map?

Regards,问候,

Rick瑞克

Extracting from the excellent article written on Baeldung , here are a few details:摘自 Baeldung上写的优秀文章,这里有一些细节:

  1. Connect Timeout:连接超时:

    A connect timeout defines a time period in which our client should establish a connection with a target host.连接超时定义了我们的客户端应该与目标主机建立连接的时间段。 By default, for the OkHttpClient , this timeout is set to 10 seconds.默认情况下,对于OkHttpClient ,此超时设置为 10 秒。

    However, we can easily change its value using the OkHttpClient.Builder#connectTimeout method.但是,我们可以使用OkHttpClient.Builder#connectTimeout方法轻松更改其值。 A value of zero means no timeout at all.零值意味着根本没有超时。

    Let's now see how to build and use an OkHttpClient with a custom connection timeout:现在让我们看看如何使用自定义连接超时构建和使用OkHttpClient

     @Test public void whenConnectTimeoutExceeded_thenSocketTimeoutException() { OkHttpClient client = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.MILLISECONDS).build(); Request request = new Request.Builder().url("http://203.0.113.1") // non routable address.build(); Throwable thrown = catchThrowable(() -> client.newCall(request).execute()); assertThat(thrown).isInstanceOf(SocketTimeoutException.class); }

    The above example shows that the client throws a SocketTimeoutException when the connection attempt exceeds the configured timeout.上面的示例显示当连接尝试超过配置的超时时间时,客户端会抛出SocketTimeoutException

  2. Call Timeout:通话超时:

    A call timeout is a bit different than the connect, read and write timeouts we already discussed.调用超时与我们已经讨论过的连接、读取和写入超时有点不同。 It defines a time limit for a complete HTTP call .它定义了一个完整的 HTTP 调用的时间限制 This includes resolving DNS, connecting, writing the request body, server processing, as well as reading the response body.这包括解析 DNS、连接、写入请求正文、服务器处理以及读取响应正文。 Unlike other timeouts, it's default value is set to zero which implies no timeout .与其他超时不同,它的默认值设置为零,这意味着没有超时 But of course, we can configure a custom value using OkHttpClient.Builder#callTimeout method.当然,我们可以使用OkHttpClient.Builder#callTimeout方法配置自定义值。

    Let's see a practical usage example:让我们看一个实际的使用示例:

     @Test public void whenCallTimeoutExceeded_thenInterruptedIOException() { OkHttpClient client = new OkHttpClient.Builder().callTimeout(1, TimeUnit.SECONDS).build(); Request request = new Request.Builder().url("https://httpbin.org/delay/2").build(); Throwable thrown = catchThrowable(() -> client.newCall(request).execute()); assertThat(thrown).isInstanceOf(InterruptedIOException.class); }
  3. Read Timeout:读取超时:

    A read timeout is applied from the moment the connection between a client and a target host has been successfully established.从客户端和目标主机之间的连接成功建立的那一刻起,就会应用读取超时。

    It defines a maximum time of inactivity between two data packets when waiting for the server's response .它定义了等待服务器响应时两个数据包之间不活动的最长时间

    The default timeout of 10 seconds can be changed using OkHttpClient.Builder#readTimeout .可以使用OkHttpClient.Builder#readTimeout更改 10 秒的默认超时。 Analogously as for the connect timeout, a zero value indicates no timeout.与连接超时类似,零值表示没有超时。

    Let's now see how to configure a custom read timeout in practice:现在让我们看看如何在实践中配置自定义读取超时:

     @Test public void whenReadTimeoutExceeded_thenSocketTimeoutException() { OkHttpClient client = new OkHttpClient.Builder().readTimeout(10, TimeUnit.MILLISECONDS).build(); Request request = new Request.Builder().url("https://httpbin.org/delay/2") // 2-second response time.build(); Throwable thrown = catchThrowable(() -> client.newCall(request).execute()); assertThat(thrown).isInstanceOf(SocketTimeoutException.class); }

    As we can see, the server doesn't return the response within the defined timeout of 500 ms.正如我们所看到的,服务器在定义的 500 毫秒超时内没有返回响应。 As a result, the OkHttpClient throws a SocketTimeoutException .结果, OkHttpClient抛出SocketTimeoutException

  4. Write Timeout:写超时:

    A write timeout defines a maximum time of inactivity between two data packets when sending the request to the server .写超时定义了向服务器发送请求时两个数据包之间不活动的最长时间

    Similarly, as for the connect and read timeouts, we can override the default value of 10 seconds using OkHttpClient.Builder#writeTimeout .同样,对于连接和读取超时,我们可以使用OkHttpClient.Builder#writeTimeout覆盖默认值 10 秒 As a convention, a zero value means no timeout at all.按照惯例,零值意味着根本没有超时。

    In the following example, we set a very short write timeout of 10 ms and post a 1 MB content to the server:在以下示例中,我们将写入超时设置为 10 毫秒,并将 1 MB 的内容发布到服务器:

     @Test public void whenWriteTimeoutExceeded_thenSocketTimeoutException() { OkHttpClient client = new OkHttpClient.Builder().writeTimeout(10, TimeUnit.MILLISECONDS).build(); Request request = new Request.Builder().url("https://httpbin.org/delay/2").post(RequestBody.create(MediaType.parse("text/plain"), create1MBString())).build(); Throwable thrown = catchThrowable(() -> client.newCall(request).execute()); assertThat(thrown).isInstanceOf(SocketTimeoutException.class); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM