34
34
import com .google .api .gax .rpc .ApiCallContext ;
35
35
import com .google .api .gax .rpc .StatusCode ;
36
36
import com .google .api .gax .rpc .TransportChannel ;
37
+ import com .google .api .gax .rpc .internal .ApiCallContextOptions ;
37
38
import com .google .api .gax .rpc .internal .Headers ;
38
39
import com .google .api .gax .tracing .ApiTracer ;
39
40
import com .google .api .gax .tracing .BaseApiTracer ;
43
44
import com .google .common .collect .ImmutableSet ;
44
45
import io .grpc .CallCredentials ;
45
46
import io .grpc .CallOptions ;
46
- import io .grpc .CallOptions .Key ;
47
47
import io .grpc .Channel ;
48
48
import io .grpc .Deadline ;
49
49
import io .grpc .Metadata ;
66
66
*/
67
67
@ BetaApi ("Reference ApiCallContext instead - this class is likely to experience breaking changes" )
68
68
public final class GrpcCallContext implements ApiCallContext {
69
- static final CallOptions .Key <ApiTracer > TRACER_KEY = Key .create ("gax.tracer" );
69
+ static final CallOptions .Key <ApiTracer > TRACER_KEY = CallOptions . Key .create ("gax.tracer" );
70
70
71
71
private final Channel channel ;
72
72
private final CallOptions callOptions ;
@@ -77,6 +77,7 @@ public final class GrpcCallContext implements ApiCallContext {
77
77
@ Nullable private final RetrySettings retrySettings ;
78
78
@ Nullable private final ImmutableSet <StatusCode .Code > retryableCodes ;
79
79
private final ImmutableMap <String , List <String >> extraHeaders ;
80
+ private final ApiCallContextOptions options ;
80
81
81
82
/** Returns an empty instance with a null channel and default {@link CallOptions}. */
82
83
public static GrpcCallContext createDefault () {
@@ -88,6 +89,7 @@ public static GrpcCallContext createDefault() {
88
89
null ,
89
90
null ,
90
91
ImmutableMap .<String , List <String >>of (),
92
+ ApiCallContextOptions .getDefaultOptions (),
91
93
null ,
92
94
null );
93
95
}
@@ -102,6 +104,7 @@ public static GrpcCallContext of(Channel channel, CallOptions callOptions) {
102
104
null ,
103
105
null ,
104
106
ImmutableMap .<String , List <String >>of (),
107
+ ApiCallContextOptions .getDefaultOptions (),
105
108
null ,
106
109
null );
107
110
}
@@ -114,6 +117,7 @@ private GrpcCallContext(
114
117
@ Nullable Duration streamIdleTimeout ,
115
118
@ Nullable Integer channelAffinity ,
116
119
ImmutableMap <String , List <String >> extraHeaders ,
120
+ ApiCallContextOptions options ,
117
121
@ Nullable RetrySettings retrySettings ,
118
122
@ Nullable Set <StatusCode .Code > retryableCodes ) {
119
123
this .channel = channel ;
@@ -123,6 +127,7 @@ private GrpcCallContext(
123
127
this .streamIdleTimeout = streamIdleTimeout ;
124
128
this .channelAffinity = channelAffinity ;
125
129
this .extraHeaders = Preconditions .checkNotNull (extraHeaders );
130
+ this .options = Preconditions .checkNotNull (options );
126
131
this .retrySettings = retrySettings ;
127
132
this .retryableCodes = retryableCodes == null ? null : ImmutableSet .copyOf (retryableCodes );
128
133
}
@@ -187,6 +192,7 @@ public GrpcCallContext withTimeout(@Nullable Duration timeout) {
187
192
this .streamIdleTimeout ,
188
193
this .channelAffinity ,
189
194
this .extraHeaders ,
195
+ this .options ,
190
196
this .retrySettings ,
191
197
this .retryableCodes );
192
198
}
@@ -212,6 +218,7 @@ public GrpcCallContext withStreamWaitTimeout(@Nullable Duration streamWaitTimeou
212
218
this .streamIdleTimeout ,
213
219
this .channelAffinity ,
214
220
this .extraHeaders ,
221
+ this .options ,
215
222
this .retrySettings ,
216
223
this .retryableCodes );
217
224
}
@@ -231,6 +238,7 @@ public GrpcCallContext withStreamIdleTimeout(@Nullable Duration streamIdleTimeou
231
238
streamIdleTimeout ,
232
239
this .channelAffinity ,
233
240
this .extraHeaders ,
241
+ this .options ,
234
242
this .retrySettings ,
235
243
this .retryableCodes );
236
244
}
@@ -245,6 +253,7 @@ public GrpcCallContext withChannelAffinity(@Nullable Integer affinity) {
245
253
this .streamIdleTimeout ,
246
254
affinity ,
247
255
this .extraHeaders ,
256
+ this .options ,
248
257
this .retrySettings ,
249
258
this .retryableCodes );
250
259
}
@@ -263,6 +272,7 @@ public GrpcCallContext withExtraHeaders(Map<String, List<String>> extraHeaders)
263
272
this .streamIdleTimeout ,
264
273
this .channelAffinity ,
265
274
newExtraHeaders ,
275
+ this .options ,
266
276
this .retrySettings ,
267
277
this .retryableCodes );
268
278
}
@@ -282,6 +292,7 @@ public GrpcCallContext withRetrySettings(RetrySettings retrySettings) {
282
292
this .streamIdleTimeout ,
283
293
this .channelAffinity ,
284
294
this .extraHeaders ,
295
+ this .options ,
285
296
retrySettings ,
286
297
this .retryableCodes );
287
298
}
@@ -301,6 +312,7 @@ public GrpcCallContext withRetryableCodes(Set<StatusCode.Code> retryableCodes) {
301
312
this .streamIdleTimeout ,
302
313
this .channelAffinity ,
303
314
this .extraHeaders ,
315
+ this .options ,
304
316
this .retrySettings ,
305
317
retryableCodes );
306
318
}
@@ -370,6 +382,8 @@ public ApiCallContext merge(ApiCallContext inputCallContext) {
370
382
ImmutableMap <String , List <String >> newExtraHeaders =
371
383
Headers .mergeHeaders (this .extraHeaders , grpcCallContext .extraHeaders );
372
384
385
+ ApiCallContextOptions newOptions = options .merge (grpcCallContext .options );
386
+
373
387
CallOptions newCallOptions =
374
388
grpcCallContext
375
389
.callOptions
@@ -388,6 +402,7 @@ public ApiCallContext merge(ApiCallContext inputCallContext) {
388
402
newStreamIdleTimeout ,
389
403
newChannelAffinity ,
390
404
newExtraHeaders ,
405
+ newOptions ,
391
406
newRetrySettings ,
392
407
newRetryableCodes );
393
408
}
@@ -448,6 +463,7 @@ public GrpcCallContext withChannel(Channel newChannel) {
448
463
this .streamIdleTimeout ,
449
464
this .channelAffinity ,
450
465
this .extraHeaders ,
466
+ this .options ,
451
467
this .retrySettings ,
452
468
this .retryableCodes );
453
469
}
@@ -462,6 +478,7 @@ public GrpcCallContext withCallOptions(CallOptions newCallOptions) {
462
478
this .streamIdleTimeout ,
463
479
this .channelAffinity ,
464
480
this .extraHeaders ,
481
+ this .options ,
465
482
this .retrySettings ,
466
483
this .retryableCodes );
467
484
}
@@ -491,6 +508,29 @@ public GrpcCallContext withTracer(@Nonnull ApiTracer tracer) {
491
508
return withCallOptions (callOptions .withOption (TRACER_KEY , tracer ));
492
509
}
493
510
511
+ /** {@inheritDoc} */
512
+ @ Override
513
+ public <T > GrpcCallContext withOption (Key <T > key , T value ) {
514
+ ApiCallContextOptions newOptions = options .withOption (key , value );
515
+ return new GrpcCallContext (
516
+ this .channel ,
517
+ this .callOptions ,
518
+ this .timeout ,
519
+ this .streamWaitTimeout ,
520
+ this .streamIdleTimeout ,
521
+ this .channelAffinity ,
522
+ this .extraHeaders ,
523
+ newOptions ,
524
+ this .retrySettings ,
525
+ this .retryableCodes );
526
+ }
527
+
528
+ /** {@inheritDoc} */
529
+ @ Override
530
+ public <T > T getOption (Key <T > key ) {
531
+ return options .getOption (key );
532
+ }
533
+
494
534
@ Override
495
535
public int hashCode () {
496
536
return Objects .hash (
@@ -501,6 +541,7 @@ public int hashCode() {
501
541
streamIdleTimeout ,
502
542
channelAffinity ,
503
543
extraHeaders ,
544
+ options ,
504
545
retrySettings ,
505
546
retryableCodes );
506
547
}
@@ -522,6 +563,7 @@ public boolean equals(Object o) {
522
563
&& Objects .equals (this .streamIdleTimeout , that .streamIdleTimeout )
523
564
&& Objects .equals (this .channelAffinity , that .channelAffinity )
524
565
&& Objects .equals (this .extraHeaders , that .extraHeaders )
566
+ && Objects .equals (this .options , that .options )
525
567
&& Objects .equals (this .retrySettings , that .retrySettings )
526
568
&& Objects .equals (this .retryableCodes , that .retryableCodes );
527
569
}
0 commit comments