From 77bfdd7867f2d89e38e7aea208238f0b5daef23e Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 9 Oct 2015 16:26:18 +0200 Subject: [PATCH 1/3] Add connectTimeout and readTimeout to ServiceOptions --- .../com/google/gcloud/ServiceOptions.java | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java index 206e1fecaa58..af98116830b5 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java @@ -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; @@ -60,6 +61,8 @@ public abstract class ServiceOptions< private final AuthCredentials authCredentials; private final RetryParams retryParams; private final ServiceRpcFactory serviceRpcFactory; + private final int connectTimeout; + private final int readTimeout; public interface HttpTransportFactory extends Serializable { HttpTransport create(); @@ -102,6 +105,8 @@ protected abstract static class Builder< private AuthCredentials authCredentials; private RetryParams retryParams; private ServiceRpcFactory serviceRpcFactory; + private int connectTimeout = -1; + private int readTimeout = -1; protected Builder() {} @@ -150,6 +155,16 @@ public B serviceRpcFactory(ServiceRpcFactory serviceRpcFa this.serviceRpcFactory = serviceRpcFactory; return self(); } + + public B connectTimeout(int connectTimeout) { + this.connectTimeout = connectTimeout; + return self(); + } + + public B readTimeout(int readTimeout) { + this.readTimeout = readTimeout; + return self(); + } } protected ServiceOptions(Builder builder) { @@ -160,6 +175,8 @@ protected ServiceOptions(Builder builder) { authCredentials = firstNonNull(builder.authCredentials, defaultAuthCredentials()); retryParams = builder.retryParams; serviceRpcFactory = builder.serviceRpcFactory; + connectTimeout = builder.connectTimeout; + readTimeout = builder.readTimeout; } private static AuthCredentials defaultAuthCredentials() { @@ -303,7 +320,20 @@ public ServiceRpcFactory 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() { From 51bfef6d69e64dc4608d729c6121e926eae088d3 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 9 Oct 2015 20:27:07 +0200 Subject: [PATCH 2/3] Add javadoc to ServiceOptions.Builder methods --- .../com/google/gcloud/ServiceOptions.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java index af98116830b5..b19ef19b4e05 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java @@ -126,41 +126,85 @@ 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 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(); From 6423879951de89d3deade718275d9a6545360947 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 9 Oct 2015 20:39:09 +0200 Subject: [PATCH 3/3] Add timeout getters and javadocs to StorageOptions --- .../com/google/gcloud/ServiceOptions.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java index b19ef19b4e05..c70975502a06 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java @@ -338,30 +338,52 @@ protected static String getAppEngineProjectId() { protected abstract Set 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 serviceRpcFactory() { return serviceRpcFactory; } + /** + * Returns a request initializer responsible for initializing requests according to service + * options. + */ public HttpRequestInitializer httpRequestInitializer() { HttpTransport httpTransport = httpTransportFactory.create(); final HttpRequestInitializer baseRequestInitializer = @@ -380,6 +402,22 @@ public void initialize(HttpRequest httpRequest) throws IOException { }; } + /** + * 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() { return Objects.hash(projectId, host, httpTransportFactory, authCredentials, retryParams, serviceRpcFactory);