Skip to content

Commit

Permalink
core: allow LBv2 impls to be stripped out by ProGuard. (#2654)
Browse files Browse the repository at this point in the history
This should address the concern of increased method count as mentioned
in #2650.
  • Loading branch information
zhangkun83 authored Jan 25, 2017
1 parent f088b81 commit 0428556
Showing 1 changed file with 35 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,10 @@ public abstract class AbstractManagedChannelImplBuilder
@Nullable
private NameResolver.Factory nameResolverFactory;

@Nullable
private LoadBalancer.Factory loadBalancerFactory;

@Nullable
private LoadBalancer2.Factory loadBalancer2Factory;
private ChannelFactory channelFactory;

@Nullable
private DecompressorRegistry decompressorRegistry;
Expand Down Expand Up @@ -206,17 +205,38 @@ public final T loadBalancerFactory(LoadBalancer.Factory loadBalancerFactory) {
"directServerAddress is set (%s), which forbids the use of LoadBalancerFactory",
directServerAddress);
this.loadBalancerFactory = loadBalancerFactory;
this.channelFactory = null;
return thisT();
}

/**
* DO NOT CALL THIS, as its argument type will soon be renamed.
*/
public final T loadBalancerFactory(LoadBalancer2.Factory loadBalancerFactory) {
public final T loadBalancerFactory(final LoadBalancer2.Factory loadBalancerFactory) {
Preconditions.checkState(directServerAddress == null,
"directServerAddress is set (%s), which forbids the use of LoadBalancerFactory",
directServerAddress);
this.loadBalancer2Factory = loadBalancerFactory;
this.channelFactory = new ChannelFactory() {
@Override
public ManagedChannel create(ClientTransportFactory transportFactory) {
return new ManagedChannelImpl2(
target,
// TODO(carl-mastrangelo): Allow clients to pass this in
new ExponentialBackoffPolicy.Provider(),
nameResolverFactory,
getNameResolverParams(),
loadBalancerFactory,
transportFactory,
firstNonNull(decompressorRegistry, DecompressorRegistry.getDefaultInstance()),
firstNonNull(compressorRegistry, CompressorRegistry.getDefaultInstance()),
SharedResourcePool.forResource(GrpcUtil.TIMER_SERVICE),
getExecutorPool(executor),
SharedResourcePool.forResource(GrpcUtil.SHARED_CHANNEL_EXECUTOR),
GrpcUtil.STOPWATCH_SUPPLIER, idleTimeoutMillis,
userAgent, interceptors, firstNonNull(statsFactory,
firstNonNull(Stats.getStatsContextFactory(), NoopStatsContextFactory.INSTANCE)));
}
};
return thisT();
}

Expand Down Expand Up @@ -294,23 +314,8 @@ public ManagedChannel build() {
// getResource(), then this shouldn't be a problem unless called on the UI thread.
nameResolverFactory = NameResolverProvider.asFactory();
}
if (loadBalancer2Factory != null) {
return new ManagedChannelImpl2(
target,
// TODO(carl-mastrangelo): Allow clients to pass this in
new ExponentialBackoffPolicy.Provider(),
nameResolverFactory,
getNameResolverParams(),
loadBalancer2Factory,
transportFactory,
firstNonNull(decompressorRegistry, DecompressorRegistry.getDefaultInstance()),
firstNonNull(compressorRegistry, CompressorRegistry.getDefaultInstance()),
SharedResourcePool.forResource(GrpcUtil.TIMER_SERVICE),
getExecutorPool(executor),
SharedResourcePool.forResource(GrpcUtil.SHARED_CHANNEL_EXECUTOR),
GrpcUtil.STOPWATCH_SUPPLIER, idleTimeoutMillis,
userAgent, interceptors, firstNonNull(statsFactory,
firstNonNull(Stats.getStatsContextFactory(), NoopStatsContextFactory.INSTANCE)));
if (channelFactory != null) {
return channelFactory.create(transportFactory);
} else {
return new ManagedChannelImpl(
target,
Expand Down Expand Up @@ -429,4 +434,13 @@ private T thisT() {
T thisT = (T) this;
return thisT;
}

/**
* A temporary solution to contain the reference to ManagedChannelImpl2 in the v2 LoadBalancer
* setter, so that on Android ManagedChannelImpl2 can be stripped out by ProGuard since the v2
* setter will not be called. This should be deleted along with the old ManagedChannelImpl.
*/
private interface ChannelFactory {
ManagedChannel create(ClientTransportFactory transportFactory);
}
}

0 comments on commit 0428556

Please sign in to comment.