Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit b7646a3

Browse files
authored
feat: non-retryable RPCs use totalTimeout (#1149)
1 parent 461ff84 commit b7646a3

2 files changed

Lines changed: 12 additions & 32 deletions

File tree

gax-grpc/src/test/java/com/google/api/gax/grpc/TimeoutTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ public void testNonRetryUnarySettings() {
102102
// Verify that the gRPC channel used the CallOptions with our custom timeout of ~2 Days.
103103
assertThat(callOptionsUsed.getDeadline()).isNotNull();
104104
assertThat(callOptionsUsed.getDeadline())
105-
.isGreaterThan(Deadline.after(DEADLINE_IN_SECONDS - 1, TimeUnit.SECONDS));
105+
.isGreaterThan(Deadline.after(DEADLINE_IN_DAYS - 1, TimeUnit.DAYS));
106106
assertThat(callOptionsUsed.getDeadline())
107-
.isLessThan(Deadline.after(DEADLINE_IN_SECONDS, TimeUnit.SECONDS));
107+
.isLessThan(Deadline.after(DEADLINE_IN_DAYS, TimeUnit.DAYS));
108108
assertThat(callOptionsUsed.getAuthority()).isEqualTo(CALL_OPTIONS_AUTHORITY);
109109
}
110110

@@ -126,9 +126,9 @@ public void testNonRetryUnarySettingsWithoutInitialRpcTimeout() {
126126
// Verify that the gRPC channel used the CallOptions with our custom timeout of ~2 Days.
127127
assertThat(callOptionsUsed.getDeadline()).isNotNull();
128128
assertThat(callOptionsUsed.getDeadline())
129-
.isGreaterThan(Deadline.after(DEADLINE_IN_MINUTES - 1, TimeUnit.MINUTES));
129+
.isGreaterThan(Deadline.after(DEADLINE_IN_DAYS - 1, TimeUnit.DAYS));
130130
assertThat(callOptionsUsed.getDeadline())
131-
.isLessThan(Deadline.after(DEADLINE_IN_MINUTES, TimeUnit.MINUTES));
131+
.isLessThan(Deadline.after(DEADLINE_IN_DAYS, TimeUnit.DAYS));
132132
assertThat(callOptionsUsed.getAuthority()).isEqualTo(CALL_OPTIONS_AUTHORITY);
133133
}
134134

@@ -175,9 +175,9 @@ public void testNonRetryServerStreamingSettings() {
175175
// Verify that the gRPC channel used the CallOptions with our custom timeout of ~2 Days.
176176
assertThat(callOptionsUsed.getDeadline()).isNotNull();
177177
assertThat(callOptionsUsed.getDeadline())
178-
.isGreaterThan(Deadline.after(DEADLINE_IN_SECONDS - 1, TimeUnit.SECONDS));
178+
.isGreaterThan(Deadline.after(DEADLINE_IN_DAYS - 1, TimeUnit.DAYS));
179179
assertThat(callOptionsUsed.getDeadline())
180-
.isLessThan(Deadline.after(DEADLINE_IN_SECONDS, TimeUnit.SECONDS));
180+
.isLessThan(Deadline.after(DEADLINE_IN_DAYS, TimeUnit.DAYS));
181181
assertThat(callOptionsUsed.getAuthority()).isEqualTo(CALL_OPTIONS_AUTHORITY);
182182
}
183183

@@ -199,9 +199,9 @@ public void testNonRetryServerStreamingSettingsWithoutInitialRpcTimeout() {
199199
// Verify that the gRPC channel used the CallOptions with our custom timeout of ~2 Days.
200200
assertThat(callOptionsUsed.getDeadline()).isNotNull();
201201
assertThat(callOptionsUsed.getDeadline())
202-
.isGreaterThan(Deadline.after(DEADLINE_IN_MINUTES - 1, TimeUnit.MINUTES));
202+
.isGreaterThan(Deadline.after(DEADLINE_IN_DAYS - 1, TimeUnit.DAYS));
203203
assertThat(callOptionsUsed.getDeadline())
204-
.isLessThan(Deadline.after(DEADLINE_IN_MINUTES, TimeUnit.MINUTES));
204+
.isLessThan(Deadline.after(DEADLINE_IN_DAYS, TimeUnit.DAYS));
205205
assertThat(callOptionsUsed.getAuthority()).isEqualTo(CALL_OPTIONS_AUTHORITY);
206206
}
207207

gax/src/main/java/com/google/api/gax/rpc/Callables.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import com.google.api.gax.retrying.ScheduledRetryingExecutor;
4040
import com.google.api.gax.retrying.StreamingRetryAlgorithm;
4141
import java.util.Collection;
42-
import org.threeten.bp.Duration;
4342

4443
/**
4544
* Class with utility methods to create callable objects using provided settings.
@@ -58,12 +57,11 @@ public static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> retrying(
5857
ClientContext clientContext) {
5958

6059
if (areRetriesDisabled(callSettings.getRetryableCodes(), callSettings.getRetrySettings())) {
61-
// When retries are disabled, the choose a timeout from the retrying settings to use as the
62-
// timeout for the single rpc call.
60+
// When retries are disabled, the total timeout can be treated as the rpc timeout.
6361
return innerCallable.withDefaultCallContext(
6462
clientContext
6563
.getDefaultCallContext()
66-
.withTimeout(singleRpcCallTimeout(callSettings.getRetrySettings())));
64+
.withTimeout(callSettings.getRetrySettings().getTotalTimeout()));
6765
}
6866

6967
RetryAlgorithm<ResponseT> retryAlgorithm =
@@ -84,12 +82,11 @@ public static <RequestT, ResponseT> ServerStreamingCallable<RequestT, ResponseT>
8482
ClientContext clientContext) {
8583

8684
if (areRetriesDisabled(callSettings.getRetryableCodes(), callSettings.getRetrySettings())) {
87-
// When retries are disabled, the choose a timeout from the retrying settings to use as the
88-
// timeout for the single rpc call.
85+
// When retries are disabled, the total timeout can be treated as the rpc timeout.
8986
return innerCallable.withDefaultCallContext(
9087
clientContext
9188
.getDefaultCallContext()
92-
.withTimeout(singleRpcCallTimeout(callSettings.getRetrySettings())));
89+
.withTimeout(callSettings.getRetrySettings().getTotalTimeout()));
9390
}
9491

9592
StreamingRetryAlgorithm<Void> retryAlgorithm =
@@ -105,23 +102,6 @@ public static <RequestT, ResponseT> ServerStreamingCallable<RequestT, ResponseT>
105102
innerCallable, retryingExecutor, callSettings.getResumptionStrategy());
106103
}
107104

108-
/*
109-
* Returns the default Duration for a single RPC call given a Callable's RetrySettings. This
110-
* configuration most likely does not belong in retry settings and may change in the future.
111-
*/
112-
static Duration singleRpcCallTimeout(RetrySettings retrySettings) {
113-
// Prefer initialRpcTimeout, then maxRpcTimeout, then totalTimeout
114-
Duration duration = retrySettings.getInitialRpcTimeout();
115-
if (!duration.isZero()) {
116-
return duration;
117-
}
118-
duration = retrySettings.getMaxRpcTimeout();
119-
if (!duration.isZero()) {
120-
return duration;
121-
}
122-
return retrySettings.getTotalTimeout();
123-
}
124-
125105
@BetaApi("The surface for streaming is not stable yet and may change in the future.")
126106
public static <RequestT, ResponseT> ServerStreamingCallable<RequestT, ResponseT> watched(
127107
ServerStreamingCallable<RequestT, ResponseT> callable,

0 commit comments

Comments
 (0)