Skip to content

Commit

Permalink
Merge pull request #234 from mziccard/add-timeout-service-options
Browse files Browse the repository at this point in the history
Add connectTimeout and readTimeout to ServiceOptions
  • Loading branch information
aozarov committed Oct 9, 2015
2 parents 9f95061 + 6423879 commit 5515d52
Showing 1 changed file with 113 additions and 1 deletion.
114 changes: 113 additions & 1 deletion gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java
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 All @@ -121,35 +126,89 @@ protected B self() {
return (B) this;
}

/**
* Sets project id.
*
* @return the builder.
*/
public B projectId(String projectId) {
this.projectId = projectId;
return self();
}

/**
* Sets service host.
*
* @return the builder.
*/
public B host(String host) {
this.host = host;
return self();
}

/**
* Sets the transport factory.
*
* @return the builder.
*/
public B httpTransportFactory(HttpTransportFactory httpTransportFactory) {
this.httpTransportFactory = httpTransportFactory;
return self();
}

/**
* Sets the service authentication credentials.
*
* @return the builder.
*/
public B authCredentials(AuthCredentials authCredentials) {
this.authCredentials = authCredentials;
return self();
}

/**
* Sets configuration parameters for request retries.
*
* @return the builder.
*/
public B retryParams(RetryParams retryParams) {
this.retryParams = retryParams;
return self();
}

/**
* Sets the factory for rpc services.
*
* @return the builder
*/
public B serviceRpcFactory(ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFactory) {
this.serviceRpcFactory = serviceRpcFactory;
return self();
}

/**
* Sets the timeout in milliseconds to establish a connection.
*
* @param connectTimeout connection timeout in milliseconds. 0 for an infinite timeout, a
* negative number for the default value (20000).
* @return the builder.
*/
public B connectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return self();
}

/**
* Sets the timeout in milliseconds to read data from an established connection.
*
* @param readTimeout read timeout in milliseconds. 0 for an infinite timeout, a
* negative number for the default value (20000).
* @return the builder.
*/
public B readTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return self();
}
}

protected ServiceOptions(Builder<ServiceRpcT, OptionsT, ?> builder) {
Expand All @@ -160,6 +219,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 @@ -277,33 +338,84 @@ protected static String getAppEngineProjectId() {

protected abstract Set<String> scopes();

/**
* Returns the project id.
*/
public String projectId() {
return projectId;
}

/**
* Returns the service host.
*/
public String host() {
return host;
}

/**
* Returns the transport factory.
*/
public HttpTransportFactory httpTransportFactory() {
return httpTransportFactory;
}

/**
* Returns the authentication credentials.
*/
public AuthCredentials authCredentials() {
return authCredentials;
}

/**
* Returns configuration parameters for request retries.
*/
public RetryParams retryParams() {
return retryParams != null ? retryParams : RetryParams.noRetries();
}

/**
* Returns the factory for rpc services.
*/
public ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFactory() {
return serviceRpcFactory;
}

/**
* Returns a request initializer responsible for initializing requests according to service
* options.
*/
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);
}
}
};
}

/**
* Returns the timeout in milliseconds to establish a connection. 0 is an infinite timeout, a
* negative number is the default value (20000).
*/
public int connectTimeout() {
return connectTimeout;
}

/**
* Returns the timeout in milliseconds to read from an established connection. 0 is an infinite
* timeout, a negative number is the default value (20000).
*/
public int readTimeout() {
return readTimeout;
}

protected int baseHashCode() {
Expand Down

0 comments on commit 5515d52

Please sign in to comment.