Skip to content

Commit 103c37a

Browse files
authored
fix: Only override built-in retry settings when the customer provides them. (#1588)
* Only overrride retry settings when the customer provides them. * Pretty * Add unit test * Fix tests * Revert change to encapsulation * Refactor
1 parent 2672d71 commit 103c37a

File tree

2 files changed

+370
-12
lines changed

2 files changed

+370
-12
lines changed

google-cloud-firestore/src/main/java/com/google/cloud/firestore/spi/v1/GrpcFirestoreRpc.java

+32-12
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@
1616

1717
package com.google.cloud.firestore.spi.v1;
1818

19-
import com.google.api.core.ApiFunction;
2019
import com.google.api.gax.core.BackgroundResource;
2120
import com.google.api.gax.core.GaxProperties;
2221
import com.google.api.gax.grpc.GrpcCallContext;
2322
import com.google.api.gax.grpc.GrpcTransportChannel;
23+
import com.google.api.gax.retrying.RetrySettings;
2424
import com.google.api.gax.rpc.BidiStreamingCallable;
2525
import com.google.api.gax.rpc.ClientContext;
2626
import com.google.api.gax.rpc.HeaderProvider;
2727
import com.google.api.gax.rpc.NoHeaderProvider;
2828
import com.google.api.gax.rpc.ServerStreamingCallable;
2929
import com.google.api.gax.rpc.TransportChannel;
30-
import com.google.api.gax.rpc.UnaryCallSettings;
3130
import com.google.api.gax.rpc.UnaryCallable;
3231
import com.google.cloud.NoCredentials;
3332
import com.google.cloud.ServiceOptions;
@@ -126,17 +125,38 @@ public GrpcFirestoreRpc(final FirestoreOptions options) throws IOException {
126125

127126
clientContext = ClientContext.create(settingsBuilder.build());
128127
}
129-
ApiFunction<UnaryCallSettings.Builder<?, ?>, Void> retrySettingsSetter =
130-
builder -> {
131-
builder.setRetrySettings(options.getRetrySettings());
132-
return null;
133-
};
128+
134129
FirestoreStubSettings.Builder firestoreBuilder =
135-
FirestoreStubSettings.newBuilder(clientContext)
136-
.applyToAllUnaryMethods(retrySettingsSetter);
137-
// Manually apply the retry settings to streaming methods
138-
firestoreBuilder.runQuerySettings().setRetrySettings(options.getRetrySettings());
139-
firestoreBuilder.batchGetDocumentsSettings().setRetrySettings(options.getRetrySettings());
130+
FirestoreStubSettings.newBuilder(clientContext);
131+
RetrySettings retrySettings = options.getRetrySettings();
132+
133+
// Override retry settings only if customer provides settings different from default.
134+
if (retrySettings.equals(ServiceOptions.getDefaultRetrySettings())) {
135+
// We are manually setting `setMaxAttempts(5)` to follow
136+
// the `firestore_grpc_service_config.json` configuration.
137+
// This code should be removed when following issue is fixed:
138+
// https://github.com/googleapis/sdk-platform-java/issues/2306
139+
firestoreBuilder.applyToAllUnaryMethods(
140+
builder -> {
141+
builder.retrySettings().setMaxAttempts(5);
142+
return null;
143+
});
144+
// Manually apply the retry settings to streaming methods
145+
firestoreBuilder.runQuerySettings().retrySettings().setMaxAttempts(5);
146+
firestoreBuilder.runAggregationQuerySettings().retrySettings().setMaxAttempts(5);
147+
firestoreBuilder.batchGetDocumentsSettings().retrySettings().setMaxAttempts(5);
148+
} else {
149+
firestoreBuilder.applyToAllUnaryMethods(
150+
builder -> {
151+
builder.setRetrySettings(retrySettings);
152+
return null;
153+
});
154+
// Manually apply the retry settings to streaming methods
155+
firestoreBuilder.runQuerySettings().setRetrySettings(retrySettings);
156+
firestoreBuilder.runAggregationQuerySettings().setRetrySettings(retrySettings);
157+
firestoreBuilder.batchGetDocumentsSettings().setRetrySettings(retrySettings);
158+
}
159+
140160
firestoreStub = GrpcFirestoreStub.create(firestoreBuilder.build());
141161
} catch (Exception e) {
142162
throw new IOException(e);

0 commit comments

Comments
 (0)