what-the-bank/sources/io/grpc/okhttp/OkHttpChannelBuilder.java

626 lines
31 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package io.grpc.okhttp;
import com.google.common.base.Preconditions;
import io.grpc.CallCredentials;
import io.grpc.ChannelCredentials;
import io.grpc.ChannelLogger;
import io.grpc.ChoiceChannelCredentials;
import io.grpc.CompositeCallCredentials;
import io.grpc.CompositeChannelCredentials;
import io.grpc.InsecureChannelCredentials;
import io.grpc.ManagedChannelBuilder;
import io.grpc.TlsChannelCredentials;
import io.grpc.internal.AbstractManagedChannelImplBuilder;
import io.grpc.internal.AtomicBackoff;
import io.grpc.internal.ClientTransportFactory;
import io.grpc.internal.ConnectionClientTransport;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.KeepAliveManager;
import io.grpc.internal.ManagedChannelImplBuilder;
import io.grpc.internal.SharedResourceHolder;
import io.grpc.internal.TransportTracer;
import io.grpc.okhttp.SslSocketFactoryChannelCredentials;
import io.grpc.okhttp.internal.CipherSuite;
import io.grpc.okhttp.internal.ConnectionSpec;
import io.grpc.okhttp.internal.Platform;
import io.grpc.okhttp.internal.TlsVersion;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.SocketFactory;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import o.C12990fNc;
/* loaded from: classes6.dex */
public final class OkHttpChannelBuilder extends AbstractManagedChannelImplBuilder<OkHttpChannelBuilder> {
public static final int DEFAULT_FLOW_CONTROL_WINDOW = 65535;
private ConnectionSpec connectionSpec;
private int flowControlWindow;
private final boolean freezeSecurityConfiguration;
private HostnameVerifier hostnameVerifier;
private long keepAliveTimeNanos;
private long keepAliveTimeoutNanos;
private boolean keepAliveWithoutCalls;
private final ManagedChannelImplBuilder managedChannelImplBuilder;
private int maxInboundMetadataSize;
private NegotiationType negotiationType;
private ScheduledExecutorService scheduledExecutorService;
private SocketFactory socketFactory;
private SSLSocketFactory sslSocketFactory;
private Executor transportExecutor;
private TransportTracer.Factory transportTracerFactory;
private final boolean useGetForSafeMethods;
private static final Logger log = Logger.getLogger(OkHttpChannelBuilder.class.getName());
static final ConnectionSpec INTERNAL_DEFAULT_CONNECTION_SPEC = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).cipherSuites(CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, CipherSuite.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, CipherSuite.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256).tlsVersions(TlsVersion.TLS_1_2).supportsTlsExtensions(true).build();
private static final long AS_LARGE_AS_INFINITE = TimeUnit.DAYS.toNanos(1000);
private static final SharedResourceHolder.Resource<Executor> SHARED_EXECUTOR = new SharedResourceHolder.Resource<Executor>() { // from class: io.grpc.okhttp.OkHttpChannelBuilder.1
@Override // io.grpc.internal.SharedResourceHolder.Resource
public Executor create() {
return Executors.newCachedThreadPool(GrpcUtil.getThreadFactory("grpc-okhttp-%d", true));
}
@Override // io.grpc.internal.SharedResourceHolder.Resource
public void close(Executor executor) {
((ExecutorService) executor).shutdown();
}
};
private static final EnumSet<TlsChannelCredentials.Feature> understoodTlsFeatures = EnumSet.of(TlsChannelCredentials.Feature.MTLS, TlsChannelCredentials.Feature.CUSTOM_MANAGERS);
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public enum NegotiationType {
TLS,
PLAINTEXT
}
public static OkHttpChannelBuilder forAddress(String str, int i) {
return new OkHttpChannelBuilder(str, i);
}
public static OkHttpChannelBuilder forAddress(String str, int i, ChannelCredentials channelCredentials) {
return forTarget(GrpcUtil.authorityFromHostAndPort(str, i), channelCredentials);
}
public static OkHttpChannelBuilder forTarget(String str) {
return new OkHttpChannelBuilder(str);
}
public static OkHttpChannelBuilder forTarget(String str, ChannelCredentials channelCredentials) {
SslSocketFactoryResult sslSocketFactoryFrom = sslSocketFactoryFrom(channelCredentials);
if (sslSocketFactoryFrom.error != null) {
throw new IllegalArgumentException(sslSocketFactoryFrom.error);
}
return new OkHttpChannelBuilder(str, channelCredentials, sslSocketFactoryFrom.callCredentials, sslSocketFactoryFrom.factory);
}
private OkHttpChannelBuilder(String str, int i) {
this(GrpcUtil.authorityFromHostAndPort(str, i));
}
private OkHttpChannelBuilder(String str) {
this.transportTracerFactory = TransportTracer.getDefaultFactory();
this.connectionSpec = INTERNAL_DEFAULT_CONNECTION_SPEC;
this.negotiationType = NegotiationType.TLS;
this.keepAliveTimeNanos = Long.MAX_VALUE;
this.keepAliveTimeoutNanos = GrpcUtil.DEFAULT_KEEPALIVE_TIMEOUT_NANOS;
this.flowControlWindow = 65535;
this.maxInboundMetadataSize = Integer.MAX_VALUE;
this.useGetForSafeMethods = false;
this.managedChannelImplBuilder = new ManagedChannelImplBuilder(str, new OkHttpChannelTransportFactoryBuilder(), new OkHttpChannelDefaultPortProvider());
this.freezeSecurityConfiguration = false;
}
/* JADX INFO: Access modifiers changed from: package-private */
public OkHttpChannelBuilder(String str, ChannelCredentials channelCredentials, CallCredentials callCredentials, SSLSocketFactory sSLSocketFactory) {
this.transportTracerFactory = TransportTracer.getDefaultFactory();
this.connectionSpec = INTERNAL_DEFAULT_CONNECTION_SPEC;
this.negotiationType = NegotiationType.TLS;
this.keepAliveTimeNanos = Long.MAX_VALUE;
this.keepAliveTimeoutNanos = GrpcUtil.DEFAULT_KEEPALIVE_TIMEOUT_NANOS;
this.flowControlWindow = 65535;
this.maxInboundMetadataSize = Integer.MAX_VALUE;
this.useGetForSafeMethods = false;
this.managedChannelImplBuilder = new ManagedChannelImplBuilder(str, channelCredentials, callCredentials, new OkHttpChannelTransportFactoryBuilder(), new OkHttpChannelDefaultPortProvider());
this.sslSocketFactory = sSLSocketFactory;
this.negotiationType = sSLSocketFactory == null ? NegotiationType.PLAINTEXT : NegotiationType.TLS;
this.freezeSecurityConfiguration = true;
}
/* loaded from: classes6.dex */
final class OkHttpChannelTransportFactoryBuilder implements ManagedChannelImplBuilder.ClientTransportFactoryBuilder {
final OkHttpChannelBuilder this$0;
private OkHttpChannelTransportFactoryBuilder(OkHttpChannelBuilder okHttpChannelBuilder) {
this.this$0 = okHttpChannelBuilder;
}
@Override // io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder
public final ClientTransportFactory buildClientTransportFactory() {
return this.this$0.buildTransportFactory();
}
}
/* loaded from: classes6.dex */
final class OkHttpChannelDefaultPortProvider implements ManagedChannelImplBuilder.ChannelBuilderDefaultPortProvider {
final OkHttpChannelBuilder this$0;
private OkHttpChannelDefaultPortProvider(OkHttpChannelBuilder okHttpChannelBuilder) {
this.this$0 = okHttpChannelBuilder;
}
@Override // io.grpc.internal.ManagedChannelImplBuilder.ChannelBuilderDefaultPortProvider
public final int getDefaultPort() {
return this.this$0.getDefaultPort();
}
}
@Deprecated
public final OkHttpChannelBuilder negotiationType(io.grpc.okhttp.NegotiationType negotiationType) {
Preconditions.checkState(!this.freezeSecurityConfiguration, "Cannot change security when using ChannelCredentials");
Preconditions.checkNotNull(negotiationType, "type");
int i = AnonymousClass2.$SwitchMap$io$grpc$okhttp$NegotiationType[negotiationType.ordinal()];
if (i == 1) {
this.negotiationType = NegotiationType.TLS;
} else if (i == 2) {
this.negotiationType = NegotiationType.PLAINTEXT;
} else {
throw new AssertionError("Unknown negotiation type: ".concat(String.valueOf(negotiationType)));
}
return this;
}
@Override // io.grpc.internal.AbstractManagedChannelImplBuilder, io.grpc.ManagedChannelBuilder
public final OkHttpChannelBuilder keepAliveTime(long j, TimeUnit timeUnit) {
Preconditions.checkArgument(j > 0, "keepalive time must be positive");
long nanos = timeUnit.toNanos(j);
this.keepAliveTimeNanos = nanos;
long clampKeepAliveTimeInNanos = KeepAliveManager.clampKeepAliveTimeInNanos(nanos);
this.keepAliveTimeNanos = clampKeepAliveTimeInNanos;
if (clampKeepAliveTimeInNanos >= AS_LARGE_AS_INFINITE) {
this.keepAliveTimeNanos = Long.MAX_VALUE;
}
return this;
}
@Override // io.grpc.internal.AbstractManagedChannelImplBuilder, io.grpc.ManagedChannelBuilder
public final OkHttpChannelBuilder keepAliveTimeout(long j, TimeUnit timeUnit) {
Preconditions.checkArgument(j > 0, "keepalive timeout must be positive");
long nanos = timeUnit.toNanos(j);
this.keepAliveTimeoutNanos = nanos;
this.keepAliveTimeoutNanos = KeepAliveManager.clampKeepAliveTimeoutInNanos(nanos);
return this;
}
public final OkHttpChannelBuilder flowControlWindow(int i) {
Preconditions.checkState(i > 0, "flowControlWindow must be positive");
this.flowControlWindow = i;
return this;
}
public final OkHttpChannelBuilder sslSocketFactory(SSLSocketFactory sSLSocketFactory) {
Preconditions.checkState(!this.freezeSecurityConfiguration, "Cannot change security when using ChannelCredentials");
this.sslSocketFactory = sSLSocketFactory;
this.negotiationType = NegotiationType.TLS;
return this;
}
public final OkHttpChannelBuilder hostnameVerifier(HostnameVerifier hostnameVerifier) {
Preconditions.checkState(!this.freezeSecurityConfiguration, "Cannot change security when using ChannelCredentials");
this.hostnameVerifier = hostnameVerifier;
return this;
}
public final OkHttpChannelBuilder connectionSpec(C12990fNc c12990fNc) {
Preconditions.checkState(!this.freezeSecurityConfiguration, "Cannot change security when using ChannelCredentials");
Preconditions.checkArgument(c12990fNc.b, "plaintext ConnectionSpec is not accepted");
this.connectionSpec = Utils.convertSpec(c12990fNc);
return this;
}
public final OkHttpChannelBuilder tlsConnectionSpec(String[] strArr, String[] strArr2) {
Preconditions.checkState(!this.freezeSecurityConfiguration, "Cannot change security when using ChannelCredentials");
Preconditions.checkNotNull(strArr, "tls versions must not null");
Preconditions.checkNotNull(strArr2, "ciphers must not null");
this.connectionSpec = new ConnectionSpec.Builder(true).supportsTlsExtensions(true).tlsVersions(strArr).cipherSuites(strArr2).build();
return this;
}
@Override // io.grpc.internal.AbstractManagedChannelImplBuilder, io.grpc.ManagedChannelBuilder
public final OkHttpChannelBuilder usePlaintext() {
Preconditions.checkState(!this.freezeSecurityConfiguration, "Cannot change security when using ChannelCredentials");
this.negotiationType = NegotiationType.PLAINTEXT;
return this;
}
@Override // io.grpc.internal.AbstractManagedChannelImplBuilder, io.grpc.ManagedChannelBuilder
public final OkHttpChannelBuilder useTransportSecurity() {
Preconditions.checkState(!this.freezeSecurityConfiguration, "Cannot change security when using ChannelCredentials");
this.negotiationType = NegotiationType.TLS;
return this;
}
public final OkHttpChannelBuilder scheduledExecutorService(ScheduledExecutorService scheduledExecutorService) {
this.scheduledExecutorService = (ScheduledExecutorService) Preconditions.checkNotNull(scheduledExecutorService, "scheduledExecutorService");
return this;
}
@Override // io.grpc.internal.AbstractManagedChannelImplBuilder, io.grpc.ManagedChannelBuilder
public final OkHttpChannelBuilder maxInboundMetadataSize(int i) {
Preconditions.checkArgument(i > 0, "maxInboundMetadataSize must be > 0");
this.maxInboundMetadataSize = i;
return this;
}
@Override // io.grpc.internal.AbstractManagedChannelImplBuilder, io.grpc.ManagedChannelBuilder
public final OkHttpChannelBuilder maxInboundMessageSize(int i) {
Preconditions.checkArgument(i >= 0, "negative max");
this.maxInboundMessageSize = i;
return this;
}
final ClientTransportFactory buildTransportFactory() {
return new OkHttpTransportFactory(this.transportExecutor, this.scheduledExecutorService, this.socketFactory, createSslSocketFactory(), this.hostnameVerifier, this.connectionSpec, this.maxInboundMessageSize, this.keepAliveTimeNanos != Long.MAX_VALUE, this.keepAliveTimeNanos, this.keepAliveTimeoutNanos, this.flowControlWindow, this.keepAliveWithoutCalls, this.maxInboundMetadataSize, this.transportTracerFactory, false);
}
/* JADX INFO: Access modifiers changed from: package-private */
public final OkHttpChannelBuilder disableCheckAuthority() {
this.managedChannelImplBuilder.disableCheckAuthority();
return this;
}
/* JADX INFO: Access modifiers changed from: package-private */
public final OkHttpChannelBuilder enableCheckAuthority() {
this.managedChannelImplBuilder.enableCheckAuthority();
return this;
}
/* JADX INFO: Access modifiers changed from: package-private */
/* renamed from: io.grpc.okhttp.OkHttpChannelBuilder$2, reason: invalid class name */
/* loaded from: classes6.dex */
public static /* synthetic */ class AnonymousClass2 {
static final int[] $SwitchMap$io$grpc$okhttp$NegotiationType;
static final int[] $SwitchMap$io$grpc$okhttp$OkHttpChannelBuilder$NegotiationType;
static {
int[] iArr = new int[NegotiationType.values().length];
$SwitchMap$io$grpc$okhttp$OkHttpChannelBuilder$NegotiationType = iArr;
try {
iArr[NegotiationType.PLAINTEXT.ordinal()] = 1;
} catch (NoSuchFieldError unused) {
}
try {
$SwitchMap$io$grpc$okhttp$OkHttpChannelBuilder$NegotiationType[NegotiationType.TLS.ordinal()] = 2;
} catch (NoSuchFieldError unused2) {
}
int[] iArr2 = new int[io.grpc.okhttp.NegotiationType.values().length];
$SwitchMap$io$grpc$okhttp$NegotiationType = iArr2;
try {
iArr2[io.grpc.okhttp.NegotiationType.TLS.ordinal()] = 1;
} catch (NoSuchFieldError unused3) {
}
try {
$SwitchMap$io$grpc$okhttp$NegotiationType[io.grpc.okhttp.NegotiationType.PLAINTEXT.ordinal()] = 2;
} catch (NoSuchFieldError unused4) {
}
}
}
final int getDefaultPort() {
int i = AnonymousClass2.$SwitchMap$io$grpc$okhttp$OkHttpChannelBuilder$NegotiationType[this.negotiationType.ordinal()];
if (i == 1) {
return 80;
}
if (i == 2) {
return GrpcUtil.DEFAULT_PORT_SSL;
}
StringBuilder sb = new StringBuilder();
sb.append(this.negotiationType);
sb.append(" not handled");
throw new AssertionError(sb.toString());
}
/* JADX INFO: Access modifiers changed from: package-private */
public final void setStatsEnabled(boolean z) {
this.managedChannelImplBuilder.setStatsEnabled(z);
}
final SSLSocketFactory createSslSocketFactory() {
int i = AnonymousClass2.$SwitchMap$io$grpc$okhttp$OkHttpChannelBuilder$NegotiationType[this.negotiationType.ordinal()];
if (i == 1) {
return null;
}
if (i == 2) {
try {
if (this.sslSocketFactory == null) {
this.sslSocketFactory = SSLContext.getInstance("Default", Platform.get().getProvider()).getSocketFactory();
}
return this.sslSocketFactory;
} catch (GeneralSecurityException e) {
throw new RuntimeException("TLS Provider failure", e);
}
}
StringBuilder sb = new StringBuilder("Unknown negotiation type: ");
sb.append(this.negotiationType);
throw new RuntimeException(sb.toString());
}
/* JADX INFO: Access modifiers changed from: package-private */
public static SslSocketFactoryResult sslSocketFactoryFrom(ChannelCredentials channelCredentials) {
KeyManager[] keyManagerArr;
TrustManager[] createTrustManager;
if (channelCredentials instanceof TlsChannelCredentials) {
TlsChannelCredentials tlsChannelCredentials = (TlsChannelCredentials) channelCredentials;
Set<TlsChannelCredentials.Feature> incomprehensible = tlsChannelCredentials.incomprehensible(understoodTlsFeatures);
if (!incomprehensible.isEmpty()) {
return SslSocketFactoryResult.error("TLS features not understood: ".concat(String.valueOf(incomprehensible)));
}
if (tlsChannelCredentials.getKeyManagers() != null) {
keyManagerArr = (KeyManager[]) tlsChannelCredentials.getKeyManagers().toArray(new KeyManager[0]);
} else {
if (tlsChannelCredentials.getPrivateKey() != null) {
return SslSocketFactoryResult.error("byte[]-based private key unsupported. Use KeyManager");
}
keyManagerArr = null;
}
if (tlsChannelCredentials.getTrustManagers() != null) {
createTrustManager = (TrustManager[]) tlsChannelCredentials.getTrustManagers().toArray(new TrustManager[0]);
} else if (tlsChannelCredentials.getRootCertificates() != null) {
try {
createTrustManager = createTrustManager(tlsChannelCredentials.getRootCertificates());
} catch (GeneralSecurityException e) {
log.log(Level.FINE, "Exception loading root certificates from credential", (Throwable) e);
StringBuilder sb = new StringBuilder("Unable to load root certificates: ");
sb.append(e.getMessage());
return SslSocketFactoryResult.error(sb.toString());
}
} else {
createTrustManager = null;
}
try {
SSLContext sSLContext = SSLContext.getInstance(org.apache.http.conn.ssl.SSLSocketFactory.TLS, Platform.get().getProvider());
sSLContext.init(keyManagerArr, createTrustManager, null);
return SslSocketFactoryResult.factory(sSLContext.getSocketFactory());
} catch (GeneralSecurityException e2) {
throw new RuntimeException("TLS Provider failure", e2);
}
}
if (channelCredentials instanceof InsecureChannelCredentials) {
return SslSocketFactoryResult.plaintext();
}
if (channelCredentials instanceof CompositeChannelCredentials) {
CompositeChannelCredentials compositeChannelCredentials = (CompositeChannelCredentials) channelCredentials;
return sslSocketFactoryFrom(compositeChannelCredentials.getChannelCredentials()).withCallCredentials(compositeChannelCredentials.getCallCredentials());
}
if (channelCredentials instanceof SslSocketFactoryChannelCredentials.ChannelCredentials) {
return SslSocketFactoryResult.factory(((SslSocketFactoryChannelCredentials.ChannelCredentials) channelCredentials).getFactory());
}
if (channelCredentials instanceof ChoiceChannelCredentials) {
StringBuilder sb2 = new StringBuilder();
Iterator<ChannelCredentials> it = ((ChoiceChannelCredentials) channelCredentials).getCredentialsList().iterator();
while (it.hasNext()) {
SslSocketFactoryResult sslSocketFactoryFrom = sslSocketFactoryFrom(it.next());
if (sslSocketFactoryFrom.error == null) {
return sslSocketFactoryFrom;
}
sb2.append(", ");
sb2.append(sslSocketFactoryFrom.error);
}
return SslSocketFactoryResult.error(sb2.substring(2));
}
StringBuilder sb3 = new StringBuilder("Unsupported credential type: ");
sb3.append(channelCredentials.getClass().getName());
return SslSocketFactoryResult.error(sb3.toString());
}
static TrustManager[] createTrustManager(byte[] bArr) throws GeneralSecurityException {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
try {
keyStore.load(null, null);
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr);
try {
X509Certificate x509Certificate = (X509Certificate) certificateFactory.generateCertificate(byteArrayInputStream);
keyStore.setCertificateEntry(x509Certificate.getSubjectX500Principal().getName("RFC2253"), x509Certificate);
GrpcUtil.closeQuietly(byteArrayInputStream);
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
return trustManagerFactory.getTrustManagers();
} catch (Throwable th) {
GrpcUtil.closeQuietly(byteArrayInputStream);
throw th;
}
} catch (IOException e) {
throw new GeneralSecurityException(e);
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public static final class SslSocketFactoryResult {
public final CallCredentials callCredentials;
public final String error;
public final SSLSocketFactory factory;
private SslSocketFactoryResult(SSLSocketFactory sSLSocketFactory, CallCredentials callCredentials, String str) {
this.factory = sSLSocketFactory;
this.callCredentials = callCredentials;
this.error = str;
}
public static SslSocketFactoryResult error(String str) {
return new SslSocketFactoryResult(null, null, (String) Preconditions.checkNotNull(str, "error"));
}
public static SslSocketFactoryResult plaintext() {
return new SslSocketFactoryResult(null, null, null);
}
public static SslSocketFactoryResult factory(SSLSocketFactory sSLSocketFactory) {
return new SslSocketFactoryResult((SSLSocketFactory) Preconditions.checkNotNull(sSLSocketFactory, "factory"), null, null);
}
public final SslSocketFactoryResult withCallCredentials(CallCredentials callCredentials) {
Preconditions.checkNotNull(callCredentials, "callCreds");
if (this.error != null) {
return this;
}
CallCredentials callCredentials2 = this.callCredentials;
if (callCredentials2 != null) {
callCredentials = new CompositeCallCredentials(callCredentials2, callCredentials);
}
return new SslSocketFactoryResult(this.factory, callCredentials, null);
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public static final class OkHttpTransportFactory implements ClientTransportFactory {
private boolean closed;
private final ConnectionSpec connectionSpec;
private final boolean enableKeepAlive;
private final Executor executor;
private final int flowControlWindow;
private final HostnameVerifier hostnameVerifier;
private final AtomicBackoff keepAliveBackoff;
private final long keepAliveTimeNanos;
private final long keepAliveTimeoutNanos;
private final boolean keepAliveWithoutCalls;
private final int maxInboundMetadataSize;
private final int maxMessageSize;
private final SocketFactory socketFactory;
private final SSLSocketFactory sslSocketFactory;
private final ScheduledExecutorService timeoutService;
private final TransportTracer.Factory transportTracerFactory;
private final boolean useGetForSafeMethods;
private final boolean usingSharedExecutor;
private final boolean usingSharedScheduler;
private OkHttpTransportFactory(Executor executor, ScheduledExecutorService scheduledExecutorService, SocketFactory socketFactory, SSLSocketFactory sSLSocketFactory, HostnameVerifier hostnameVerifier, ConnectionSpec connectionSpec, int i, boolean z, long j, long j2, int i2, boolean z2, int i3, TransportTracer.Factory factory, boolean z3) {
boolean z4 = scheduledExecutorService == null;
this.usingSharedScheduler = z4;
this.timeoutService = z4 ? (ScheduledExecutorService) SharedResourceHolder.get(GrpcUtil.TIMER_SERVICE) : scheduledExecutorService;
this.socketFactory = socketFactory;
this.sslSocketFactory = sSLSocketFactory;
this.hostnameVerifier = hostnameVerifier;
this.connectionSpec = connectionSpec;
this.maxMessageSize = i;
this.enableKeepAlive = z;
this.keepAliveTimeNanos = j;
this.keepAliveBackoff = new AtomicBackoff("keepalive time nanos", j);
this.keepAliveTimeoutNanos = j2;
this.flowControlWindow = i2;
this.keepAliveWithoutCalls = z2;
this.maxInboundMetadataSize = i3;
this.useGetForSafeMethods = z3;
boolean z5 = executor == null;
this.usingSharedExecutor = z5;
this.transportTracerFactory = (TransportTracer.Factory) Preconditions.checkNotNull(factory, "transportTracerFactory");
if (z5) {
this.executor = (Executor) SharedResourceHolder.get(OkHttpChannelBuilder.SHARED_EXECUTOR);
} else {
this.executor = executor;
}
}
@Override // io.grpc.internal.ClientTransportFactory
public final ConnectionClientTransport newClientTransport(SocketAddress socketAddress, ClientTransportFactory.ClientTransportOptions clientTransportOptions, ChannelLogger channelLogger) {
if (this.closed) {
throw new IllegalStateException("The transport factory is closed.");
}
AtomicBackoff.State state = this.keepAliveBackoff.getState();
OkHttpClientTransport okHttpClientTransport = new OkHttpClientTransport((InetSocketAddress) socketAddress, clientTransportOptions.getAuthority(), clientTransportOptions.getUserAgent(), clientTransportOptions.getEagAttributes(), this.executor, this.socketFactory, this.sslSocketFactory, this.hostnameVerifier, this.connectionSpec, this.maxMessageSize, this.flowControlWindow, clientTransportOptions.getHttpConnectProxiedSocketAddress(), new Runnable(this, state) { // from class: io.grpc.okhttp.OkHttpChannelBuilder.OkHttpTransportFactory.1
final OkHttpTransportFactory this$0;
final AtomicBackoff.State val$keepAliveTimeNanosState;
{
this.this$0 = this;
this.val$keepAliveTimeNanosState = state;
}
@Override // java.lang.Runnable
public void run() {
this.val$keepAliveTimeNanosState.backoff();
}
}, this.maxInboundMetadataSize, this.transportTracerFactory.create(), this.useGetForSafeMethods);
if (this.enableKeepAlive) {
okHttpClientTransport.enableKeepAlive(true, state.get(), this.keepAliveTimeoutNanos, this.keepAliveWithoutCalls);
}
return okHttpClientTransport;
}
@Override // io.grpc.internal.ClientTransportFactory
public final ClientTransportFactory.SwapChannelCredentialsResult swapChannelCredentials(ChannelCredentials channelCredentials) {
SslSocketFactoryResult sslSocketFactoryFrom = OkHttpChannelBuilder.sslSocketFactoryFrom(channelCredentials);
if (sslSocketFactoryFrom.error != null) {
return null;
}
return new ClientTransportFactory.SwapChannelCredentialsResult(new OkHttpTransportFactory(this.executor, this.timeoutService, this.socketFactory, sslSocketFactoryFrom.factory, this.hostnameVerifier, this.connectionSpec, this.maxMessageSize, this.enableKeepAlive, this.keepAliveTimeNanos, this.keepAliveTimeoutNanos, this.flowControlWindow, this.keepAliveWithoutCalls, this.maxInboundMetadataSize, this.transportTracerFactory, this.useGetForSafeMethods), sslSocketFactoryFrom.callCredentials);
}
@Override // io.grpc.internal.ClientTransportFactory, java.io.Closeable, java.lang.AutoCloseable
public final void close() {
if (this.closed) {
return;
}
this.closed = true;
if (this.usingSharedScheduler) {
SharedResourceHolder.release(GrpcUtil.TIMER_SERVICE, this.timeoutService);
}
if (this.usingSharedExecutor) {
SharedResourceHolder.release(OkHttpChannelBuilder.SHARED_EXECUTOR, this.executor);
}
}
@Override // io.grpc.internal.ClientTransportFactory
public final ScheduledExecutorService getScheduledExecutorService() {
return this.timeoutService;
}
}
public final OkHttpChannelBuilder transportExecutor(Executor executor) {
this.transportExecutor = executor;
return this;
}
public final OkHttpChannelBuilder socketFactory(SocketFactory socketFactory) {
this.socketFactory = socketFactory;
return this;
}
final OkHttpChannelBuilder setTransportTracerFactory(TransportTracer.Factory factory) {
this.transportTracerFactory = factory;
return this;
}
@Override // io.grpc.internal.AbstractManagedChannelImplBuilder, io.grpc.ManagedChannelBuilder
public final OkHttpChannelBuilder keepAliveWithoutCalls(boolean z) {
this.keepAliveWithoutCalls = z;
return this;
}
@Override // io.grpc.internal.AbstractManagedChannelImplBuilder
public final ManagedChannelBuilder<?> delegate() {
return this.managedChannelImplBuilder;
}
}