Skip to content

Commit

Permalink
Add connectTimeout and readTimeout to ServiceOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Oct 9, 2015
1 parent 92b6fce commit c810895
Showing 1 changed file with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.api.client.extensions.appengine.http.UrlFetchTransport;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
Expand Down Expand Up @@ -60,6 +61,8 @@ public abstract class ServiceOptions<
private final AuthCredentials authCredentials;
private final RetryParams retryParams;
private final ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFactory;
private final int connectTimeout;
private final int readTimeout;

public interface HttpTransportFactory extends Serializable {
HttpTransport create();
Expand Down Expand Up @@ -102,6 +105,8 @@ protected abstract static class Builder<
private AuthCredentials authCredentials;
private RetryParams retryParams;
private ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFactory;
private int connectTimeout = -1;
private int readTimeout = -1;

protected Builder() {}

Expand Down Expand Up @@ -146,8 +151,13 @@ public B retryParams(RetryParams retryParams) {
return self();
}

public B serviceRpcFactory(ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFactory) {
this.serviceRpcFactory = serviceRpcFactory;
public B connectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return self();
}

public B readTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return self();
}
}
Expand All @@ -160,6 +170,8 @@ protected ServiceOptions(Builder<ServiceRpcT, OptionsT, ?> builder) {
authCredentials = firstNonNull(builder.authCredentials, defaultAuthCredentials());
retryParams = builder.retryParams;
serviceRpcFactory = builder.serviceRpcFactory;
connectTimeout = builder.connectTimeout;
readTimeout = builder.readTimeout;
}

private static AuthCredentials defaultAuthCredentials() {
Expand Down Expand Up @@ -303,7 +315,20 @@ public ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFactory() {

public HttpRequestInitializer httpRequestInitializer() {
HttpTransport httpTransport = httpTransportFactory.create();
return authCredentials().httpRequestInitializer(httpTransport, scopes());
final HttpRequestInitializer baseRequestInitializer =
authCredentials().httpRequestInitializer(httpTransport, scopes());
return new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
baseRequestInitializer.initialize(httpRequest);
if (connectTimeout >= 0) {
httpRequest.setConnectTimeout(connectTimeout);
}
if (readTimeout >= 0) {
httpRequest.setReadTimeout(readTimeout);
}
}
};
}

protected int baseHashCode() {
Expand Down

0 comments on commit c810895

Please sign in to comment.