30
30
package com .google .api .gax .grpc ;
31
31
32
32
import com .google .api .core .BetaApi ;
33
+ import com .google .api .gax .retrying .RetrySettings ;
33
34
import com .google .api .gax .rpc .ApiCallContext ;
35
+ import com .google .api .gax .rpc .StatusCode ;
34
36
import com .google .api .gax .rpc .TransportChannel ;
35
37
import com .google .api .gax .rpc .internal .Headers ;
36
38
import com .google .api .gax .tracing .ApiTracer ;
37
39
import com .google .api .gax .tracing .NoopApiTracer ;
38
40
import com .google .auth .Credentials ;
39
41
import com .google .common .base .Preconditions ;
40
42
import com .google .common .collect .ImmutableMap ;
43
+ import com .google .common .collect .ImmutableSet ;
41
44
import io .grpc .CallCredentials ;
42
45
import io .grpc .CallOptions ;
43
46
import io .grpc .CallOptions .Key ;
48
51
import java .util .List ;
49
52
import java .util .Map ;
50
53
import java .util .Objects ;
54
+ import java .util .Set ;
51
55
import javax .annotation .Nonnull ;
52
56
import javax .annotation .Nullable ;
53
57
import org .threeten .bp .Duration ;
@@ -70,18 +74,36 @@ public final class GrpcCallContext implements ApiCallContext {
70
74
@ Nullable private final Duration streamWaitTimeout ;
71
75
@ Nullable private final Duration streamIdleTimeout ;
72
76
@ Nullable private final Integer channelAffinity ;
77
+ @ Nullable private final RetrySettings retrySettings ;
78
+ @ Nullable private final ImmutableSet <StatusCode .Code > retryableCodes ;
73
79
private final ImmutableMap <String , List <String >> extraHeaders ;
74
80
75
81
/** Returns an empty instance with a null channel and default {@link CallOptions}. */
76
82
public static GrpcCallContext createDefault () {
77
83
return new GrpcCallContext (
78
- null , CallOptions .DEFAULT , null , null , null , null , ImmutableMap .<String , List <String >>of ());
84
+ null ,
85
+ CallOptions .DEFAULT ,
86
+ null ,
87
+ null ,
88
+ null ,
89
+ null ,
90
+ ImmutableMap .<String , List <String >>of (),
91
+ null ,
92
+ null );
79
93
}
80
94
81
95
/** Returns an instance with the given channel and {@link CallOptions}. */
82
96
public static GrpcCallContext of (Channel channel , CallOptions callOptions ) {
83
97
return new GrpcCallContext (
84
- channel , callOptions , null , null , null , null , ImmutableMap .<String , List <String >>of ());
98
+ channel ,
99
+ callOptions ,
100
+ null ,
101
+ null ,
102
+ null ,
103
+ null ,
104
+ ImmutableMap .<String , List <String >>of (),
105
+ null ,
106
+ null );
85
107
}
86
108
87
109
private GrpcCallContext (
@@ -91,14 +113,18 @@ private GrpcCallContext(
91
113
@ Nullable Duration streamWaitTimeout ,
92
114
@ Nullable Duration streamIdleTimeout ,
93
115
@ Nullable Integer channelAffinity ,
94
- ImmutableMap <String , List <String >> extraHeaders ) {
116
+ ImmutableMap <String , List <String >> extraHeaders ,
117
+ @ Nullable RetrySettings retrySettings ,
118
+ @ Nullable Set <StatusCode .Code > retryableCodes ) {
95
119
this .channel = channel ;
96
120
this .callOptions = Preconditions .checkNotNull (callOptions );
97
121
this .timeout = timeout ;
98
122
this .streamWaitTimeout = streamWaitTimeout ;
99
123
this .streamIdleTimeout = streamIdleTimeout ;
100
124
this .channelAffinity = channelAffinity ;
101
125
this .extraHeaders = Preconditions .checkNotNull (extraHeaders );
126
+ this .retrySettings = retrySettings ;
127
+ this .retryableCodes = retryableCodes == null ? null : ImmutableSet .copyOf (retryableCodes );
102
128
}
103
129
104
130
/**
@@ -160,7 +186,9 @@ public GrpcCallContext withTimeout(@Nullable Duration timeout) {
160
186
this .streamWaitTimeout ,
161
187
this .streamIdleTimeout ,
162
188
this .channelAffinity ,
163
- this .extraHeaders );
189
+ this .extraHeaders ,
190
+ this .retrySettings ,
191
+ this .retryableCodes );
164
192
}
165
193
166
194
@ Nullable
@@ -177,13 +205,15 @@ public GrpcCallContext withStreamWaitTimeout(@Nullable Duration streamWaitTimeou
177
205
}
178
206
179
207
return new GrpcCallContext (
180
- channel ,
181
- callOptions ,
182
- timeout ,
208
+ this . channel ,
209
+ this . callOptions ,
210
+ this . timeout ,
183
211
streamWaitTimeout ,
184
- streamIdleTimeout ,
185
- channelAffinity ,
186
- extraHeaders );
212
+ this .streamIdleTimeout ,
213
+ this .channelAffinity ,
214
+ this .extraHeaders ,
215
+ this .retrySettings ,
216
+ this .retryableCodes );
187
217
}
188
218
189
219
@ Override
@@ -194,25 +224,29 @@ public GrpcCallContext withStreamIdleTimeout(@Nullable Duration streamIdleTimeou
194
224
}
195
225
196
226
return new GrpcCallContext (
197
- channel ,
198
- callOptions ,
199
- timeout ,
200
- streamWaitTimeout ,
227
+ this . channel ,
228
+ this . callOptions ,
229
+ this . timeout ,
230
+ this . streamWaitTimeout ,
201
231
streamIdleTimeout ,
202
- channelAffinity ,
203
- extraHeaders );
232
+ this .channelAffinity ,
233
+ this .extraHeaders ,
234
+ this .retrySettings ,
235
+ this .retryableCodes );
204
236
}
205
237
206
238
@ BetaApi ("The surface for channel affinity is not stable yet and may change in the future." )
207
239
public GrpcCallContext withChannelAffinity (@ Nullable Integer affinity ) {
208
240
return new GrpcCallContext (
209
- channel ,
210
- callOptions ,
211
- timeout ,
212
- streamWaitTimeout ,
213
- streamIdleTimeout ,
241
+ this . channel ,
242
+ this . callOptions ,
243
+ this . timeout ,
244
+ this . streamWaitTimeout ,
245
+ this . streamIdleTimeout ,
214
246
affinity ,
215
- extraHeaders );
247
+ this .extraHeaders ,
248
+ this .retrySettings ,
249
+ this .retryableCodes );
216
250
}
217
251
218
252
@ BetaApi ("The surface for extra headers is not stable yet and may change in the future." )
@@ -222,13 +256,53 @@ public GrpcCallContext withExtraHeaders(Map<String, List<String>> extraHeaders)
222
256
ImmutableMap <String , List <String >> newExtraHeaders =
223
257
Headers .mergeHeaders (this .extraHeaders , extraHeaders );
224
258
return new GrpcCallContext (
225
- channel ,
226
- callOptions ,
227
- timeout ,
228
- streamWaitTimeout ,
229
- streamIdleTimeout ,
230
- channelAffinity ,
231
- newExtraHeaders );
259
+ this .channel ,
260
+ this .callOptions ,
261
+ this .timeout ,
262
+ this .streamWaitTimeout ,
263
+ this .streamIdleTimeout ,
264
+ this .channelAffinity ,
265
+ newExtraHeaders ,
266
+ this .retrySettings ,
267
+ this .retryableCodes );
268
+ }
269
+
270
+ @ Override
271
+ public RetrySettings getRetrySettings () {
272
+ return this .retrySettings ;
273
+ }
274
+
275
+ @ Override
276
+ public GrpcCallContext withRetrySettings (RetrySettings retrySettings ) {
277
+ return new GrpcCallContext (
278
+ this .channel ,
279
+ this .callOptions ,
280
+ this .timeout ,
281
+ this .streamWaitTimeout ,
282
+ this .streamIdleTimeout ,
283
+ this .channelAffinity ,
284
+ this .extraHeaders ,
285
+ retrySettings ,
286
+ this .retryableCodes );
287
+ }
288
+
289
+ @ Override
290
+ public Set <StatusCode .Code > getRetryableCodes () {
291
+ return this .retryableCodes ;
292
+ }
293
+
294
+ @ Override
295
+ public GrpcCallContext withRetryableCodes (Set <StatusCode .Code > retryableCodes ) {
296
+ return new GrpcCallContext (
297
+ this .channel ,
298
+ this .callOptions ,
299
+ this .timeout ,
300
+ this .streamWaitTimeout ,
301
+ this .streamIdleTimeout ,
302
+ this .channelAffinity ,
303
+ this .extraHeaders ,
304
+ this .retrySettings ,
305
+ retryableCodes );
232
306
}
233
307
234
308
@ Override
@@ -283,8 +357,18 @@ public ApiCallContext merge(ApiCallContext inputCallContext) {
283
357
newChannelAffinity = this .channelAffinity ;
284
358
}
285
359
360
+ RetrySettings newRetrySettings = grpcCallContext .retrySettings ;
361
+ if (newRetrySettings == null ) {
362
+ newRetrySettings = this .retrySettings ;
363
+ }
364
+
365
+ Set <StatusCode .Code > newRetryableCodes = grpcCallContext .retryableCodes ;
366
+ if (newRetryableCodes == null ) {
367
+ newRetryableCodes = this .retryableCodes ;
368
+ }
369
+
286
370
ImmutableMap <String , List <String >> newExtraHeaders =
287
- Headers .mergeHeaders (extraHeaders , grpcCallContext .extraHeaders );
371
+ Headers .mergeHeaders (this . extraHeaders , grpcCallContext .extraHeaders );
288
372
289
373
CallOptions newCallOptions =
290
374
grpcCallContext
@@ -303,7 +387,9 @@ public ApiCallContext merge(ApiCallContext inputCallContext) {
303
387
newStreamWaitTimeout ,
304
388
newStreamIdleTimeout ,
305
389
newChannelAffinity ,
306
- newExtraHeaders );
390
+ newExtraHeaders ,
391
+ newRetrySettings ,
392
+ newRetryableCodes );
307
393
}
308
394
309
395
/** The {@link Channel} set on this context. */
@@ -357,23 +443,27 @@ public GrpcCallContext withChannel(Channel newChannel) {
357
443
return new GrpcCallContext (
358
444
newChannel ,
359
445
this .callOptions ,
360
- timeout ,
446
+ this . timeout ,
361
447
this .streamWaitTimeout ,
362
448
this .streamIdleTimeout ,
363
449
this .channelAffinity ,
364
- this .extraHeaders );
450
+ this .extraHeaders ,
451
+ this .retrySettings ,
452
+ this .retryableCodes );
365
453
}
366
454
367
455
/** Returns a new instance with the call options set to the given call options. */
368
456
public GrpcCallContext withCallOptions (CallOptions newCallOptions ) {
369
457
return new GrpcCallContext (
370
458
this .channel ,
371
459
newCallOptions ,
372
- timeout ,
460
+ this . timeout ,
373
461
this .streamWaitTimeout ,
374
462
this .streamIdleTimeout ,
375
463
this .channelAffinity ,
376
- this .extraHeaders );
464
+ this .extraHeaders ,
465
+ this .retrySettings ,
466
+ this .retryableCodes );
377
467
}
378
468
379
469
public GrpcCallContext withRequestParamsDynamicHeaderOption (String requestParams ) {
@@ -410,7 +500,9 @@ public int hashCode() {
410
500
streamWaitTimeout ,
411
501
streamIdleTimeout ,
412
502
channelAffinity ,
413
- extraHeaders );
503
+ extraHeaders ,
504
+ retrySettings ,
505
+ retryableCodes );
414
506
}
415
507
416
508
@ Override
@@ -423,13 +515,15 @@ public boolean equals(Object o) {
423
515
}
424
516
425
517
GrpcCallContext that = (GrpcCallContext ) o ;
426
- return Objects .equals (channel , that .channel )
427
- && Objects .equals (callOptions , that .callOptions )
428
- && Objects .equals (timeout , that .timeout )
429
- && Objects .equals (streamWaitTimeout , that .streamWaitTimeout )
430
- && Objects .equals (streamIdleTimeout , that .streamIdleTimeout )
431
- && Objects .equals (channelAffinity , that .channelAffinity )
432
- && Objects .equals (extraHeaders , that .extraHeaders );
518
+ return Objects .equals (this .channel , that .channel )
519
+ && Objects .equals (this .callOptions , that .callOptions )
520
+ && Objects .equals (this .timeout , that .timeout )
521
+ && Objects .equals (this .streamWaitTimeout , that .streamWaitTimeout )
522
+ && Objects .equals (this .streamIdleTimeout , that .streamIdleTimeout )
523
+ && Objects .equals (this .channelAffinity , that .channelAffinity )
524
+ && Objects .equals (this .extraHeaders , that .extraHeaders )
525
+ && Objects .equals (this .retrySettings , that .retrySettings )
526
+ && Objects .equals (this .retryableCodes , that .retryableCodes );
433
527
}
434
528
435
529
Metadata getMetadata () {
0 commit comments