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 connectTimeout and readTimeout to ServiceOptions #234

Merged
merged 3 commits into from
Oct 9, 2015
Merged
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
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 comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

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;

This comment was marked as spam.

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