lryan | 56e307f | 2014-12-05 13:25:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014, Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * * Neither the name of Google Inc. nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
nmittler | b687bdc | 2015-08-31 16:13:39 -0700 | [diff] [blame] | 32 | package io.grpc.internal; |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 33 | |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 34 | import static com.google.common.base.MoreObjects.firstNonNull; |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 35 | import static com.google.common.base.Preconditions.checkArgument; |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 36 | |
Kun Zhang | 631a9d5 | 2016-06-02 16:47:36 -0700 | [diff] [blame] | 37 | import com.google.common.annotations.VisibleForTesting; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 38 | import com.google.common.base.Preconditions; |
buchgr | 602473d | 2015-11-11 12:53:08 +0100 | [diff] [blame] | 39 | import com.google.common.util.concurrent.MoreExecutors; |
Kun Zhang | cce8eac | 2017-01-05 10:48:13 -0800 | [diff] [blame] | 40 | import com.google.instrumentation.stats.Stats; |
| 41 | import com.google.instrumentation.stats.StatsContextFactory; |
Kun Zhang | 49bde54 | 2017-04-25 13:53:29 -0700 | [diff] [blame] | 42 | import com.google.instrumentation.trace.Tracing; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 43 | import io.grpc.Attributes; |
| 44 | import io.grpc.ClientInterceptor; |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 45 | import io.grpc.CompressorRegistry; |
| 46 | import io.grpc.DecompressorRegistry; |
Kun Zhang | 418d52d | 2017-03-22 18:29:31 -0700 | [diff] [blame] | 47 | import io.grpc.EquivalentAddressGroup; |
Kun Zhang | a9bd947 | 2017-02-21 17:11:03 -0800 | [diff] [blame] | 48 | import io.grpc.LoadBalancer; |
Kun Zhang | d17a7b5 | 2017-01-10 15:30:12 -0800 | [diff] [blame] | 49 | import io.grpc.ManagedChannel; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 50 | import io.grpc.ManagedChannelBuilder; |
| 51 | import io.grpc.NameResolver; |
Eric Anderson | 56a2938 | 2016-06-11 11:55:49 -0700 | [diff] [blame] | 52 | import io.grpc.NameResolverProvider; |
Kun Zhang | a9bd947 | 2017-02-21 17:11:03 -0800 | [diff] [blame] | 53 | import io.grpc.PickFirstBalancerFactory; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 54 | import java.net.SocketAddress; |
| 55 | import java.net.URI; |
Kun Zhang | 631a9d5 | 2016-06-02 16:47:36 -0700 | [diff] [blame] | 56 | import java.net.URISyntaxException; |
Eric Anderson | 0df3d5e | 2015-06-25 18:00:00 -0700 | [diff] [blame] | 57 | import java.util.ArrayList; |
| 58 | import java.util.Arrays; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 59 | import java.util.Collections; |
Eric Anderson | 0df3d5e | 2015-06-25 18:00:00 -0700 | [diff] [blame] | 60 | import java.util.List; |
Louis Ryan | 6a782a0 | 2015-09-03 14:27:48 -0700 | [diff] [blame] | 61 | import java.util.concurrent.Executor; |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 62 | import java.util.concurrent.TimeUnit; |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 63 | import javax.annotation.Nullable; |
| 64 | |
| 65 | /** |
| 66 | * The base class for channel builders. |
nathanmittler | 0304b3d | 2014-10-24 13:39:13 -0700 | [diff] [blame] | 67 | * |
nmittler | b687bdc | 2015-08-31 16:13:39 -0700 | [diff] [blame] | 68 | * @param <T> The concrete type of this builder. |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 69 | */ |
nmittler | b687bdc | 2015-08-31 16:13:39 -0700 | [diff] [blame] | 70 | public abstract class AbstractManagedChannelImplBuilder |
| 71 | <T extends AbstractManagedChannelImplBuilder<T>> extends ManagedChannelBuilder<T> { |
Kun Zhang | 1624715 | 2015-12-08 17:52:05 -0800 | [diff] [blame] | 72 | private static final String DIRECT_ADDRESS_SCHEME = "directaddress"; |
Eric Anderson | aeeebb7 | 2014-12-19 16:41:03 -0800 | [diff] [blame] | 73 | |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 74 | /** |
| 75 | * An idle timeout larger than this would disable idle mode. |
| 76 | */ |
| 77 | @VisibleForTesting |
| 78 | static final long IDLE_MODE_MAX_TIMEOUT_DAYS = 30; |
| 79 | |
| 80 | /** |
Kun Zhang | d74091f | 2016-09-16 22:34:27 -0700 | [diff] [blame] | 81 | * The default idle timeout. |
| 82 | */ |
| 83 | @VisibleForTesting |
| 84 | static final long IDLE_MODE_DEFAULT_TIMEOUT_MILLIS = TimeUnit.MINUTES.toMillis(30); |
| 85 | |
| 86 | /** |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 87 | * An idle timeout smaller than this would be capped to it. |
| 88 | */ |
| 89 | @VisibleForTesting |
| 90 | static final long IDLE_MODE_MIN_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(1); |
| 91 | |
ejona | 7235a39 | 2015-01-13 13:38:54 -0800 | [diff] [blame] | 92 | @Nullable |
Louis Ryan | 6a782a0 | 2015-09-03 14:27:48 -0700 | [diff] [blame] | 93 | private Executor executor; |
Kun Zhang | d17a7b5 | 2017-01-10 15:30:12 -0800 | [diff] [blame] | 94 | |
Eric Anderson | 0df3d5e | 2015-06-25 18:00:00 -0700 | [diff] [blame] | 95 | private final List<ClientInterceptor> interceptors = new ArrayList<ClientInterceptor>(); |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 96 | |
Kun Zhang | efac679 | 2015-10-22 14:59:44 -0700 | [diff] [blame] | 97 | private final String target; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 98 | |
| 99 | @Nullable |
| 100 | private final SocketAddress directServerAddress; |
| 101 | |
nmittler | 8c1d38a | 2015-06-01 08:31:00 -0700 | [diff] [blame] | 102 | @Nullable |
| 103 | private String userAgent; |
| 104 | |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 105 | @Nullable |
| 106 | private String authorityOverride; |
| 107 | |
| 108 | @Nullable |
| 109 | private NameResolver.Factory nameResolverFactory; |
| 110 | |
Kun Zhang | a9bd947 | 2017-02-21 17:11:03 -0800 | [diff] [blame] | 111 | private LoadBalancer.Factory loadBalancerFactory; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 112 | |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 113 | @Nullable |
| 114 | private DecompressorRegistry decompressorRegistry; |
| 115 | |
| 116 | @Nullable |
| 117 | private CompressorRegistry compressorRegistry; |
| 118 | |
Kun Zhang | d74091f | 2016-09-16 22:34:27 -0700 | [diff] [blame] | 119 | private long idleTimeoutMillis = IDLE_MODE_DEFAULT_TIMEOUT_MILLIS; |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 120 | |
Carl Mastrangelo | fdd062c | 2016-10-26 16:55:41 -0700 | [diff] [blame] | 121 | private int maxInboundMessageSize = GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE; |
| 122 | |
Kun Zhang | be74e97 | 2017-04-26 10:50:55 -0700 | [diff] [blame^] | 123 | private boolean enableStatsTagPropagation; |
| 124 | private boolean enableTracing; |
| 125 | |
Carl Mastrangelo | fdd062c | 2016-10-26 16:55:41 -0700 | [diff] [blame] | 126 | // Can be overriden by subclasses. |
| 127 | @Override |
| 128 | public T maxInboundMessageSize(int max) { |
| 129 | checkArgument(max >= 0, "negative max"); |
| 130 | maxInboundMessageSize = max; |
| 131 | return thisT(); |
| 132 | } |
| 133 | |
| 134 | protected final int maxInboundMessageSize() { |
| 135 | return maxInboundMessageSize; |
| 136 | } |
| 137 | |
Kun Zhang | 132f7a9 | 2016-10-06 17:15:24 -0700 | [diff] [blame] | 138 | @Nullable |
Kun Zhang | cce8eac | 2017-01-05 10:48:13 -0800 | [diff] [blame] | 139 | private StatsContextFactory statsFactory; |
Kun Zhang | 132f7a9 | 2016-10-06 17:15:24 -0700 | [diff] [blame] | 140 | |
Kun Zhang | efac679 | 2015-10-22 14:59:44 -0700 | [diff] [blame] | 141 | protected AbstractManagedChannelImplBuilder(String target) { |
Carl Mastrangelo | 1285477 | 2016-08-12 14:52:00 -0700 | [diff] [blame] | 142 | this.target = Preconditions.checkNotNull(target, "target"); |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 143 | this.directServerAddress = null; |
| 144 | } |
| 145 | |
Kun Zhang | 631a9d5 | 2016-06-02 16:47:36 -0700 | [diff] [blame] | 146 | /** |
| 147 | * Returns a target string for the SocketAddress. It is only used as a placeholder, because |
| 148 | * DirectAddressNameResolverFactory will not actually try to use it. However, it must be a valid |
| 149 | * URI. |
| 150 | */ |
| 151 | @VisibleForTesting |
| 152 | static String makeTargetStringForDirectAddress(SocketAddress address) { |
| 153 | try { |
| 154 | return new URI(DIRECT_ADDRESS_SCHEME, "", "/" + address, null).toString(); |
| 155 | } catch (URISyntaxException e) { |
| 156 | // It should not happen. |
| 157 | throw new RuntimeException(e); |
| 158 | } |
| 159 | } |
| 160 | |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 161 | protected AbstractManagedChannelImplBuilder(SocketAddress directServerAddress, String authority) { |
Kun Zhang | 631a9d5 | 2016-06-02 16:47:36 -0700 | [diff] [blame] | 162 | this.target = makeTargetStringForDirectAddress(directServerAddress); |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 163 | this.directServerAddress = directServerAddress; |
| 164 | this.nameResolverFactory = new DirectAddressNameResolverFactory(directServerAddress, authority); |
| 165 | } |
| 166 | |
Eric Anderson | 6122daf | 2015-09-03 12:14:30 -0700 | [diff] [blame] | 167 | @Override |
buchgr | 602473d | 2015-11-11 12:53:08 +0100 | [diff] [blame] | 168 | public final T directExecutor() { |
| 169 | return executor(MoreExecutors.directExecutor()); |
| 170 | } |
| 171 | |
| 172 | @Override |
Louis Ryan | 6a782a0 | 2015-09-03 14:27:48 -0700 | [diff] [blame] | 173 | public final T executor(Executor executor) { |
nmittler | 777e928 | 2015-08-19 10:01:52 -0700 | [diff] [blame] | 174 | this.executor = executor; |
Eric Anderson | 0df3d5e | 2015-06-25 18:00:00 -0700 | [diff] [blame] | 175 | return thisT(); |
| 176 | } |
| 177 | |
Eric Anderson | 6122daf | 2015-09-03 12:14:30 -0700 | [diff] [blame] | 178 | @Override |
nmittler | b687bdc | 2015-08-31 16:13:39 -0700 | [diff] [blame] | 179 | public final T intercept(List<ClientInterceptor> interceptors) { |
Eric Anderson | 0df3d5e | 2015-06-25 18:00:00 -0700 | [diff] [blame] | 180 | this.interceptors.addAll(interceptors); |
| 181 | return thisT(); |
| 182 | } |
| 183 | |
Eric Anderson | 6122daf | 2015-09-03 12:14:30 -0700 | [diff] [blame] | 184 | @Override |
nmittler | b687bdc | 2015-08-31 16:13:39 -0700 | [diff] [blame] | 185 | public final T intercept(ClientInterceptor... interceptors) { |
Eric Anderson | 0df3d5e | 2015-06-25 18:00:00 -0700 | [diff] [blame] | 186 | return intercept(Arrays.asList(interceptors)); |
| 187 | } |
| 188 | |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 189 | @Override |
| 190 | public final T nameResolverFactory(NameResolver.Factory resolverFactory) { |
| 191 | Preconditions.checkState(directServerAddress == null, |
| 192 | "directServerAddress is set (%s), which forbids the use of NameResolverFactory", |
| 193 | directServerAddress); |
| 194 | this.nameResolverFactory = resolverFactory; |
| 195 | return thisT(); |
| 196 | } |
| 197 | |
| 198 | @Override |
Kun Zhang | a9bd947 | 2017-02-21 17:11:03 -0800 | [diff] [blame] | 199 | public final T loadBalancerFactory(LoadBalancer.Factory loadBalancerFactory) { |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 200 | Preconditions.checkState(directServerAddress == null, |
Kun Zhang | a9bd947 | 2017-02-21 17:11:03 -0800 | [diff] [blame] | 201 | "directServerAddress is set (%s), which forbids the use of LoadBalancer.Factory", |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 202 | directServerAddress); |
| 203 | this.loadBalancerFactory = loadBalancerFactory; |
Kun Zhang | d17a7b5 | 2017-01-10 15:30:12 -0800 | [diff] [blame] | 204 | return thisT(); |
| 205 | } |
| 206 | |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 207 | @Override |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 208 | public final T decompressorRegistry(DecompressorRegistry registry) { |
| 209 | this.decompressorRegistry = registry; |
| 210 | return thisT(); |
| 211 | } |
| 212 | |
| 213 | @Override |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 214 | public final T compressorRegistry(CompressorRegistry registry) { |
| 215 | this.compressorRegistry = registry; |
| 216 | return thisT(); |
| 217 | } |
| 218 | |
Eric Anderson | 6122daf | 2015-09-03 12:14:30 -0700 | [diff] [blame] | 219 | @Override |
Carl Mastrangelo | 1cc76d8 | 2016-05-24 16:29:26 -0700 | [diff] [blame] | 220 | public final T userAgent(@Nullable String userAgent) { |
nmittler | 8c1d38a | 2015-06-01 08:31:00 -0700 | [diff] [blame] | 221 | this.userAgent = userAgent; |
nmittler | b687bdc | 2015-08-31 16:13:39 -0700 | [diff] [blame] | 222 | return thisT(); |
nmittler | 8c1d38a | 2015-06-01 08:31:00 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Eric Anderson | 6122daf | 2015-09-03 12:14:30 -0700 | [diff] [blame] | 225 | @Override |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 226 | public final T overrideAuthority(String authority) { |
| 227 | this.authorityOverride = checkAuthority(authority); |
| 228 | return thisT(); |
| 229 | } |
| 230 | |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 231 | @Override |
| 232 | public final T idleTimeout(long value, TimeUnit unit) { |
| 233 | checkArgument(value > 0, "idle timeout is %s, but must be positive", value); |
| 234 | // We convert to the largest unit to avoid overflow |
| 235 | if (unit.toDays(value) >= IDLE_MODE_MAX_TIMEOUT_DAYS) { |
| 236 | // This disables idle mode |
Kun Zhang | a9bd947 | 2017-02-21 17:11:03 -0800 | [diff] [blame] | 237 | this.idleTimeoutMillis = ManagedChannelImpl.IDLE_TIMEOUT_MILLIS_DISABLE; |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 238 | } else { |
| 239 | this.idleTimeoutMillis = Math.max(unit.toMillis(value), IDLE_MODE_MIN_TIMEOUT_MILLIS); |
| 240 | } |
| 241 | return thisT(); |
| 242 | } |
| 243 | |
Kun Zhang | 132f7a9 | 2016-10-06 17:15:24 -0700 | [diff] [blame] | 244 | /** |
Kun Zhang | 737cd16 | 2017-01-20 17:20:44 -0800 | [diff] [blame] | 245 | * Override the default stats implementation. |
Kun Zhang | 132f7a9 | 2016-10-06 17:15:24 -0700 | [diff] [blame] | 246 | */ |
| 247 | @VisibleForTesting |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 248 | protected final T statsContextFactory(StatsContextFactory statsFactory) { |
Kun Zhang | cce8eac | 2017-01-05 10:48:13 -0800 | [diff] [blame] | 249 | this.statsFactory = statsFactory; |
Kun Zhang | 132f7a9 | 2016-10-06 17:15:24 -0700 | [diff] [blame] | 250 | return thisT(); |
| 251 | } |
| 252 | |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 253 | /** |
| 254 | * Indicates whether this transport will record stats with {@link ClientStreamTracer}. |
| 255 | * |
| 256 | * <p>By default it returns {@code true}. If the transport doesn't record stats, it may override |
| 257 | * this method to return {@code false} so that the builder won't install the Census interceptor. |
| 258 | * |
| 259 | * <p>If it returns true when it shouldn't be, Census will receive incomplete stats. |
| 260 | */ |
| 261 | protected boolean recordsStats() { |
| 262 | return true; |
| 263 | } |
| 264 | |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 265 | @VisibleForTesting |
| 266 | final long getIdleTimeoutMillis() { |
| 267 | return idleTimeoutMillis; |
| 268 | } |
| 269 | |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 270 | /** |
| 271 | * Verifies the authority is valid. This method exists as an escape hatch for putting in an |
| 272 | * authority that is valid, but would fail the default validation provided by this |
| 273 | * implementation. |
| 274 | */ |
| 275 | protected String checkAuthority(String authority) { |
| 276 | return GrpcUtil.checkAuthority(authority); |
| 277 | } |
| 278 | |
Kun Zhang | be74e97 | 2017-04-26 10:50:55 -0700 | [diff] [blame^] | 279 | /** |
| 280 | * Set it to true to propagate the stats tags on the wire. This will be deleted assuming always |
| 281 | * enabled once the instrumentation-java wire format is stabilized. |
| 282 | */ |
| 283 | @Deprecated |
| 284 | public void setEnableStatsTagPropagation(boolean enabled) { |
| 285 | this.enableStatsTagPropagation = enabled; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Set it to true to record traces and propagate tracing information on the wire. This will be |
| 290 | * deleted assuming always enabled once the instrumentation-java wire format is stabilized. |
| 291 | */ |
| 292 | @Deprecated |
| 293 | public void setEnableTracing(boolean enabled) { |
| 294 | this.enableTracing = enabled; |
| 295 | } |
| 296 | |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 297 | @Override |
Kun Zhang | d17a7b5 | 2017-01-10 15:30:12 -0800 | [diff] [blame] | 298 | public ManagedChannel build() { |
Sky Ao | 1d8aefa | 2016-04-18 23:43:30 +0800 | [diff] [blame] | 299 | ClientTransportFactory transportFactory = buildTransportFactory(); |
| 300 | if (authorityOverride != null) { |
| 301 | transportFactory = new AuthorityOverridingTransportFactory( |
| 302 | transportFactory, authorityOverride); |
| 303 | } |
Eric Anderson | bfcb04c | 2016-07-11 10:02:23 -0700 | [diff] [blame] | 304 | NameResolver.Factory nameResolverFactory = this.nameResolverFactory; |
| 305 | if (nameResolverFactory == null) { |
| 306 | // Avoid loading the provider unless necessary, as a way to workaround a possibly-costly |
| 307 | // and poorly optimized getResource() call on Android. If any other piece of code calls |
| 308 | // getResource(), then this shouldn't be a problem unless called on the UI thread. |
| 309 | nameResolverFactory = NameResolverProvider.asFactory(); |
| 310 | } |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 311 | |
Kun Zhang | 49bde54 | 2017-04-25 13:53:29 -0700 | [diff] [blame] | 312 | List<ClientInterceptor> effectiveInterceptors = |
| 313 | new ArrayList<ClientInterceptor>(this.interceptors); |
Kun Zhang | be74e97 | 2017-04-26 10:50:55 -0700 | [diff] [blame^] | 314 | if (recordsStats()) { |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 315 | StatsContextFactory statsCtxFactory = |
| 316 | this.statsFactory != null ? this.statsFactory : Stats.getStatsContextFactory(); |
| 317 | if (statsCtxFactory != null) { |
Kun Zhang | 49bde54 | 2017-04-25 13:53:29 -0700 | [diff] [blame] | 318 | CensusStatsModule censusStats = |
Kun Zhang | be74e97 | 2017-04-26 10:50:55 -0700 | [diff] [blame^] | 319 | new CensusStatsModule( |
| 320 | statsCtxFactory, GrpcUtil.STOPWATCH_SUPPLIER, enableStatsTagPropagation); |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 321 | // First interceptor runs last (see ClientInterceptors.intercept()), so that no |
| 322 | // other interceptor can override the tracer factory we set in CallOptions. |
Kun Zhang | 49bde54 | 2017-04-25 13:53:29 -0700 | [diff] [blame] | 323 | effectiveInterceptors.add(0, censusStats.getClientInterceptor()); |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
Kun Zhang | be74e97 | 2017-04-26 10:50:55 -0700 | [diff] [blame^] | 326 | if (enableTracing) { |
Kun Zhang | 49bde54 | 2017-04-25 13:53:29 -0700 | [diff] [blame] | 327 | CensusTracingModule censusTracing = |
| 328 | new CensusTracingModule(Tracing.getTracer(), Tracing.getBinaryPropagationHandler()); |
| 329 | effectiveInterceptors.add(0, censusTracing.getClientInterceptor()); |
| 330 | } |
| 331 | |
Kun Zhang | a9bd947 | 2017-02-21 17:11:03 -0800 | [diff] [blame] | 332 | return new ManagedChannelImpl( |
Kun Zhang | a6b1d89 | 2017-02-21 14:26:23 -0800 | [diff] [blame] | 333 | target, |
| 334 | // TODO(carl-mastrangelo): Allow clients to pass this in |
| 335 | new ExponentialBackoffPolicy.Provider(), |
| 336 | nameResolverFactory, |
| 337 | getNameResolverParams(), |
Kun Zhang | a9bd947 | 2017-02-21 17:11:03 -0800 | [diff] [blame] | 338 | firstNonNull(loadBalancerFactory, PickFirstBalancerFactory.getInstance()), |
Kun Zhang | a6b1d89 | 2017-02-21 14:26:23 -0800 | [diff] [blame] | 339 | transportFactory, |
| 340 | firstNonNull(decompressorRegistry, DecompressorRegistry.getDefaultInstance()), |
| 341 | firstNonNull(compressorRegistry, CompressorRegistry.getDefaultInstance()), |
| 342 | SharedResourcePool.forResource(GrpcUtil.TIMER_SERVICE), |
| 343 | getExecutorPool(executor), |
| 344 | SharedResourcePool.forResource(GrpcUtil.SHARED_CHANNEL_EXECUTOR), |
Kun Zhang | a9bd947 | 2017-02-21 17:11:03 -0800 | [diff] [blame] | 345 | GrpcUtil.STOPWATCH_SUPPLIER, |
| 346 | idleTimeoutMillis, |
| 347 | userAgent, |
Kun Zhang | 49bde54 | 2017-04-25 13:53:29 -0700 | [diff] [blame] | 348 | effectiveInterceptors); |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /** |
Kun Zhang | edd57c9 | 2015-10-27 12:47:29 -0700 | [diff] [blame] | 352 | * Subclasses should override this method to provide the {@link ClientTransportFactory} |
| 353 | * appropriate for this channel. This method is meant for Transport implementors and should not |
| 354 | * be used by normal users. |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 355 | */ |
nmittler | 777e928 | 2015-08-19 10:01:52 -0700 | [diff] [blame] | 356 | protected abstract ClientTransportFactory buildTransportFactory(); |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 357 | |
Kun Zhang | edd57c9 | 2015-10-27 12:47:29 -0700 | [diff] [blame] | 358 | /** |
| 359 | * Subclasses can override this method to provide additional parameters to {@link |
| 360 | * NameResolver.Factory#newNameResolver}. The default implementation returns {@link |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 361 | * Attributes#EMPTY}. |
Kun Zhang | edd57c9 | 2015-10-27 12:47:29 -0700 | [diff] [blame] | 362 | */ |
| 363 | protected Attributes getNameResolverParams() { |
| 364 | return Attributes.EMPTY; |
| 365 | } |
| 366 | |
Kun Zhang | d17a7b5 | 2017-01-10 15:30:12 -0800 | [diff] [blame] | 367 | private static ObjectPool<? extends Executor> getExecutorPool(final @Nullable Executor executor) { |
| 368 | if (executor != null) { |
| 369 | return new ObjectPool<Executor>() { |
| 370 | @Override |
| 371 | public Executor getObject() { |
| 372 | return executor; |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public Executor returnObject(Object returned) { |
| 377 | return null; |
| 378 | } |
| 379 | }; |
| 380 | } else { |
| 381 | return SharedResourcePool.forResource(GrpcUtil.SHARED_CHANNEL_EXECUTOR); |
| 382 | } |
| 383 | } |
| 384 | |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 385 | private static class AuthorityOverridingTransportFactory implements ClientTransportFactory { |
| 386 | final ClientTransportFactory factory; |
Sky Ao | 1d8aefa | 2016-04-18 23:43:30 +0800 | [diff] [blame] | 387 | final String authorityOverride; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 388 | |
| 389 | AuthorityOverridingTransportFactory( |
Sky Ao | 1d8aefa | 2016-04-18 23:43:30 +0800 | [diff] [blame] | 390 | ClientTransportFactory factory, String authorityOverride) { |
| 391 | this.factory = Preconditions.checkNotNull(factory, "factory should not be null"); |
| 392 | this.authorityOverride = Preconditions.checkNotNull( |
| 393 | authorityOverride, "authorityOverride should not be null"); |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | @Override |
Kun Zhang | 432cec7 | 2016-05-29 14:43:10 -0700 | [diff] [blame] | 397 | public ConnectionClientTransport newClientTransport(SocketAddress serverAddress, |
Carl Mastrangelo | 1cc76d8 | 2016-05-24 16:29:26 -0700 | [diff] [blame] | 398 | String authority, @Nullable String userAgent) { |
| 399 | return factory.newClientTransport(serverAddress, authorityOverride, userAgent); |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | @Override |
buchgr | 3c68c05 | 2016-03-17 00:14:43 +0100 | [diff] [blame] | 403 | public void close() { |
| 404 | factory.close(); |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
| 408 | private static class DirectAddressNameResolverFactory extends NameResolver.Factory { |
| 409 | final SocketAddress address; |
| 410 | final String authority; |
| 411 | |
| 412 | DirectAddressNameResolverFactory(SocketAddress address, String authority) { |
| 413 | this.address = address; |
| 414 | this.authority = authority; |
| 415 | } |
| 416 | |
| 417 | @Override |
Kun Zhang | edd57c9 | 2015-10-27 12:47:29 -0700 | [diff] [blame] | 418 | public NameResolver newNameResolver(URI notUsedUri, Attributes params) { |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 419 | return new NameResolver() { |
| 420 | @Override |
| 421 | public String getServiceAuthority() { |
| 422 | return authority; |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public void start(final Listener listener) { |
Kun Zhang | 418d52d | 2017-03-22 18:29:31 -0700 | [diff] [blame] | 427 | listener.onAddresses( |
| 428 | Collections.singletonList(new EquivalentAddressGroup(address)), |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 429 | Attributes.EMPTY); |
| 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public void shutdown() {} |
| 434 | }; |
| 435 | } |
Kun Zhang | 1624715 | 2015-12-08 17:52:05 -0800 | [diff] [blame] | 436 | |
| 437 | @Override |
| 438 | public String getDefaultScheme() { |
| 439 | return DIRECT_ADDRESS_SCHEME; |
| 440 | } |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 441 | } |
Carl Mastrangelo | 8493498 | 2017-01-11 12:46:47 -0800 | [diff] [blame] | 442 | |
| 443 | /** |
| 444 | * Returns the correctly typed version of the builder. |
| 445 | */ |
| 446 | private T thisT() { |
| 447 | @SuppressWarnings("unchecked") |
| 448 | T thisT = (T) this; |
| 449 | return thisT; |
| 450 | } |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 451 | } |