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; |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 42 | |
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 | 132f7a9 | 2016-10-06 17:15:24 -0700 | [diff] [blame] | 47 | import io.grpc.Internal; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 48 | import io.grpc.LoadBalancer; |
| 49 | import io.grpc.ManagedChannelBuilder; |
| 50 | import io.grpc.NameResolver; |
Eric Anderson | 56a2938 | 2016-06-11 11:55:49 -0700 | [diff] [blame] | 51 | import io.grpc.NameResolverProvider; |
Lukasz Strzalkowski | 577164c | 2016-08-23 00:40:09 +0200 | [diff] [blame] | 52 | import io.grpc.PickFirstBalancerFactory; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 53 | import io.grpc.ResolvedServerInfo; |
Lukasz Strzalkowski | 23652c5 | 2016-08-19 16:56:56 +0200 | [diff] [blame] | 54 | import io.grpc.ResolvedServerInfoGroup; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 55 | |
| 56 | import java.net.SocketAddress; |
| 57 | import java.net.URI; |
Kun Zhang | 631a9d5 | 2016-06-02 16:47:36 -0700 | [diff] [blame] | 58 | import java.net.URISyntaxException; |
Eric Anderson | 0df3d5e | 2015-06-25 18:00:00 -0700 | [diff] [blame] | 59 | import java.util.ArrayList; |
| 60 | import java.util.Arrays; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 61 | import java.util.Collections; |
Eric Anderson | 0df3d5e | 2015-06-25 18:00:00 -0700 | [diff] [blame] | 62 | import java.util.List; |
Louis Ryan | 6a782a0 | 2015-09-03 14:27:48 -0700 | [diff] [blame] | 63 | import java.util.concurrent.Executor; |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 64 | |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 65 | import java.util.concurrent.TimeUnit; |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 66 | import javax.annotation.Nullable; |
| 67 | |
| 68 | /** |
| 69 | * The base class for channel builders. |
nathanmittler | 0304b3d | 2014-10-24 13:39:13 -0700 | [diff] [blame] | 70 | * |
nmittler | b687bdc | 2015-08-31 16:13:39 -0700 | [diff] [blame] | 71 | * @param <T> The concrete type of this builder. |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 72 | */ |
nmittler | b687bdc | 2015-08-31 16:13:39 -0700 | [diff] [blame] | 73 | public abstract class AbstractManagedChannelImplBuilder |
| 74 | <T extends AbstractManagedChannelImplBuilder<T>> extends ManagedChannelBuilder<T> { |
Kun Zhang | 1624715 | 2015-12-08 17:52:05 -0800 | [diff] [blame] | 75 | private static final String DIRECT_ADDRESS_SCHEME = "directaddress"; |
Eric Anderson | aeeebb7 | 2014-12-19 16:41:03 -0800 | [diff] [blame] | 76 | |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 77 | /** |
| 78 | * An idle timeout larger than this would disable idle mode. |
| 79 | */ |
| 80 | @VisibleForTesting |
| 81 | static final long IDLE_MODE_MAX_TIMEOUT_DAYS = 30; |
| 82 | |
| 83 | /** |
Kun Zhang | d74091f | 2016-09-16 22:34:27 -0700 | [diff] [blame] | 84 | * The default idle timeout. |
| 85 | */ |
| 86 | @VisibleForTesting |
| 87 | static final long IDLE_MODE_DEFAULT_TIMEOUT_MILLIS = TimeUnit.MINUTES.toMillis(30); |
| 88 | |
| 89 | /** |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 90 | * An idle timeout smaller than this would be capped to it. |
| 91 | */ |
| 92 | @VisibleForTesting |
| 93 | static final long IDLE_MODE_MIN_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(1); |
| 94 | |
ejona | 7235a39 | 2015-01-13 13:38:54 -0800 | [diff] [blame] | 95 | @Nullable |
Louis Ryan | 6a782a0 | 2015-09-03 14:27:48 -0700 | [diff] [blame] | 96 | private Executor executor; |
Eric Anderson | 0df3d5e | 2015-06-25 18:00:00 -0700 | [diff] [blame] | 97 | private final List<ClientInterceptor> interceptors = new ArrayList<ClientInterceptor>(); |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 98 | |
Kun Zhang | efac679 | 2015-10-22 14:59:44 -0700 | [diff] [blame] | 99 | private final String target; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 100 | |
| 101 | @Nullable |
| 102 | private final SocketAddress directServerAddress; |
| 103 | |
nmittler | 8c1d38a | 2015-06-01 08:31:00 -0700 | [diff] [blame] | 104 | @Nullable |
| 105 | private String userAgent; |
| 106 | |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 107 | @Nullable |
| 108 | private String authorityOverride; |
| 109 | |
| 110 | @Nullable |
| 111 | private NameResolver.Factory nameResolverFactory; |
| 112 | |
| 113 | @Nullable |
| 114 | private LoadBalancer.Factory loadBalancerFactory; |
| 115 | |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 116 | @Nullable |
| 117 | private DecompressorRegistry decompressorRegistry; |
| 118 | |
| 119 | @Nullable |
| 120 | private CompressorRegistry compressorRegistry; |
| 121 | |
Kun Zhang | d74091f | 2016-09-16 22:34:27 -0700 | [diff] [blame] | 122 | private long idleTimeoutMillis = IDLE_MODE_DEFAULT_TIMEOUT_MILLIS; |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 123 | |
Carl Mastrangelo | fdd062c | 2016-10-26 16:55:41 -0700 | [diff] [blame] | 124 | private int maxInboundMessageSize = GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE; |
| 125 | |
| 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 |
| 199 | public final T loadBalancerFactory(LoadBalancer.Factory loadBalancerFactory) { |
| 200 | Preconditions.checkState(directServerAddress == null, |
| 201 | "directServerAddress is set (%s), which forbids the use of LoadBalancerFactory", |
| 202 | directServerAddress); |
| 203 | this.loadBalancerFactory = loadBalancerFactory; |
| 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 |
| 237 | this.idleTimeoutMillis = ManagedChannelImpl.IDLE_TIMEOUT_MILLIS_DISABLE; |
| 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 | cce8eac | 2017-01-05 10:48:13 -0800 | [diff] [blame^] | 245 | * Override the default stats implementation. This is meant to be used in tests. |
Kun Zhang | 132f7a9 | 2016-10-06 17:15:24 -0700 | [diff] [blame] | 246 | */ |
| 247 | @VisibleForTesting |
| 248 | @Internal |
Kun Zhang | cce8eac | 2017-01-05 10:48:13 -0800 | [diff] [blame^] | 249 | public T statsContextFactory(StatsContextFactory statsFactory) { |
| 250 | this.statsFactory = statsFactory; |
Kun Zhang | 132f7a9 | 2016-10-06 17:15:24 -0700 | [diff] [blame] | 251 | return thisT(); |
| 252 | } |
| 253 | |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 254 | @VisibleForTesting |
| 255 | final long getIdleTimeoutMillis() { |
| 256 | return idleTimeoutMillis; |
| 257 | } |
| 258 | |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 259 | /** |
| 260 | * Verifies the authority is valid. This method exists as an escape hatch for putting in an |
| 261 | * authority that is valid, but would fail the default validation provided by this |
| 262 | * implementation. |
| 263 | */ |
| 264 | protected String checkAuthority(String authority) { |
| 265 | return GrpcUtil.checkAuthority(authority); |
| 266 | } |
| 267 | |
| 268 | @Override |
nmittler | b687bdc | 2015-08-31 16:13:39 -0700 | [diff] [blame] | 269 | public ManagedChannelImpl build() { |
Sky Ao | 1d8aefa | 2016-04-18 23:43:30 +0800 | [diff] [blame] | 270 | ClientTransportFactory transportFactory = buildTransportFactory(); |
| 271 | if (authorityOverride != null) { |
| 272 | transportFactory = new AuthorityOverridingTransportFactory( |
| 273 | transportFactory, authorityOverride); |
| 274 | } |
Eric Anderson | bfcb04c | 2016-07-11 10:02:23 -0700 | [diff] [blame] | 275 | NameResolver.Factory nameResolverFactory = this.nameResolverFactory; |
| 276 | if (nameResolverFactory == null) { |
| 277 | // Avoid loading the provider unless necessary, as a way to workaround a possibly-costly |
| 278 | // and poorly optimized getResource() call on Android. If any other piece of code calls |
| 279 | // getResource(), then this shouldn't be a problem unless called on the UI thread. |
| 280 | nameResolverFactory = NameResolverProvider.asFactory(); |
| 281 | } |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 282 | return new ManagedChannelImpl( |
| 283 | target, |
Eric Anderson | a40b686 | 2016-03-25 10:23:58 -0700 | [diff] [blame] | 284 | // TODO(carl-mastrangelo): Allow clients to pass this in |
| 285 | new ExponentialBackoffPolicy.Provider(), |
Eric Anderson | bfcb04c | 2016-07-11 10:02:23 -0700 | [diff] [blame] | 286 | nameResolverFactory, |
Kun Zhang | edd57c9 | 2015-10-27 12:47:29 -0700 | [diff] [blame] | 287 | getNameResolverParams(), |
Lukasz Strzalkowski | 577164c | 2016-08-23 00:40:09 +0200 | [diff] [blame] | 288 | firstNonNull(loadBalancerFactory, PickFirstBalancerFactory.getInstance()), |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 289 | transportFactory, |
| 290 | firstNonNull(decompressorRegistry, DecompressorRegistry.getDefaultInstance()), |
| 291 | firstNonNull(compressorRegistry, CompressorRegistry.getDefaultInstance()), |
Kun Zhang | 527fd67 | 2016-06-17 09:47:41 -0700 | [diff] [blame] | 292 | GrpcUtil.TIMER_SERVICE, GrpcUtil.STOPWATCH_SUPPLIER, idleTimeoutMillis, |
Kun Zhang | 132f7a9 | 2016-10-06 17:15:24 -0700 | [diff] [blame] | 293 | executor, userAgent, interceptors, |
Kun Zhang | cce8eac | 2017-01-05 10:48:13 -0800 | [diff] [blame^] | 294 | firstNonNull(statsFactory, |
| 295 | firstNonNull(Stats.getStatsContextFactory(), NoopStatsContextFactory.INSTANCE))); |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /** |
Kun Zhang | edd57c9 | 2015-10-27 12:47:29 -0700 | [diff] [blame] | 299 | * Subclasses should override this method to provide the {@link ClientTransportFactory} |
| 300 | * appropriate for this channel. This method is meant for Transport implementors and should not |
| 301 | * be used by normal users. |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 302 | */ |
nmittler | 777e928 | 2015-08-19 10:01:52 -0700 | [diff] [blame] | 303 | protected abstract ClientTransportFactory buildTransportFactory(); |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 304 | |
Kun Zhang | edd57c9 | 2015-10-27 12:47:29 -0700 | [diff] [blame] | 305 | /** |
| 306 | * Subclasses can override this method to provide additional parameters to {@link |
| 307 | * NameResolver.Factory#newNameResolver}. The default implementation returns {@link |
Carl Mastrangelo | 82a79d8 | 2015-12-07 14:40:11 -0800 | [diff] [blame] | 308 | * Attributes#EMPTY}. |
Kun Zhang | edd57c9 | 2015-10-27 12:47:29 -0700 | [diff] [blame] | 309 | */ |
| 310 | protected Attributes getNameResolverParams() { |
| 311 | return Attributes.EMPTY; |
| 312 | } |
| 313 | |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 314 | private static class AuthorityOverridingTransportFactory implements ClientTransportFactory { |
| 315 | final ClientTransportFactory factory; |
Sky Ao | 1d8aefa | 2016-04-18 23:43:30 +0800 | [diff] [blame] | 316 | final String authorityOverride; |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 317 | |
| 318 | AuthorityOverridingTransportFactory( |
Sky Ao | 1d8aefa | 2016-04-18 23:43:30 +0800 | [diff] [blame] | 319 | ClientTransportFactory factory, String authorityOverride) { |
| 320 | this.factory = Preconditions.checkNotNull(factory, "factory should not be null"); |
| 321 | this.authorityOverride = Preconditions.checkNotNull( |
| 322 | authorityOverride, "authorityOverride should not be null"); |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | @Override |
Kun Zhang | 432cec7 | 2016-05-29 14:43:10 -0700 | [diff] [blame] | 326 | public ConnectionClientTransport newClientTransport(SocketAddress serverAddress, |
Carl Mastrangelo | 1cc76d8 | 2016-05-24 16:29:26 -0700 | [diff] [blame] | 327 | String authority, @Nullable String userAgent) { |
| 328 | return factory.newClientTransport(serverAddress, authorityOverride, userAgent); |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | @Override |
buchgr | 3c68c05 | 2016-03-17 00:14:43 +0100 | [diff] [blame] | 332 | public void close() { |
| 333 | factory.close(); |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | private static class DirectAddressNameResolverFactory extends NameResolver.Factory { |
| 338 | final SocketAddress address; |
| 339 | final String authority; |
| 340 | |
| 341 | DirectAddressNameResolverFactory(SocketAddress address, String authority) { |
| 342 | this.address = address; |
| 343 | this.authority = authority; |
| 344 | } |
| 345 | |
| 346 | @Override |
Kun Zhang | edd57c9 | 2015-10-27 12:47:29 -0700 | [diff] [blame] | 347 | public NameResolver newNameResolver(URI notUsedUri, Attributes params) { |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 348 | return new NameResolver() { |
| 349 | @Override |
| 350 | public String getServiceAuthority() { |
| 351 | return authority; |
| 352 | } |
| 353 | |
| 354 | @Override |
| 355 | public void start(final Listener listener) { |
Lukasz Strzalkowski | 23652c5 | 2016-08-19 16:56:56 +0200 | [diff] [blame] | 356 | listener.onUpdate(Collections.singletonList( |
| 357 | ResolvedServerInfoGroup.builder().add(new ResolvedServerInfo(address)).build()), |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 358 | Attributes.EMPTY); |
| 359 | } |
| 360 | |
| 361 | @Override |
| 362 | public void shutdown() {} |
| 363 | }; |
| 364 | } |
Kun Zhang | 1624715 | 2015-12-08 17:52:05 -0800 | [diff] [blame] | 365 | |
| 366 | @Override |
| 367 | public String getDefaultScheme() { |
| 368 | return DIRECT_ADDRESS_SCHEME; |
| 369 | } |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 370 | } |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 371 | } |