2222import static java .nio .charset .StandardCharsets .UTF_8 ;
2323
2424import com .google .api .client .extensions .appengine .http .UrlFetchTransport ;
25+ import com .google .api .client .http .HttpRequest ;
2526import com .google .api .client .http .HttpRequestInitializer ;
2627import com .google .api .client .http .HttpTransport ;
2728import com .google .api .client .http .javanet .NetHttpTransport ;
@@ -60,6 +61,8 @@ public abstract class ServiceOptions<
6061 private final AuthCredentials authCredentials ;
6162 private final RetryParams retryParams ;
6263 private final ServiceRpcFactory <ServiceRpcT , OptionsT > serviceRpcFactory ;
64+ private final int connectTimeout ;
65+ private final int readTimeout ;
6366
6467 public interface HttpTransportFactory extends Serializable {
6568 HttpTransport create ();
@@ -102,6 +105,8 @@ protected abstract static class Builder<
102105 private AuthCredentials authCredentials ;
103106 private RetryParams retryParams ;
104107 private ServiceRpcFactory <ServiceRpcT , OptionsT > serviceRpcFactory ;
108+ private int connectTimeout = -1 ;
109+ private int readTimeout = -1 ;
105110
106111 protected Builder () {}
107112
@@ -150,6 +155,16 @@ public B serviceRpcFactory(ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFa
150155 this .serviceRpcFactory = serviceRpcFactory ;
151156 return self ();
152157 }
158+
159+ public B connectTimeout (int connectTimeout ) {
160+ this .connectTimeout = connectTimeout ;
161+ return self ();
162+ }
163+
164+ public B readTimeout (int readTimeout ) {
165+ this .readTimeout = readTimeout ;
166+ return self ();
167+ }
153168 }
154169
155170 protected ServiceOptions (Builder <ServiceRpcT , OptionsT , ?> builder ) {
@@ -160,6 +175,8 @@ protected ServiceOptions(Builder<ServiceRpcT, OptionsT, ?> builder) {
160175 authCredentials = firstNonNull (builder .authCredentials , defaultAuthCredentials ());
161176 retryParams = builder .retryParams ;
162177 serviceRpcFactory = builder .serviceRpcFactory ;
178+ connectTimeout = builder .connectTimeout ;
179+ readTimeout = builder .readTimeout ;
163180 }
164181
165182 private static AuthCredentials defaultAuthCredentials () {
@@ -303,7 +320,20 @@ public ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFactory() {
303320
304321 public HttpRequestInitializer httpRequestInitializer () {
305322 HttpTransport httpTransport = httpTransportFactory .create ();
306- return authCredentials ().httpRequestInitializer (httpTransport , scopes ());
323+ final HttpRequestInitializer baseRequestInitializer =
324+ authCredentials ().httpRequestInitializer (httpTransport , scopes ());
325+ return new HttpRequestInitializer () {
326+ @ Override
327+ public void initialize (HttpRequest httpRequest ) throws IOException {
328+ baseRequestInitializer .initialize (httpRequest );
329+ if (connectTimeout >= 0 ) {
330+ httpRequest .setConnectTimeout (connectTimeout );
331+ }
332+ if (readTimeout >= 0 ) {
333+ httpRequest .setReadTimeout (readTimeout );
334+ }
335+ }
336+ };
307337 }
308338
309339 protected int baseHashCode () {
0 commit comments