what-the-bank/sources/io/grpc/internal/PickFirstLoadBalancer.java

186 lines
8.1 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package io.grpc.internal;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import io.grpc.ConnectivityState;
import io.grpc.ConnectivityStateInfo;
import io.grpc.EquivalentAddressGroup;
import io.grpc.LoadBalancer;
import io.grpc.Status;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
/* loaded from: classes6.dex */
final class PickFirstLoadBalancer extends LoadBalancer {
private final LoadBalancer.Helper helper;
private LoadBalancer.Subchannel subchannel;
/* JADX INFO: Access modifiers changed from: package-private */
public PickFirstLoadBalancer(LoadBalancer.Helper helper) {
this.helper = (LoadBalancer.Helper) Preconditions.checkNotNull(helper, "helper");
}
@Override // io.grpc.LoadBalancer
public final void handleResolvedAddresses(LoadBalancer.ResolvedAddresses resolvedAddresses) {
List<EquivalentAddressGroup> addresses = resolvedAddresses.getAddresses();
LoadBalancer.Subchannel subchannel = this.subchannel;
if (subchannel == null) {
LoadBalancer.Subchannel createSubchannel = this.helper.createSubchannel(LoadBalancer.CreateSubchannelArgs.newBuilder().setAddresses(addresses).build());
createSubchannel.start(new LoadBalancer.SubchannelStateListener(this, createSubchannel) { // from class: io.grpc.internal.PickFirstLoadBalancer.1
final PickFirstLoadBalancer this$0;
final LoadBalancer.Subchannel val$subchannel;
{
this.this$0 = this;
this.val$subchannel = createSubchannel;
}
@Override // io.grpc.LoadBalancer.SubchannelStateListener
public void onSubchannelState(ConnectivityStateInfo connectivityStateInfo) {
this.this$0.processSubchannelState(this.val$subchannel, connectivityStateInfo);
}
});
this.subchannel = createSubchannel;
this.helper.updateBalancingState(ConnectivityState.CONNECTING, new Picker(LoadBalancer.PickResult.withSubchannel(createSubchannel)));
createSubchannel.requestConnection();
return;
}
subchannel.updateAddresses(addresses);
}
@Override // io.grpc.LoadBalancer
public final void handleNameResolutionError(Status status) {
LoadBalancer.Subchannel subchannel = this.subchannel;
if (subchannel != null) {
subchannel.shutdown();
this.subchannel = null;
}
this.helper.updateBalancingState(ConnectivityState.TRANSIENT_FAILURE, new Picker(LoadBalancer.PickResult.withError(status)));
}
/* JADX INFO: Access modifiers changed from: private */
public void processSubchannelState(LoadBalancer.Subchannel subchannel, ConnectivityStateInfo connectivityStateInfo) {
LoadBalancer.SubchannelPicker requestConnectionPicker;
LoadBalancer.SubchannelPicker subchannelPicker;
ConnectivityState state = connectivityStateInfo.getState();
if (state == ConnectivityState.SHUTDOWN) {
return;
}
if (connectivityStateInfo.getState() == ConnectivityState.TRANSIENT_FAILURE || connectivityStateInfo.getState() == ConnectivityState.IDLE) {
this.helper.refreshNameResolution();
}
int i = AnonymousClass2.$SwitchMap$io$grpc$ConnectivityState[state.ordinal()];
if (i == 1) {
requestConnectionPicker = new RequestConnectionPicker(this, subchannel);
} else {
if (i == 2) {
subchannelPicker = new Picker(LoadBalancer.PickResult.withNoResult());
} else if (i == 3) {
requestConnectionPicker = new Picker(LoadBalancer.PickResult.withSubchannel(subchannel));
} else if (i == 4) {
subchannelPicker = new Picker(LoadBalancer.PickResult.withError(connectivityStateInfo.getStatus()));
} else {
throw new IllegalArgumentException("Unsupported state:".concat(String.valueOf(state)));
}
this.helper.updateBalancingState(state, subchannelPicker);
}
subchannelPicker = requestConnectionPicker;
this.helper.updateBalancingState(state, subchannelPicker);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* renamed from: io.grpc.internal.PickFirstLoadBalancer$2, reason: invalid class name */
/* loaded from: classes6.dex */
public static /* synthetic */ class AnonymousClass2 {
static final int[] $SwitchMap$io$grpc$ConnectivityState;
static {
int[] iArr = new int[ConnectivityState.values().length];
$SwitchMap$io$grpc$ConnectivityState = iArr;
try {
iArr[ConnectivityState.IDLE.ordinal()] = 1;
} catch (NoSuchFieldError unused) {
}
try {
$SwitchMap$io$grpc$ConnectivityState[ConnectivityState.CONNECTING.ordinal()] = 2;
} catch (NoSuchFieldError unused2) {
}
try {
$SwitchMap$io$grpc$ConnectivityState[ConnectivityState.READY.ordinal()] = 3;
} catch (NoSuchFieldError unused3) {
}
try {
$SwitchMap$io$grpc$ConnectivityState[ConnectivityState.TRANSIENT_FAILURE.ordinal()] = 4;
} catch (NoSuchFieldError unused4) {
}
}
}
@Override // io.grpc.LoadBalancer
public final void shutdown() {
LoadBalancer.Subchannel subchannel = this.subchannel;
if (subchannel != null) {
subchannel.shutdown();
}
}
@Override // io.grpc.LoadBalancer
public final void requestConnection() {
LoadBalancer.Subchannel subchannel = this.subchannel;
if (subchannel != null) {
subchannel.requestConnection();
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public static final class Picker extends LoadBalancer.SubchannelPicker {
private final LoadBalancer.PickResult result;
Picker(LoadBalancer.PickResult pickResult) {
this.result = (LoadBalancer.PickResult) Preconditions.checkNotNull(pickResult, "result");
}
public final String toString() {
return MoreObjects.toStringHelper((Class<?>) Picker.class).add("result", this.result).toString();
}
@Override // io.grpc.LoadBalancer.SubchannelPicker
public final LoadBalancer.PickResult pickSubchannel(LoadBalancer.PickSubchannelArgs pickSubchannelArgs) {
return this.result;
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public final class RequestConnectionPicker extends LoadBalancer.SubchannelPicker {
private final AtomicBoolean connectionRequested = new AtomicBoolean(false);
private final LoadBalancer.Subchannel subchannel;
final PickFirstLoadBalancer this$0;
RequestConnectionPicker(PickFirstLoadBalancer pickFirstLoadBalancer, LoadBalancer.Subchannel subchannel) {
this.this$0 = pickFirstLoadBalancer;
this.subchannel = (LoadBalancer.Subchannel) Preconditions.checkNotNull(subchannel, "subchannel");
}
@Override // io.grpc.LoadBalancer.SubchannelPicker
public final LoadBalancer.PickResult pickSubchannel(LoadBalancer.PickSubchannelArgs pickSubchannelArgs) {
if (this.connectionRequested.compareAndSet(false, true)) {
this.this$0.helper.getSynchronizationContext().execute(new Runnable(this) { // from class: io.grpc.internal.PickFirstLoadBalancer.RequestConnectionPicker.1
final RequestConnectionPicker this$1;
{
this.this$1 = this;
}
@Override // java.lang.Runnable
public void run() {
this.this$1.subchannel.requestConnection();
}
});
}
return LoadBalancer.PickResult.withNoResult();
}
}
}