Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add read timeout setter on HttpComponentsClientHttpRequestFactory #33556

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest

private long connectionRequestTimeout = -1;


private long readTimeout = -1;

/**
* Create a new instance of the {@code HttpComponentsClientHttpRequestFactory}
Expand Down Expand Up @@ -136,7 +138,7 @@ public void setConnectTimeout(int connectTimeout) {
* handshakes or CONNECT requests; for that, it is required to
* use the {@link SocketConfig} on the
* {@link HttpClient} itself.
* @param connectTimeout the timeout value in milliseconds
* @param connectTimeout the timeout as {@code Duration}.
* @since 6.1
* @see RequestConfig#getConnectTimeout()
* @see SocketConfig#getSoTimeout
Expand Down Expand Up @@ -167,7 +169,7 @@ public void setConnectionRequestTimeout(int connectionRequestTimeout) {
* A timeout value of 0 specifies an infinite timeout.
* <p>Additional properties can be configured by specifying a
* {@link RequestConfig} instance on a custom {@link HttpClient}.
* @param connectionRequestTimeout the timeout value to request a connection in milliseconds
* @param connectionRequestTimeout the timeout value to request a connection as {@code Duration}.
* @since 6.1
* @see RequestConfig#getConnectionRequestTimeout()
*/
Expand All @@ -177,6 +179,43 @@ public void setConnectionRequestTimeout(Duration connectionRequestTimeout) {
this.connectionRequestTimeout = connectionRequestTimeout.toMillis();
}

/**
* Set the response timeout for the underlying {@link RequestConfig}.
* A timeout value of 0 specifies an infinite timeout.
* <p>Additional properties can be configured by specifying a
* {@link RequestConfig} instance on a custom {@link HttpClient}.
* <p>This options does not affect connection timeouts for SSL
* handshakes or CONNECT requests; for that, it is required to
* use the {@link SocketConfig} on the
* {@link HttpClient} itself.
* @param readTimeout the timeout value in milliseconds
* @since 6.2
* @see RequestConfig#getResponseTimeout()
*/
public void setReadTimeout(int readTimeout) {
Assert.isTrue(readTimeout >= 0, "Timeout must be a non-negative value");
this.readTimeout = readTimeout;
}

/**
* Set the response timeout for the underlying {@link RequestConfig}.
* A timeout value of 0 specifies an infinite timeout.
* <p>Additional properties can be configured by specifying a
* {@link RequestConfig} instance on a custom {@link HttpClient}.
* <p>This options does not affect connection timeouts for SSL
* handshakes or CONNECT requests; for that, it is required to
* use the {@link SocketConfig} on the
* {@link HttpClient} itself.
* @param readTimeout the timeout as {@code Duration}.
* @since 6.2
* @see RequestConfig#getResponseTimeout()
*/
public void setReadTimeout(Duration readTimeout) {
Assert.notNull(readTimeout, "ReadTimeout must not be null");
Assert.isTrue(!readTimeout.isNegative(), "Timeout must be a non-negative value");
this.readTimeout = readTimeout.toMillis();
}

/**
* Indicates whether this request factory should buffer the request body internally.
* <p>Default is {@code true}. When sending large amounts of data via POST or PUT, it is
Expand Down Expand Up @@ -262,7 +301,7 @@ protected RequestConfig createRequestConfig(Object client) {
*/
@SuppressWarnings("deprecation") // setConnectTimeout
protected RequestConfig mergeRequestConfig(RequestConfig clientConfig) {
if (this.connectTimeout == -1 && this.connectionRequestTimeout == -1) { // nothing to merge
if (this.connectTimeout == -1 && this.connectionRequestTimeout == -1 && this.readTimeout == -1) { // nothing to merge
return clientConfig;
}

Expand All @@ -273,6 +312,9 @@ protected RequestConfig mergeRequestConfig(RequestConfig clientConfig) {
if (this.connectionRequestTimeout >= 0) {
builder.setConnectionRequestTimeout(this.connectionRequestTimeout, TimeUnit.MILLISECONDS);
}
if (this.readTimeout >= 0) {
builder.setResponseTimeout(this.readTimeout, TimeUnit.MILLISECONDS);
}
return builder.build();
}

Expand Down
Loading