-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathManagedChannelBuilder.java
597 lines (559 loc) · 22.6 KB
/
ManagedChannelBuilder.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
* Copyright 2015 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc;
import com.google.common.base.Preconditions;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
/**
* A builder for {@link ManagedChannel} instances.
*
* @param <T> The concrete type of this builder.
*/
public abstract class ManagedChannelBuilder<T extends ManagedChannelBuilder<T>> {
/**
* Creates a channel with the target's address and port number.
*
* @see #forTarget(String)
* @since 1.0.0
*/
public static ManagedChannelBuilder<?> forAddress(String name, int port) {
return ManagedChannelProvider.provider().builderForAddress(name, port);
}
/**
* Creates a channel with a target string, which can be either a valid {@link
* NameResolver}-compliant URI, or an authority string.
*
* <p>A {@code NameResolver}-compliant URI is an absolute hierarchical URI as defined by {@link
* java.net.URI}. Example URIs:
* <ul>
* <li>{@code "dns:///foo.googleapis.com:8080"}</li>
* <li>{@code "dns:///foo.googleapis.com"}</li>
* <li>{@code "dns:///%5B2001:db8:85a3:8d3:1319:8a2e:370:7348%5D:443"}</li>
* <li>{@code "dns://8.8.8.8/foo.googleapis.com:8080"}</li>
* <li>{@code "dns://8.8.8.8/foo.googleapis.com"}</li>
* <li>{@code "zookeeper://zk.example.com:9900/example_service"}</li>
* </ul>
*
* <p>An authority string will be converted to a {@code NameResolver}-compliant URI, which has
* the scheme from the name resolver with the highest priority (e.g. {@code "dns"}),
* no authority, and the original authority string as its path after properly escaped.
* We recommend libraries to specify the schema explicitly if it is known, since libraries cannot
* know which NameResolver will be default during runtime.
* Example authority strings:
* <ul>
* <li>{@code "localhost"}</li>
* <li>{@code "127.0.0.1"}</li>
* <li>{@code "localhost:8080"}</li>
* <li>{@code "foo.googleapis.com:8080"}</li>
* <li>{@code "127.0.0.1:8080"}</li>
* <li>{@code "[2001:db8:85a3:8d3:1319:8a2e:370:7348]"}</li>
* <li>{@code "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443"}</li>
* </ul>
*
* @since 1.0.0
*/
public static ManagedChannelBuilder<?> forTarget(String target) {
return ManagedChannelProvider.provider().builderForTarget(target);
}
/**
* Execute application code directly in the transport thread.
*
* <p>Depending on the underlying transport, using a direct executor may lead to substantial
* performance improvements. However, it also requires the application to not block under
* any circumstances.
*
* <p>Calling this method is semantically equivalent to calling {@link #executor(Executor)} and
* passing in a direct executor. However, this is the preferred way as it may allow the transport
* to perform special optimizations.
*
* @return this
* @since 1.0.0
*/
public abstract T directExecutor();
/**
* Provides a custom executor.
*
* <p>It's an optional parameter. If the user has not provided an executor when the channel is
* built, the builder will use a static cached thread pool.
*
* <p>The channel won't take ownership of the given executor. It's caller's responsibility to
* shut down the executor when it's desired.
*
* @return this
* @since 1.0.0
*/
public abstract T executor(Executor executor);
/**
* Provides a custom executor that will be used for operations that block or are expensive.
*
* <p>It's an optional parameter. If the user has not provided an executor when the channel is
* built, the builder will use a static cached thread pool.
*
* <p>The channel won't take ownership of the given executor. It's caller's responsibility to shut
* down the executor when it's desired.
*
* @return this
* @throws UnsupportedOperationException if unsupported
* @since 1.25.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/6279")
public T offloadExecutor(Executor executor) {
throw new UnsupportedOperationException();
}
/**
* Adds interceptors that will be called before the channel performs its real work. This is
* functionally equivalent to using {@link ClientInterceptors#intercept(Channel, List)}, but while
* still having access to the original {@code ManagedChannel}. Interceptors run in the reverse
* order in which they are added, just as with consecutive calls to {@code
* ClientInterceptors.intercept()}.
*
* @return this
* @since 1.0.0
*/
public abstract T intercept(List<ClientInterceptor> interceptors);
/**
* Adds interceptors that will be called before the channel performs its real work. This is
* functionally equivalent to using {@link ClientInterceptors#intercept(Channel,
* ClientInterceptor...)}, but while still having access to the original {@code ManagedChannel}.
* Interceptors run in the reverse order in which they are added, just as with consecutive calls
* to {@code ClientInterceptors.intercept()}.
*
* @return this
* @since 1.0.0
*/
public abstract T intercept(ClientInterceptor... interceptors);
/**
* Provides a custom {@code User-Agent} for the application.
*
* <p>It's an optional parameter. The library will provide a user agent independent of this
* option. If provided, the given agent will prepend the library's user agent information.
*
* @return this
* @since 1.0.0
*/
public abstract T userAgent(String userAgent);
/**
* Overrides the authority used with TLS and HTTP virtual hosting. It does not change what host is
* actually connected to. Is commonly in the form {@code host:port}.
*
* <p>This method is intended for testing, but may safely be used outside of tests as an
* alternative to DNS overrides.
*
* @return this
* @since 1.0.0
*/
public abstract T overrideAuthority(String authority);
/**
* Use of a plaintext connection to the server. By default a secure connection mechanism
* such as TLS will be used.
*
* <p>Should only be used for testing or for APIs where the use of such API or the data
* exchanged is not sensitive.
*
* <p>This assumes prior knowledge that the target of this channel is using plaintext. It will
* not perform HTTP/1.1 upgrades.
*
* @return this
* @throws IllegalStateException if ChannelCredentials were provided when constructing the builder
* @throws UnsupportedOperationException if plaintext mode is not supported.
* @since 1.11.0
*/
public T usePlaintext() {
throw new UnsupportedOperationException();
}
/**
* Makes the client use TLS.
*
* @return this
* @throws IllegalStateException if ChannelCredentials were provided when constructing the builder
* @throws UnsupportedOperationException if transport security is not supported.
* @since 1.9.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/3713")
public T useTransportSecurity() {
throw new UnsupportedOperationException();
}
/**
* Provides a custom {@link NameResolver.Factory} for the channel. If this method is not called,
* the builder will try the providers registered in the default {@link NameResolverRegistry} for
* the given target.
*
* <p>This method should rarely be used, as name resolvers should provide a {@code
* NameResolverProvider} and users rely on service loading to find implementations in the class
* path. That allows application's configuration to easily choose the name resolver via the
* 'target' string passed to {@link ManagedChannelBuilder#forTarget(String)}.
*
* @return this
* @since 1.0.0
* @deprecated Most usages should use a globally-registered {@link NameResolverProvider} instead,
* with either the SPI mechanism or {@link NameResolverRegistry#register}. Replacements for
* all use-cases are not necessarily available yet. See
* <a href="https://github.com/grpc/grpc-java/issues/7133">#7133</a>.
*/
@Deprecated
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1770")
public abstract T nameResolverFactory(NameResolver.Factory resolverFactory);
/**
* Sets the default load-balancing policy that will be used if the service config doesn't specify
* one. If not set, the default will be the "pick_first" policy.
*
* <p>Policy implementations are looked up in the
* {@link LoadBalancerRegistry#getDefaultRegistry default LoadBalancerRegistry}.
*
* <p>This method is implemented by all stock channel builders that are shipped with gRPC, but may
* not be implemented by custom channel builders, in which case this method will throw.
*
* @return this
* @since 1.18.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771")
public T defaultLoadBalancingPolicy(String policy) {
throw new UnsupportedOperationException();
}
/**
* Enables full-stream decompression of inbound streams. This will cause the channel's outbound
* headers to advertise support for GZIP compressed streams, and gRPC servers which support the
* feature may respond with a GZIP compressed stream.
*
* <p>EXPERIMENTAL: This method is here to enable an experimental feature, and may be changed or
* removed once the feature is stable.
*
* @throws UnsupportedOperationException if unsupported
* @since 1.7.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/3399")
public T enableFullStreamDecompression() {
throw new UnsupportedOperationException();
}
/**
* Set the decompression registry for use in the channel. This is an advanced API call and
* shouldn't be used unless you are using custom message encoding. The default supported
* decompressors are in {@link DecompressorRegistry#getDefaultInstance}.
*
* @return this
* @since 1.0.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1704")
public abstract T decompressorRegistry(DecompressorRegistry registry);
/**
* Set the compression registry for use in the channel. This is an advanced API call and
* shouldn't be used unless you are using custom message encoding. The default supported
* compressors are in {@link CompressorRegistry#getDefaultInstance}.
*
* @return this
* @since 1.0.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1704")
public abstract T compressorRegistry(CompressorRegistry registry);
/**
* Set the duration without ongoing RPCs before going to idle mode.
*
* <p>In idle mode the channel shuts down all connections, the NameResolver and the
* LoadBalancer. A new RPC would take the channel out of idle mode. A channel starts in idle mode.
* Defaults to 30 minutes.
*
* <p>This is an advisory option. Do not rely on any specific behavior related to this option.
*
* @return this
* @since 1.0.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2022")
public abstract T idleTimeout(long value, TimeUnit unit);
/**
* Sets the maximum message size allowed to be received on the channel. If not called,
* defaults to 4 MiB. The default provides protection to clients who haven't considered the
* possibility of receiving large messages while trying to be large enough to not be hit in normal
* usage.
*
* <p>This method is advisory, and implementations may decide to not enforce this. Currently,
* the only known transport to not enforce this is {@code InProcessTransport}.
*
* @param bytes the maximum number of bytes a single message can be.
* @return this
* @throws IllegalArgumentException if bytes is negative.
* @since 1.1.0
*/
public T maxInboundMessageSize(int bytes) {
// intentional noop rather than throw, this method is only advisory.
Preconditions.checkArgument(bytes >= 0, "bytes must be >= 0");
return thisT();
}
/**
* Sets the maximum size of metadata allowed to be received. {@code Integer.MAX_VALUE} disables
* the enforcement. The default is implementation-dependent, but is not generally less than 8 KiB
* and may be unlimited.
*
* <p>This is cumulative size of the metadata. The precise calculation is
* implementation-dependent, but implementations are encouraged to follow the calculation used for
* <a href="http://httpwg.org/specs/rfc7540.html#rfc.section.6.5.2">
* HTTP/2's SETTINGS_MAX_HEADER_LIST_SIZE</a>. It sums the bytes from each entry's key and value,
* plus 32 bytes of overhead per entry.
*
* @param bytes the maximum size of received metadata
* @return this
* @throws IllegalArgumentException if bytes is non-positive
* @since 1.17.0
*/
public T maxInboundMetadataSize(int bytes) {
Preconditions.checkArgument(bytes > 0, "maxInboundMetadataSize must be > 0");
// intentional noop rather than throw, this method is only advisory.
return thisT();
}
/**
* Sets the time without read activity before sending a keepalive ping. An unreasonably small
* value might be increased, and {@code Long.MAX_VALUE} nano seconds or an unreasonably large
* value will disable keepalive. Defaults to infinite.
*
* <p>Clients must receive permission from the service owner before enabling this option.
* Keepalives can increase the load on services and are commonly "invisible" making it hard to
* notice when they are causing excessive load. Clients are strongly encouraged to use only as
* small of a value as necessary.
*
* @throws UnsupportedOperationException if unsupported
* @since 1.7.0
*/
public T keepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
throw new UnsupportedOperationException();
}
/**
* Sets the time waiting for read activity after sending a keepalive ping. If the time expires
* without any read activity on the connection, the connection is considered dead. An unreasonably
* small value might be increased. Defaults to 20 seconds.
*
* <p>This value should be at least multiple times the RTT to allow for lost packets.
*
* @throws UnsupportedOperationException if unsupported
* @since 1.7.0
*/
public T keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit) {
throw new UnsupportedOperationException();
}
/**
* Sets whether keepalive will be performed when there are no outstanding RPC on a connection.
* Defaults to {@code false}.
*
* <p>Clients must receive permission from the service owner before enabling this option.
* Keepalives on unused connections can easilly accidentally consume a considerable amount of
* bandwidth and CPU. {@link ManagedChannelBuilder#idleTimeout idleTimeout()} should generally be
* used instead of this option.
*
* @throws UnsupportedOperationException if unsupported
* @see #keepAliveTime(long, TimeUnit)
* @since 1.7.0
*/
public T keepAliveWithoutCalls(boolean enable) {
throw new UnsupportedOperationException();
}
/**
* Sets the maximum number of retry attempts that may be configured by the service config. If the
* service config specifies a larger value it will be reduced to this value. Setting this number
* to zero is not effectively the same as {@code disableRetry()} because the former does not
* disable
* <a
* href="https://github.com/grpc/proposal/blob/master/A6-client-retries.md#transparent-retries">
* transparent retry</a>.
*
* <p>This method may not work as expected for the current release because retry is not fully
* implemented yet.
*
* @return this
* @since 1.11.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/3982")
public T maxRetryAttempts(int maxRetryAttempts) {
throw new UnsupportedOperationException();
}
/**
* Sets the maximum number of hedged attempts that may be configured by the service config. If the
* service config specifies a larger value it will be reduced to this value.
*
* <p>This method may not work as expected for the current release because retry is not fully
* implemented yet.
*
* @return this
* @since 1.11.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/3982")
public T maxHedgedAttempts(int maxHedgedAttempts) {
throw new UnsupportedOperationException();
}
/**
* Sets the retry buffer size in bytes. If the buffer limit is exceeded, no RPC
* could retry at the moment, and in hedging case all hedges but one of the same RPC will cancel.
* The implementation may only estimate the buffer size being used rather than count the
* exact physical memory allocated. The method does not have any effect if retry is disabled by
* the client.
*
* <p>This method may not work as expected for the current release because retry is not fully
* implemented yet.
*
* @return this
* @since 1.10.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/3982")
public T retryBufferSize(long bytes) {
throw new UnsupportedOperationException();
}
/**
* Sets the per RPC buffer limit in bytes used for retry. The RPC is not retriable if its buffer
* limit is exceeded. The implementation may only estimate the buffer size being used rather than
* count the exact physical memory allocated. It does not have any effect if retry is disabled by
* the client.
*
* <p>This method may not work as expected for the current release because retry is not fully
* implemented yet.
*
* @return this
* @since 1.10.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/3982")
public T perRpcBufferLimit(long bytes) {
throw new UnsupportedOperationException();
}
/**
* Disables the retry and hedging subsystem provided by the gRPC library. This is designed for the
* case when users have their own retry implementation and want to avoid their own retry taking
* place simultaneously with the gRPC library layer retry.
*
* @return this
* @since 1.11.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/3982")
public T disableRetry() {
throw new UnsupportedOperationException();
}
/**
* Enables the retry and hedging subsystem which will use
* <a href="https://github.com/grpc/proposal/blob/master/A6-client-retries.md#integration-with-service-config">
* per-method configuration</a>. If a method is unconfigured, it will be limited to
* transparent retries, which are safe for non-idempotent RPCs. Service config is ideally provided
* by the name resolver, but may also be specified via {@link #defaultServiceConfig}.
*
* <p>For the current release, this method may have a side effect that disables Census stats and
* tracing.
*
* @return this
* @since 1.11.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/3982")
public T enableRetry() {
throw new UnsupportedOperationException();
}
/**
* Sets the BinaryLog object that this channel should log to. The channel does not take
* ownership of the object, and users are responsible for calling {@link BinaryLog#close()}.
*
* @param binaryLog the object to provide logging.
* @return this
* @since 1.13.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4017")
public T setBinaryLog(BinaryLog binaryLog) {
throw new UnsupportedOperationException();
}
/**
* Sets the maximum number of channel trace events to keep in the tracer for each channel or
* subchannel. If set to 0, channel tracing is effectively disabled.
*
* @return this
* @since 1.13.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4471")
public T maxTraceEvents(int maxTraceEvents) {
throw new UnsupportedOperationException();
}
/**
* Sets the proxy detector to be used in addresses name resolution. If <code>null</code> is passed
* the default proxy detector will be used. For how proxies work in gRPC, please refer to the
* documentation on {@link ProxyDetector}.
*
* @return this
* @since 1.19.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/5113")
public T proxyDetector(ProxyDetector proxyDetector) {
throw new UnsupportedOperationException();
}
/**
* Provides a service config to the channel. The channel will use the default service config when
* the name resolver provides no service config or if the channel disables lookup service config
* from name resolver (see {@link #disableServiceConfigLookUp()}). The argument
* {@code serviceConfig} is a nested map representing a Json object in the most natural way:
*
* <table border="1">
* <tr>
* <td>Json entry</td><td>Java Type</td>
* </tr>
* <tr>
* <td>object</td><td>{@link Map}</td>
* </tr>
* <tr>
* <td>array</td><td>{@link List}</td>
* </tr>
* <tr>
* <td>string</td><td>{@link String}</td>
* </tr>
* <tr>
* <td>number</td><td>{@link Double}</td>
* </tr>
* <tr>
* <td>boolean</td><td>{@link Boolean}</td>
* </tr>
* <tr>
* <td>null</td><td>{@code null}</td>
* </tr>
* </table>
*
* <p>If null is passed, then there will be no default service config.
*
* @throws IllegalArgumentException When the given serviceConfig is invalid or the current version
* of grpc library can not parse it gracefully. The state of the builder is unchanged if
* an exception is thrown.
* @return this
* @since 1.20.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/5189")
public T defaultServiceConfig(@Nullable Map<String, ?> serviceConfig) {
throw new UnsupportedOperationException();
}
/**
* Disables service config look-up from the naming system, which is enabled by default.
*
* @return this
* @since 1.20.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/5189")
public T disableServiceConfigLookUp() {
throw new UnsupportedOperationException();
}
/**
* Builds a channel using the given parameters.
*
* @since 1.0.0
*/
public abstract ManagedChannel build();
/**
* Returns the correctly typed version of the builder.
*/
private T thisT() {
@SuppressWarnings("unchecked")
T thisT = (T) this;
return thisT;
}
}