This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 119
/
StubSettings.java
464 lines (414 loc) · 18.9 KB
/
StubSettings.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
* Copyright 2018 Google LLC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.api.gax.rpc;
import com.google.api.core.ApiClock;
import com.google.api.core.ApiFunction;
import com.google.api.core.BetaApi;
import com.google.api.core.NanoClock;
import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.core.ExecutorProvider;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.api.gax.core.FixedExecutorProvider;
import com.google.api.gax.core.InstantiatingExecutorProvider;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.tracing.ApiTracerFactory;
import com.google.api.gax.tracing.NoopApiTracerFactory;
import com.google.auth.Credentials;
import com.google.auth.oauth2.QuotaProjectIdProvider;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import java.io.IOException;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.threeten.bp.Duration;
/**
* A base settings class to configure a client stub class.
*
* <p>This base class includes settings that are applicable to all services, which includes things
* like settings for creating an executor, credentials, transport-specific settings, and identifiers
* for http headers.
*
* <p>If no ExecutorProvider is set, then InstantiatingExecutorProvider will be used, which creates
* a default executor.
*/
public abstract class StubSettings<SettingsT extends StubSettings<SettingsT>> {
static final String QUOTA_PROJECT_ID_HEADER_KEY = "x-google-user-project";
private final ExecutorProvider executorProvider;
private final CredentialsProvider credentialsProvider;
private final HeaderProvider headerProvider;
private final HeaderProvider internalHeaderProvider;
private final TransportChannelProvider transportChannelProvider;
private final ApiClock clock;
private final String endpoint;
private final String quotaProjectID;
@Nullable private final WatchdogProvider streamWatchdogProvider;
@Nonnull private final Duration streamWatchdogCheckInterval;
@Nonnull private final ApiTracerFactory tracerFactory;
/** Constructs an instance of StubSettings. */
protected StubSettings(Builder builder) {
this.executorProvider = builder.executorProvider;
this.transportChannelProvider = builder.transportChannelProvider;
this.credentialsProvider = builder.credentialsProvider;
this.headerProvider = builder.headerProvider;
this.internalHeaderProvider = builder.internalHeaderProvider;
this.clock = builder.clock;
this.endpoint = builder.endpoint;
this.quotaProjectID = builder.quotaProjectID;
this.streamWatchdogProvider = builder.streamWatchdogProvider;
this.streamWatchdogCheckInterval = builder.streamWatchdogCheckInterval;
this.tracerFactory = builder.tracerFactory;
}
public final ExecutorProvider getExecutorProvider() {
return executorProvider;
}
public final TransportChannelProvider getTransportChannelProvider() {
return transportChannelProvider;
}
public final CredentialsProvider getCredentialsProvider() {
return credentialsProvider;
}
@BetaApi("The surface for customizing headers is not stable yet and may change in the future.")
public final HeaderProvider getHeaderProvider() {
return headerProvider;
}
@BetaApi("The surface for customizing headers is not stable yet and may change in the future.")
protected final HeaderProvider getInternalHeaderProvider() {
return internalHeaderProvider;
}
public final ApiClock getClock() {
return clock;
}
public final String getEndpoint() {
return endpoint;
}
public final String getQuotaProjectID() {
return quotaProjectID;
}
@BetaApi("The surface for streaming is not stable yet and may change in the future.")
@Nullable
public final WatchdogProvider getStreamWatchdogProvider() {
return streamWatchdogProvider;
}
@BetaApi("The surface for streaming is not stable yet and may change in the future.")
@Nonnull
public final Duration getStreamWatchdogCheckInterval() {
return streamWatchdogCheckInterval;
}
/**
* Gets the configured {@link ApiTracerFactory} that will be used to generate traces for
* operations.
*/
@BetaApi("The surface for tracing is not stable yet and may change in the future.")
@Nonnull
public ApiTracerFactory getTracerFactory() {
return tracerFactory;
}
public String toString() {
return MoreObjects.toStringHelper(this)
.add("executorProvider", executorProvider)
.add("transportChannelProvider", transportChannelProvider)
.add("credentialsProvider", credentialsProvider)
.add("headerProvider", headerProvider)
.add("internalHeaderProvider", internalHeaderProvider)
.add("clock", clock)
.add("endpoint", endpoint)
.add("quotaProjectID", quotaProjectID)
.add("streamWatchdogProvider", streamWatchdogProvider)
.add("streamWatchdogCheckInterval", streamWatchdogCheckInterval)
.add("tracerFactory", tracerFactory)
.toString();
}
public abstract StubSettings.Builder toBuilder();
public abstract static class Builder<
SettingsT extends StubSettings<SettingsT>, B extends Builder<SettingsT, B>> {
private ExecutorProvider executorProvider;
private CredentialsProvider credentialsProvider;
private HeaderProvider headerProvider;
private HeaderProvider internalHeaderProvider;
private TransportChannelProvider transportChannelProvider;
private ApiClock clock;
private String endpoint;
private String quotaProjectID;
@Nullable private WatchdogProvider streamWatchdogProvider;
@Nonnull private Duration streamWatchdogCheckInterval;
@Nonnull private ApiTracerFactory tracerFactory;
/** Create a builder from a StubSettings object. */
protected Builder(StubSettings settings) {
this.executorProvider = settings.executorProvider;
this.transportChannelProvider = settings.transportChannelProvider;
this.credentialsProvider = settings.credentialsProvider;
this.headerProvider = settings.headerProvider;
this.internalHeaderProvider = settings.internalHeaderProvider;
this.clock = settings.clock;
this.endpoint = settings.endpoint;
this.quotaProjectID = settings.quotaProjectID;
this.streamWatchdogProvider = settings.streamWatchdogProvider;
this.streamWatchdogCheckInterval = settings.streamWatchdogCheckInterval;
this.tracerFactory = settings.tracerFactory;
}
/** Get Quota Project ID from Client Context * */
private static String getQuotaProjectIDFromClientContext(ClientContext clientContext) {
if (clientContext.getQuotaProjectID() != null) {
return clientContext.getQuotaProjectID();
}
if (clientContext.getCredentials() instanceof QuotaProjectIdProvider) {
return ((QuotaProjectIdProvider) clientContext.getCredentials()).getQuotaProjectId();
}
if (clientContext.getHeaders().containsKey(QUOTA_PROJECT_ID_HEADER_KEY)) {
return clientContext.getHeaders().get(QUOTA_PROJECT_ID_HEADER_KEY);
}
if (clientContext.getInternalHeaders().containsKey(QUOTA_PROJECT_ID_HEADER_KEY)) {
return clientContext.getInternalHeaders().get(QUOTA_PROJECT_ID_HEADER_KEY);
}
return null;
}
protected Builder(ClientContext clientContext) {
if (clientContext == null) {
this.executorProvider = InstantiatingExecutorProvider.newBuilder().build();
this.transportChannelProvider = null;
this.credentialsProvider = NoCredentialsProvider.create();
this.headerProvider = new NoHeaderProvider();
this.internalHeaderProvider = new NoHeaderProvider();
this.clock = NanoClock.getDefaultClock();
this.endpoint = null;
this.quotaProjectID = null;
this.streamWatchdogProvider = InstantiatingWatchdogProvider.create();
this.streamWatchdogCheckInterval = Duration.ofSeconds(10);
this.tracerFactory = NoopApiTracerFactory.getInstance();
} else {
this.executorProvider = FixedExecutorProvider.create(clientContext.getExecutor());
this.transportChannelProvider =
FixedTransportChannelProvider.create(clientContext.getTransportChannel());
this.credentialsProvider = FixedCredentialsProvider.create(clientContext.getCredentials());
this.headerProvider = FixedHeaderProvider.create(clientContext.getHeaders());
this.internalHeaderProvider =
FixedHeaderProvider.create(clientContext.getInternalHeaders());
this.clock = clientContext.getClock();
this.endpoint = clientContext.getEndpoint();
this.streamWatchdogProvider =
FixedWatchdogProvider.create(clientContext.getStreamWatchdog());
this.streamWatchdogCheckInterval = clientContext.getStreamWatchdogCheckInterval();
this.tracerFactory = clientContext.getTracerFactory();
this.quotaProjectID = getQuotaProjectIDFromClientContext(clientContext);
}
}
protected Builder() {
this((ClientContext) null);
}
@SuppressWarnings("unchecked")
protected B self() {
return (B) this;
}
/**
* Sets the ExecutorProvider to use for getting the executor to use for running asynchronous API
* call logic (such as retries and long-running operations), and also to pass to the transport
* settings if an executor is needed for the transport and it doesn't have its own executor
* provider.
*/
public B setExecutorProvider(ExecutorProvider executorProvider) {
this.executorProvider = executorProvider;
return self();
}
/** Sets the CredentialsProvider to use for getting the credentials to make calls with. */
public B setCredentialsProvider(CredentialsProvider credentialsProvider) {
this.credentialsProvider = Preconditions.checkNotNull(credentialsProvider);
try {
Credentials credentials = credentialsProvider.getCredentials();
if (this.quotaProjectID == null && credentials instanceof QuotaProjectIdProvider) {
this.quotaProjectID = ((QuotaProjectIdProvider) credentials).getQuotaProjectId();
}
} catch (IOException e) {
System.out.println("fail to fetch credentials");
}
return self();
}
/**
* Sets the HeaderProvider for getting custom static headers for http requests. The header
* provider will be called during client construction only once. The headers returned by the
* provider will be cached and supplied as is for each request issued by the constructed client.
* Some reserved headers can be overridden (e.g. Content-Type) or merged with the default value
* (e.g. User-Agent) by the underlying transport layer.
*/
@BetaApi("The surface for customizing headers is not stable yet and may change in the future.")
public B setHeaderProvider(HeaderProvider headerProvider) {
this.headerProvider = headerProvider;
if (this.quotaProjectID == null
&& headerProvider.getHeaders().containsKey(QUOTA_PROJECT_ID_HEADER_KEY)) {
this.quotaProjectID = headerProvider.getHeaders().get(QUOTA_PROJECT_ID_HEADER_KEY);
}
return self();
}
/**
* Sets the HeaderProvider for getting internal (library-defined) static headers for http
* requests. The header provider will be called during client construction only once. The
* headers returned by the provider will be cached and supplied as is for each request issued by
* the constructed client. Some reserved headers can be overridden (e.g. Content-Type) or merged
* with the default value (e.g. User-Agent) by the underlying transport layer.
*/
@BetaApi("The surface for customizing headers is not stable yet and may change in the future.")
protected B setInternalHeaderProvider(HeaderProvider internalHeaderProvider) {
this.internalHeaderProvider = internalHeaderProvider;
if (this.quotaProjectID == null
&& internalHeaderProvider.getHeaders().containsKey(QUOTA_PROJECT_ID_HEADER_KEY)) {
this.quotaProjectID = internalHeaderProvider.getHeaders().get(QUOTA_PROJECT_ID_HEADER_KEY);
}
return self();
}
/**
* Sets the TransportProvider to use for getting the transport-specific context to make calls
* with.
*/
public B setTransportChannelProvider(TransportChannelProvider transportChannelProvider) {
this.transportChannelProvider = transportChannelProvider;
return self();
}
/**
* Sets the {@link WatchdogProvider} to use for streaming RPC.
*
* <p>This will default to a {@link InstantiatingWatchdogProvider} if it is not set.
*/
@BetaApi("The surface for streaming is not stable yet and may change in the future.")
public B setStreamWatchdogProvider(@Nullable WatchdogProvider streamWatchdogProvider) {
this.streamWatchdogProvider = streamWatchdogProvider;
return self();
}
/**
* Sets the clock to use for retry logic.
*
* <p>This will default to a system clock if it is not set.
*/
public B setClock(ApiClock clock) {
this.clock = clock;
return self();
}
public B setEndpoint(String endpoint) {
this.endpoint = endpoint;
return self();
}
public B setQuotaProjectID(String quotaProjectID) {
this.quotaProjectID = quotaProjectID;
return self();
}
/**
* Sets how often the {@link Watchdog} will check ongoing streaming RPCs. Defaults to 10 secs.
* Use {@link Duration#ZERO} to disable.
*/
@BetaApi("The surface for streaming is not stable yet and may change in the future.")
public B setStreamWatchdogCheckInterval(@Nonnull Duration checkInterval) {
Preconditions.checkNotNull(checkInterval);
this.streamWatchdogCheckInterval = checkInterval;
return self();
}
/**
* Configures the {@link ApiTracerFactory} that will be used to generate traces.
*
* @param tracerFactory an instance of {@link ApiTracerFactory} to set.
*/
@BetaApi("The surface for tracing is not stable yet and may change in the future.")
public B setTracerFactory(@Nonnull ApiTracerFactory tracerFactory) {
Preconditions.checkNotNull(tracerFactory);
this.tracerFactory = tracerFactory;
return self();
}
/** Gets the ExecutorProvider that was previously set on this Builder. */
public ExecutorProvider getExecutorProvider() {
return executorProvider;
}
/** Gets the TransportProvider that was previously set on this Builder. */
public TransportChannelProvider getTransportChannelProvider() {
return transportChannelProvider;
}
/** Gets the CredentialsProvider that was previously set on this Builder. */
public CredentialsProvider getCredentialsProvider() {
return credentialsProvider;
}
/** Gets the custom HeaderProvider that was previously set on this Builder. */
@BetaApi("The surface for customizing headers is not stable yet and may change in the future.")
public HeaderProvider getHeaderProvider() {
return headerProvider;
}
/** Gets the internal HeaderProvider that was previously set on this Builder. */
@BetaApi("The surface for customizing headers is not stable yet and may change in the future.")
protected HeaderProvider getInternalHeaderProvider() {
return internalHeaderProvider;
}
/** Gets the {@link WatchdogProvider }that was previously set on this Builder. */
@BetaApi("The surface for streaming is not stable yet and may change in the future.")
@Nullable
public WatchdogProvider getStreamWatchdogProvider() {
return streamWatchdogProvider;
}
/** Gets the ApiClock that was previously set on this Builder. */
public ApiClock getClock() {
return clock;
}
public String getEndpoint() {
return endpoint;
}
/** Gets the QuotaProjectID that was previously set on this Builder. */
public String getQuotaProjectID() {
return quotaProjectID;
}
@BetaApi("The surface for streaming is not stable yet and may change in the future.")
@Nonnull
public Duration getStreamWatchdogCheckInterval() {
return streamWatchdogCheckInterval;
}
@BetaApi("The surface for tracing is not stable yet and may change in the future.")
@Nonnull
public ApiTracerFactory getTracerFactory() {
return tracerFactory;
}
/** Applies the given settings updater function to the given method settings builders. */
protected static void applyToAllUnaryMethods(
Iterable<UnaryCallSettings.Builder<?, ?>> methodSettingsBuilders,
ApiFunction<UnaryCallSettings.Builder<?, ?>, Void> settingsUpdater) {
for (UnaryCallSettings.Builder<?, ?> settingsBuilder : methodSettingsBuilders) {
settingsUpdater.apply(settingsBuilder);
}
}
public abstract <B extends StubSettings<B>> StubSettings<B> build() throws IOException;
public String toString() {
return MoreObjects.toStringHelper(this)
.add("executorProvider", executorProvider)
.add("transportChannelProvider", transportChannelProvider)
.add("credentialsProvider", credentialsProvider)
.add("headerProvider", headerProvider)
.add("internalHeaderProvider", internalHeaderProvider)
.add("clock", clock)
.add("endpoint", endpoint)
.add("quotaProjectID", quotaProjectID)
.add("streamWatchdogProvider", streamWatchdogProvider)
.add("streamWatchdogCheckInterval", streamWatchdogCheckInterval)
.add("tracerFactory", tracerFactory)
.toString();
}
}
}