Skip to content

Commit

Permalink
Make read timeout overridable on a per-request basis. (#1318)
Browse files Browse the repository at this point in the history
  • Loading branch information
twz123 authored and slandelle committed Dec 22, 2016
1 parent e21781b commit b5a8541
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
8 changes: 8 additions & 0 deletions client/src/main/java/org/asynchttpclient/DefaultRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class DefaultRequest implements Request {
private final File file;
private final Boolean followRedirect;
private final int requestTimeout;
private final int readTimeout;
private final long rangeOffset;
private final Charset charset;
private final ChannelPoolPartitioning channelPoolPartitioning;
Expand Down Expand Up @@ -83,6 +84,7 @@ public DefaultRequest(String method,//
File file,//
Boolean followRedirect,//
int requestTimeout,//
int readTimeout,//
long rangeOffset,//
Charset charset,//
ChannelPoolPartitioning channelPoolPartitioning,//
Expand All @@ -107,6 +109,7 @@ public DefaultRequest(String method,//
this.file = file;
this.followRedirect = followRedirect;
this.requestTimeout = requestTimeout;
this.readTimeout = readTimeout;
this.rangeOffset = rangeOffset;
this.charset = charset;
this.channelPoolPartitioning = channelPoolPartitioning;
Expand Down Expand Up @@ -218,6 +221,11 @@ public int getRequestTimeout() {
return requestTimeout;
}

@Override
public int getReadTimeout() {
return readTimeout;
}

@Override
public long getRangeOffset() {
return rangeOffset;
Expand Down
5 changes: 5 additions & 0 deletions client/src/main/java/org/asynchttpclient/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ public interface Request {
*/
int getRequestTimeout();

/**
* @return the read timeout. Non zero values means "override config value".
*/
int getReadTimeout();

/**
* @return the range header value, or 0 is not set.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public abstract class RequestBuilderBase<T extends RequestBuilderBase<T>> {
protected File file;
protected Boolean followRedirect;
protected int requestTimeout;
protected int readTimeout;
protected long rangeOffset;
protected Charset charset;
protected ChannelPoolPartitioning channelPoolPartitioning = ChannelPoolPartitioning.PerHostChannelPoolPartitioning.INSTANCE;
Expand Down Expand Up @@ -481,6 +482,11 @@ public T setRequestTimeout(int requestTimeout) {
return asDerivedType();
}

public T setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return asDerivedType();
}

public T setRangeOffset(long rangeOffset) {
this.rangeOffset = rangeOffset;
return asDerivedType();
Expand Down Expand Up @@ -621,6 +627,7 @@ public Request build() {
rb.file,//
rb.followRedirect,//
rb.requestTimeout,//
rb.readTimeout,//
rb.rangeOffset,//
finalCharset,//
rb.channelPoolPartitioning,//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

import org.asynchttpclient.AsyncHttpClientConfig;
import org.asynchttpclient.Request;
import org.asynchttpclient.netty.NettyResponseFuture;
import org.asynchttpclient.netty.request.NettyRequestSender;

Expand All @@ -44,9 +45,13 @@ public TimeoutsHolder(Timer nettyTimer, NettyResponseFuture<?> nettyResponseFutu
this.nettyTimer = nettyTimer;
this.nettyResponseFuture = nettyResponseFuture;
this.requestSender = requestSender;
this.readTimeoutValue = config.getReadTimeout();

int requestTimeoutInMs = nettyResponseFuture.getTargetRequest().getRequestTimeout();
final Request targetRequest = nettyResponseFuture.getTargetRequest();

final int readTimeoutInMs = targetRequest.getReadTimeout();
this.readTimeoutValue = readTimeoutInMs == 0 ? config.getReadTimeout() : readTimeoutInMs;

int requestTimeoutInMs = targetRequest.getRequestTimeout();
if (requestTimeoutInMs == 0) {
requestTimeoutInMs = config.getRequestTimeout();
}
Expand Down

0 comments on commit b5a8541

Please sign in to comment.