From 86c68b3ffd241f6932516d0e7b5d9ae5714b89e0 Mon Sep 17 00:00:00 2001 From: Vadym Matsishevskyi <25311427+vam-google@users.noreply.github.com> Date: Wed, 8 Sep 2021 10:36:00 -0700 Subject: [PATCH] fix: REGAPIC fix socket timeout for wait calls (#1476) wait() LRO calls may take 2+ minutes, while default read timeout was 20sec. --- .../google/api/gax/httpjson/HttpRequestRunnable.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpRequestRunnable.java b/gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpRequestRunnable.java index 5f61588ef..d0afc08e8 100644 --- a/gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpRequestRunnable.java +++ b/gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpRequestRunnable.java @@ -54,6 +54,8 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import org.threeten.bp.Duration; +import org.threeten.bp.Instant; /** A runnable object that creates and executes an HTTP request. */ @AutoValue @@ -115,6 +117,16 @@ HttpRequest createHttpRequest() throws IOException { HttpRequest httpRequest = buildRequest(requestFactory, url, jsonHttpContent); + Instant deadline = getHttpJsonCallOptions().getDeadline(); + if (deadline != null) { + long readTimeout = Duration.between(Instant.now(), deadline).toMillis(); + if (httpRequest.getReadTimeout() > 0 + && httpRequest.getReadTimeout() < readTimeout + && readTimeout < Integer.MAX_VALUE) { + httpRequest.setReadTimeout((int) readTimeout); + } + } + for (HttpJsonHeaderEnhancer enhancer : getHeaderEnhancers()) { enhancer.enhance(httpRequest.getHeaders()); }