|
25 | 25 | import io.grpc.CompressorRegistry; |
26 | 26 | import io.grpc.DecompressorRegistry; |
27 | 27 | import io.grpc.EquivalentAddressGroup; |
| 28 | +import io.grpc.InternalChannelz; |
28 | 29 | import io.grpc.ManagedChannel; |
29 | 30 | import io.grpc.ManagedChannelBuilder; |
30 | 31 | import io.grpc.NameResolver; |
| 32 | +import io.grpc.NameResolverRegistry; |
31 | 33 | import io.grpc.ProxyDetector; |
32 | 34 | import java.lang.reflect.InvocationTargetException; |
33 | 35 | import java.lang.reflect.Method; |
|
51 | 53 | */ |
52 | 54 | public final class ManagedChannelImplBuilder |
53 | 55 | extends AbstractManagedChannelImplBuilder<ManagedChannelImplBuilder> { |
| 56 | + private static final Logger log = Logger.getLogger(ManagedChannelImplBuilder.class.getName()); |
| 57 | + private static final String DIRECT_ADDRESS_SCHEME = "directaddress"; |
| 58 | + private static final long DEFAULT_RETRY_BUFFER_SIZE_IN_BYTES = 1L << 24; // 16M |
| 59 | + private static final long DEFAULT_PER_RPC_BUFFER_LIMIT_IN_BYTES = 1L << 20; // 1Ms |
54 | 60 |
|
55 | | - /** |
56 | | - * An interface for Transport implementors to provide the {@link ClientTransportFactory} |
57 | | - * appropriate for the channel. |
58 | | - */ |
59 | | - public interface ClientTransportFactoryBuilder { |
60 | | - ClientTransportFactory buildClientTransportFactory(); |
61 | | - } |
62 | | - |
63 | | - /** |
64 | | - * TODO(sergiitk): javadoc. |
65 | | - */ |
66 | | - public static class ClientTransportFactoryBuilderImpl implements ClientTransportFactoryBuilder { |
67 | | - @Override |
68 | | - public ClientTransportFactory buildClientTransportFactory() { |
69 | | - throw new UnsupportedOperationException(); |
70 | | - } |
71 | | - } |
72 | | - |
73 | | - /** |
74 | | - * An interface for Transport implementors to provide a default port to {@link |
75 | | - * io.grpc.NameResolver} for use in cases where the target string doesn't include a port. The |
76 | | - * default implementation returns {@link GrpcUtil#DEFAULT_PORT_SSL}. |
77 | | - */ |
78 | | - public interface ChannelBuilderDefaultPortProvider { |
79 | | - int getDefaultPort(); |
80 | | - } |
81 | | - |
82 | | - /** |
83 | | - * Default implementation of {@link ChannelBuilderDefaultPortProvider} that returns a fixed port. |
84 | | - */ |
85 | | - public static final class FixedPortProvider implements ChannelBuilderDefaultPortProvider { |
86 | | - private final int port; |
| 61 | + private static final ObjectPool<? extends Executor> DEFAULT_EXECUTOR_POOL = |
| 62 | + SharedResourcePool.forResource(GrpcUtil.SHARED_CHANNEL_EXECUTOR); |
| 63 | + private static final DecompressorRegistry DEFAULT_DECOMPRESSOR_REGISTRY = |
| 64 | + DecompressorRegistry.getDefaultInstance(); |
| 65 | + private static final CompressorRegistry DEFAULT_COMPRESSOR_REGISTRY = |
| 66 | + CompressorRegistry.getDefaultInstance(); |
87 | 67 |
|
88 | | - public FixedPortProvider(int port) { |
89 | | - this.port = port; |
90 | | - } |
91 | 68 |
|
92 | | - @Override |
93 | | - public int getDefaultPort() { |
94 | | - return port; |
95 | | - } |
96 | | - } |
| 69 | + /** An idle timeout smaller than this would be capped to it. */ |
| 70 | + static final long IDLE_MODE_MIN_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(1); |
97 | 71 |
|
98 | | - private static final class ManagedChannelDefaultPortProvider implements |
99 | | - ChannelBuilderDefaultPortProvider { |
100 | | - @Override |
101 | | - public int getDefaultPort() { |
102 | | - return GrpcUtil.DEFAULT_PORT_SSL; |
103 | | - } |
104 | | - } |
| 72 | + /** An idle timeout larger than this would disable idle mode. */ |
| 73 | + @VisibleForTesting static final long IDLE_MODE_MAX_TIMEOUT_DAYS = 30; |
105 | 74 |
|
106 | | - private static final String DIRECT_ADDRESS_SCHEME = "directaddress"; |
107 | | - private static final Logger log = Logger.getLogger(ManagedChannelImplBuilder.class.getName()); |
| 75 | + /** The default idle timeout. */ |
| 76 | + @VisibleForTesting |
| 77 | + static final long IDLE_MODE_DEFAULT_TIMEOUT_MILLIS = TimeUnit.MINUTES.toMillis(30); |
108 | 78 |
|
109 | 79 | final String target; |
110 | 80 | @Nullable |
111 | 81 | private final SocketAddress directServerAddress; |
112 | 82 | private final ClientTransportFactoryBuilder clientTransportFactoryBuilder; |
113 | 83 | private final ChannelBuilderDefaultPortProvider channelBuilderDefaultPortProvider; |
| 84 | + private final List<ClientInterceptor> interceptors = new ArrayList<>(); |
| 85 | + |
| 86 | + @VisibleForTesting |
| 87 | + @Nullable |
| 88 | + String authorityOverride; |
114 | 89 | private boolean authorityCheckerDisabled; |
| 90 | + private boolean statsEnabled = true; |
| 91 | + private boolean recordStartedRpcs = true; |
| 92 | + private boolean recordFinishedRpcs = true; |
| 93 | + private boolean recordRealTimeMetrics = false; |
| 94 | + private boolean tracingEnabled = true; |
115 | 95 |
|
116 | | - public static ManagedChannelBuilder<?> forAddress(String name, int port) { |
117 | | - throw new UnsupportedOperationException( |
118 | | - "ClientTransportFactoryBuilder is required, use a constructor"); |
119 | | - } |
| 96 | + ObjectPool<? extends Executor> executorPool = DEFAULT_EXECUTOR_POOL; |
| 97 | + ObjectPool<? extends Executor> offloadExecutorPool = DEFAULT_EXECUTOR_POOL; |
120 | 98 |
|
121 | | - public static ManagedChannelBuilder<?> forTarget(String target) { |
122 | | - throw new UnsupportedOperationException( |
123 | | - "ClientTransportFactoryBuilder is required, use a constructor"); |
124 | | - } |
| 99 | + DecompressorRegistry decompressorRegistry = DEFAULT_DECOMPRESSOR_REGISTRY; |
| 100 | + CompressorRegistry compressorRegistry = DEFAULT_COMPRESSOR_REGISTRY; |
| 101 | + |
| 102 | + final NameResolverRegistry nameResolverRegistry = NameResolverRegistry.getDefaultRegistry(); |
| 103 | + // Access via getter, which may perform authority override as needed |
| 104 | + NameResolver.Factory nameResolverFactory = nameResolverRegistry.asFactory(); |
| 105 | + |
| 106 | + int maxRetryAttempts = 5; |
| 107 | + int maxHedgedAttempts = 5; |
| 108 | + long retryBufferSize = DEFAULT_RETRY_BUFFER_SIZE_IN_BYTES; |
| 109 | + long perRpcBufferLimit = DEFAULT_PER_RPC_BUFFER_LIMIT_IN_BYTES; |
| 110 | + boolean retryEnabled = false; // TODO(zdapeng): default to true |
| 111 | + |
| 112 | + long idleTimeoutMillis = IDLE_MODE_DEFAULT_TIMEOUT_MILLIS; |
| 113 | + @Nullable |
| 114 | + String userAgent; |
| 115 | + String defaultLbPolicy = GrpcUtil.DEFAULT_LB_POLICY; |
| 116 | + boolean fullStreamDecompression; |
| 117 | + // Temporarily disable retry when stats or tracing is enabled to avoid breakage, until we know |
| 118 | + // what should be the desired behavior for retry + stats/tracing. |
| 119 | + // TODO(zdapeng): delete me |
| 120 | + boolean temporarilyDisableRetry; |
| 121 | + int maxTraceEvents; |
| 122 | + InternalChannelz channelz = InternalChannelz.instance(); |
| 123 | + @Nullable |
| 124 | + Map<String, ?> defaultServiceConfig; |
| 125 | + boolean lookUpServiceConfig = true; |
| 126 | + @Nullable |
| 127 | + BinaryLog binlog; |
| 128 | + @Nullable |
| 129 | + ProxyDetector proxyDetector; |
125 | 130 |
|
126 | 131 | /** |
127 | 132 | * Creates a new managed channel builder with a target string, which can be either a valid {@link |
@@ -319,18 +324,6 @@ public ManagedChannelImplBuilder idleTimeout(long value, TimeUnit unit) { |
319 | 324 | return this; |
320 | 325 | } |
321 | 326 |
|
322 | | - /** |
323 | | - * Sets the maximum message size allowed for a single gRPC frame. If an inbound messages larger |
324 | | - * than this limit is received it will not be processed and the RPC will fail with |
325 | | - * RESOURCE_EXHAUSTED. |
326 | | - */ |
327 | | - @Override |
328 | | - public ManagedChannelImplBuilder maxInboundMessageSize(int max) { |
329 | | - Preconditions.checkArgument(max >= 0, "negative max"); |
330 | | - maxInboundMessageSize = max; |
331 | | - return this; |
332 | | - } |
333 | | - |
334 | 327 | @Override |
335 | 328 | public ManagedChannelImplBuilder maxRetryAttempts(int maxRetryAttempts) { |
336 | 329 | this.maxRetryAttempts = maxRetryAttempts; |
@@ -621,6 +614,67 @@ final List<ClientInterceptor> getEffectiveInterceptors() { |
621 | 614 | return effectiveInterceptors; |
622 | 615 | } |
623 | 616 |
|
| 617 | + public static ManagedChannelBuilder<?> forAddress(String name, int port) { |
| 618 | + throw new UnsupportedOperationException( |
| 619 | + "ClientTransportFactoryBuilder is required, use a constructor"); |
| 620 | + } |
| 621 | + |
| 622 | + public static ManagedChannelBuilder<?> forTarget(String target) { |
| 623 | + throw new UnsupportedOperationException( |
| 624 | + "ClientTransportFactoryBuilder is required, use a constructor"); |
| 625 | + } |
| 626 | + |
| 627 | + /** |
| 628 | + * An interface for Transport implementors to provide the {@link ClientTransportFactory} |
| 629 | + * appropriate for the channel. |
| 630 | + */ |
| 631 | + public interface ClientTransportFactoryBuilder { |
| 632 | + ClientTransportFactory buildClientTransportFactory(); |
| 633 | + } |
| 634 | + |
| 635 | + /** |
| 636 | + * TODO(sergiitk): javadoc. |
| 637 | + */ |
| 638 | + public static class ClientTransportFactoryBuilderImpl implements ClientTransportFactoryBuilder { |
| 639 | + @Override |
| 640 | + public ClientTransportFactory buildClientTransportFactory() { |
| 641 | + throw new UnsupportedOperationException(); |
| 642 | + } |
| 643 | + } |
| 644 | + |
| 645 | + /** |
| 646 | + * An interface for Transport implementors to provide a default port to {@link |
| 647 | + * io.grpc.NameResolver} for use in cases where the target string doesn't include a port. The |
| 648 | + * default implementation returns {@link GrpcUtil#DEFAULT_PORT_SSL}. |
| 649 | + */ |
| 650 | + public interface ChannelBuilderDefaultPortProvider { |
| 651 | + int getDefaultPort(); |
| 652 | + } |
| 653 | + |
| 654 | + /** |
| 655 | + * Default implementation of {@link ChannelBuilderDefaultPortProvider} that returns a fixed port. |
| 656 | + */ |
| 657 | + public static final class FixedPortProvider implements ChannelBuilderDefaultPortProvider { |
| 658 | + private final int port; |
| 659 | + |
| 660 | + public FixedPortProvider(int port) { |
| 661 | + this.port = port; |
| 662 | + } |
| 663 | + |
| 664 | + @Override |
| 665 | + public int getDefaultPort() { |
| 666 | + return port; |
| 667 | + } |
| 668 | + } |
| 669 | + |
| 670 | + private static final class ManagedChannelDefaultPortProvider implements |
| 671 | + ChannelBuilderDefaultPortProvider { |
| 672 | + @Override |
| 673 | + public int getDefaultPort() { |
| 674 | + return GrpcUtil.DEFAULT_PORT_SSL; |
| 675 | + } |
| 676 | + } |
| 677 | + |
624 | 678 | private static class DirectAddressNameResolverFactory extends NameResolver.Factory { |
625 | 679 | final SocketAddress address; |
626 | 680 | final String authority; |
|
0 commit comments