package io.grpc.util; import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; import io.grpc.ConnectivityState; import io.grpc.ConnectivityStateInfo; import io.grpc.LoadBalancer; import io.grpc.Status; /* loaded from: classes6.dex */ public final class GracefulSwitchLoadBalancer extends ForwardingLoadBalancer { static final LoadBalancer.SubchannelPicker BUFFER_PICKER = new LoadBalancer.SubchannelPicker() { // from class: io.grpc.util.GracefulSwitchLoadBalancer.2 @Override // io.grpc.LoadBalancer.SubchannelPicker public LoadBalancer.PickResult pickSubchannel(LoadBalancer.PickSubchannelArgs pickSubchannelArgs) { return LoadBalancer.PickResult.withNoResult(); } public String toString() { return "BUFFER_PICKER"; } }; private LoadBalancer.Factory currentBalancerFactory; private LoadBalancer currentLb; private boolean currentLbIsReady; private final LoadBalancer defaultBalancer; private final LoadBalancer.Helper helper; private LoadBalancer.Factory pendingBalancerFactory; private LoadBalancer pendingLb; private LoadBalancer.SubchannelPicker pendingPicker; private ConnectivityState pendingState; public GracefulSwitchLoadBalancer(LoadBalancer.Helper helper) { LoadBalancer loadBalancer = new LoadBalancer(this) { // from class: io.grpc.util.GracefulSwitchLoadBalancer.1 final GracefulSwitchLoadBalancer this$0; @Override // io.grpc.LoadBalancer public void shutdown() { } { this.this$0 = this; } @Override // io.grpc.LoadBalancer public void handleResolvedAddresses(LoadBalancer.ResolvedAddresses resolvedAddresses) { throw new IllegalStateException("GracefulSwitchLoadBalancer must switch to a load balancing policy before handling ResolvedAddresses"); } @Override // io.grpc.LoadBalancer public void handleNameResolutionError(Status status) { this.this$0.helper.updateBalancingState(ConnectivityState.TRANSIENT_FAILURE, new LoadBalancer.SubchannelPicker(this, status) { // from class: io.grpc.util.GracefulSwitchLoadBalancer.1.1ErrorPicker final AnonymousClass1 this$1; final Status val$error; { this.this$1 = this; this.val$error = status; } @Override // io.grpc.LoadBalancer.SubchannelPicker public LoadBalancer.PickResult pickSubchannel(LoadBalancer.PickSubchannelArgs pickSubchannelArgs) { return LoadBalancer.PickResult.withError(this.val$error); } public String toString() { return MoreObjects.toStringHelper((Class) C1ErrorPicker.class).add("error", this.val$error).toString(); } }); } }; this.defaultBalancer = loadBalancer; this.currentLb = loadBalancer; this.pendingLb = loadBalancer; this.helper = (LoadBalancer.Helper) Preconditions.checkNotNull(helper, "helper"); } public final void switchTo(LoadBalancer.Factory factory) { Preconditions.checkNotNull(factory, "newBalancerFactory"); if (factory.equals(this.pendingBalancerFactory)) { return; } this.pendingLb.shutdown(); this.pendingLb = this.defaultBalancer; this.pendingBalancerFactory = null; this.pendingState = ConnectivityState.CONNECTING; this.pendingPicker = BUFFER_PICKER; if (factory.equals(this.currentBalancerFactory)) { return; } C1PendingHelper c1PendingHelper = new C1PendingHelper(this); c1PendingHelper.lb = factory.newLoadBalancer(c1PendingHelper); this.pendingLb = c1PendingHelper.lb; this.pendingBalancerFactory = factory; if (this.currentLbIsReady) { return; } swap(); } /* renamed from: io.grpc.util.GracefulSwitchLoadBalancer$1PendingHelper, reason: invalid class name */ /* loaded from: classes6.dex */ class C1PendingHelper extends ForwardingLoadBalancerHelper { LoadBalancer lb; final GracefulSwitchLoadBalancer this$0; C1PendingHelper(GracefulSwitchLoadBalancer gracefulSwitchLoadBalancer) { this.this$0 = gracefulSwitchLoadBalancer; } @Override // io.grpc.util.ForwardingLoadBalancerHelper protected LoadBalancer.Helper delegate() { return this.this$0.helper; } @Override // io.grpc.util.ForwardingLoadBalancerHelper, io.grpc.LoadBalancer.Helper public void updateBalancingState(ConnectivityState connectivityState, LoadBalancer.SubchannelPicker subchannelPicker) { if (this.lb == this.this$0.pendingLb) { Preconditions.checkState(this.this$0.currentLbIsReady, "there's pending lb while current lb has been out of READY"); this.this$0.pendingState = connectivityState; this.this$0.pendingPicker = subchannelPicker; if (connectivityState == ConnectivityState.READY) { this.this$0.swap(); return; } return; } if (this.lb == this.this$0.currentLb) { this.this$0.currentLbIsReady = connectivityState == ConnectivityState.READY; if (this.this$0.currentLbIsReady || this.this$0.pendingLb == this.this$0.defaultBalancer) { this.this$0.helper.updateBalancingState(connectivityState, subchannelPicker); } else { this.this$0.swap(); } } } } /* JADX INFO: Access modifiers changed from: private */ public void swap() { this.helper.updateBalancingState(this.pendingState, this.pendingPicker); this.currentLb.shutdown(); this.currentLb = this.pendingLb; this.currentBalancerFactory = this.pendingBalancerFactory; this.pendingLb = this.defaultBalancer; this.pendingBalancerFactory = null; } @Override // io.grpc.util.ForwardingLoadBalancer, io.grpc.LoadBalancer @Deprecated public final void handleSubchannelState(LoadBalancer.Subchannel subchannel, ConnectivityStateInfo connectivityStateInfo) { StringBuilder sb = new StringBuilder("handleSubchannelState() is not supported by "); sb.append(getClass().getName()); throw new UnsupportedOperationException(sb.toString()); } @Override // io.grpc.util.ForwardingLoadBalancer, io.grpc.LoadBalancer public final void shutdown() { this.pendingLb.shutdown(); this.currentLb.shutdown(); } @Override // io.grpc.util.ForwardingLoadBalancer protected final LoadBalancer delegate() { LoadBalancer loadBalancer = this.pendingLb; return loadBalancer == this.defaultBalancer ? this.currentLb : loadBalancer; } }