Connection and connection request timeout

后端 未结 3 1148
萌比男神i
萌比男神i 2020-12-15 17:43

I am using Http Apache Components to perform the http interactions. I need to adjust my http client. For this purpose I have two parameters: connection timeout and connectio

相关标签:
3条回答
  • 2020-12-15 18:01

    So, my question is what do [connection-timeout and connection-request-timeout] mean and how they distinct from each other.

    Connection-timeout is the timeout in milliseconds until the server accepts the request. If you specify 3000, the http-client will wait 3 seconds for the server to accept the TCP connection before timing out. This typically is used to make sure you don't have a networking issue or that you are contacting the right hostname or address. This is equivalent to curl's --connect-timeout seconds option.

    Connection-request-timeout is the input/output timeout after the connection has been established. If you specify this value as 10000 then after the http-client has connected to the server and sends a request, it will wait 10 seconds for the server to return a result. This typically is used to ensure that your job doesn't wait for a slow server forever. This is equivalent to curl's --max-time seconds option.

    In HttpClient 4.X.X , the following is how you build a client that uses a particular connectTimeoutMillis and requestTimeoutMillis.

    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    RequestConfig.Builder requestBuilder = RequestConfig.custom();
    requestBuilder = requestBuilder.setConnectTimeout(connectTimeoutMillis);
    requestBuilder = requestBuilder.setConnectionRequestTimeout(requestTimeoutMillis);
    clientBuilder.setDefaultRequestConfig(requestBuilder.build());
    CloseableHttpClient httpClient = clientBuilder.build();
    ...
    

    Btw, the javadocs for this code sucks. Try to figure out by hand how to use the config builder. Holy crap.

    0 讨论(0)
  • 2020-12-15 18:07

    From the documentation:

    /**
     * Returns the timeout in milliseconds used when requesting a connection
     * from the connection manager. A timeout value of zero is interpreted
     * as an infinite timeout.
     * <p>
     * A timeout value of zero is interpreted as an infinite timeout.
     * A negative value is interpreted as undefined (system default).
     * </p>
     * <p>
     * Default: {@code -1}
     * </p>
     */
    public int getConnectionRequestTimeout() {
        return connectionRequestTimeout;
    }
    
    /**
     * Determines the timeout in milliseconds until a connection is established.
     * A timeout value of zero is interpreted as an infinite timeout.
     * <p>
     * A timeout value of zero is interpreted as an infinite timeout.
     * A negative value is interpreted as undefined (system default).
     * </p>
     * <p>
     * Default: {@code -1}
     * </p>
     */
    public int getConnectTimeout() {
        return connectTimeout;
    }
    

    This is how the code should look:

    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    RequestConfig.Builder requestBuilder = RequestConfig.custom();
    // Connection Timeout to establish a connection
    requestBuilder = requestBuilder.setConnectTimeout(connectTimeoutMillis);
    // Timeout to get a connection from the connection manager for the Http Client
    requestBuilder = requestBuilder.setConnectionRequestTimeout(requestTimeoutMillis);
    // Timeout between two data packets from the server
    requestBuilder = requestBuilder.setSocketTimeout(requestTimeoutMillis);
    clientBuilder.setDefaultRequestConfig(requestBuilder.build());
    CloseableHttpClient httpClient = clientBuilder.build();
    
    0 讨论(0)
  • 2020-12-15 18:16

    HttpClient has a way to set connection and socket timeout (setConnectionTimeout() and setTimeout()) according to the HttpClient javadocs.

    Connection timeout is the timeout until a connection with the server is established.

    Socket timeout is the timeout to receive data (socket timeout).

    Example:

    Let's say you point your browser to access a web page. If the server does not anwser in X seconds, a connection timeout will occur. But if it establishes the connection, then the server will start to process the result for the browser. If it does not ends this processing in Y seconds, a socket timeout will occur.

    0 讨论(0)
提交回复
热议问题