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

230 lines
9.0 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package io.grpc.internal;
import com.google.common.base.Preconditions;
import com.google.common.base.Stopwatch;
import com.google.common.util.concurrent.MoreExecutors;
import io.grpc.Status;
import io.grpc.internal.ClientTransport;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
/* loaded from: classes6.dex */
public class KeepAliveManager {
private final boolean keepAliveDuringTransportIdle;
private final KeepAlivePinger keepAlivePinger;
private final long keepAliveTimeInNanos;
private final long keepAliveTimeoutInNanos;
private ScheduledFuture<?> pingFuture;
private final ScheduledExecutorService scheduler;
private final Runnable sendPing;
private final Runnable shutdown;
private ScheduledFuture<?> shutdownFuture;
private State state;
private final Stopwatch stopwatch;
private static final long MIN_KEEPALIVE_TIME_NANOS = TimeUnit.SECONDS.toNanos(10);
private static final long MIN_KEEPALIVE_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(10);
/* loaded from: classes6.dex */
public interface KeepAlivePinger {
void onPingTimeout();
void ping();
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public enum State {
IDLE,
PING_SCHEDULED,
PING_DELAYED,
PING_SENT,
IDLE_AND_PING_SENT,
DISCONNECTED
}
public KeepAliveManager(KeepAlivePinger keepAlivePinger, ScheduledExecutorService scheduledExecutorService, long j, long j2, boolean z) {
this(keepAlivePinger, scheduledExecutorService, Stopwatch.createUnstarted(), j, j2, z);
}
KeepAliveManager(KeepAlivePinger keepAlivePinger, ScheduledExecutorService scheduledExecutorService, Stopwatch stopwatch, long j, long j2, boolean z) {
this.state = State.IDLE;
this.shutdown = new LogExceptionRunnable(new Runnable(this) { // from class: io.grpc.internal.KeepAliveManager.1
final KeepAliveManager this$0;
{
this.this$0 = this;
}
@Override // java.lang.Runnable
public void run() {
boolean z2;
synchronized (this.this$0) {
if (this.this$0.state != State.DISCONNECTED) {
this.this$0.state = State.DISCONNECTED;
z2 = true;
} else {
z2 = false;
}
}
if (z2) {
this.this$0.keepAlivePinger.onPingTimeout();
}
}
});
this.sendPing = new LogExceptionRunnable(new Runnable(this) { // from class: io.grpc.internal.KeepAliveManager.2
final KeepAliveManager this$0;
{
this.this$0 = this;
}
@Override // java.lang.Runnable
public void run() {
boolean z2;
synchronized (this.this$0) {
this.this$0.pingFuture = null;
if (this.this$0.state == State.PING_SCHEDULED) {
this.this$0.state = State.PING_SENT;
KeepAliveManager keepAliveManager = this.this$0;
keepAliveManager.shutdownFuture = keepAliveManager.scheduler.schedule(this.this$0.shutdown, this.this$0.keepAliveTimeoutInNanos, TimeUnit.NANOSECONDS);
z2 = true;
} else {
if (this.this$0.state == State.PING_DELAYED) {
KeepAliveManager keepAliveManager2 = this.this$0;
keepAliveManager2.pingFuture = keepAliveManager2.scheduler.schedule(this.this$0.sendPing, this.this$0.keepAliveTimeInNanos - this.this$0.stopwatch.elapsed(TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS);
this.this$0.state = State.PING_SCHEDULED;
}
z2 = false;
}
}
if (z2) {
this.this$0.keepAlivePinger.ping();
}
}
});
this.keepAlivePinger = (KeepAlivePinger) Preconditions.checkNotNull(keepAlivePinger, "keepAlivePinger");
this.scheduler = (ScheduledExecutorService) Preconditions.checkNotNull(scheduledExecutorService, "scheduler");
this.stopwatch = (Stopwatch) Preconditions.checkNotNull(stopwatch, "stopwatch");
this.keepAliveTimeInNanos = j;
this.keepAliveTimeoutInNanos = j2;
this.keepAliveDuringTransportIdle = z;
stopwatch.reset().start();
}
public void onTransportStarted() {
synchronized (this) {
if (this.keepAliveDuringTransportIdle) {
onTransportActive();
}
}
}
public void onDataReceived() {
synchronized (this) {
this.stopwatch.reset().start();
if (this.state == State.PING_SCHEDULED) {
this.state = State.PING_DELAYED;
} else if (this.state == State.PING_SENT || this.state == State.IDLE_AND_PING_SENT) {
ScheduledFuture<?> scheduledFuture = this.shutdownFuture;
if (scheduledFuture != null) {
scheduledFuture.cancel(false);
}
if (this.state == State.IDLE_AND_PING_SENT) {
this.state = State.IDLE;
} else {
this.state = State.PING_SCHEDULED;
Preconditions.checkState(this.pingFuture == null, "There should be no outstanding pingFuture");
this.pingFuture = this.scheduler.schedule(this.sendPing, this.keepAliveTimeInNanos, TimeUnit.NANOSECONDS);
}
}
}
}
public void onTransportActive() {
synchronized (this) {
if (this.state == State.IDLE) {
this.state = State.PING_SCHEDULED;
if (this.pingFuture == null) {
this.pingFuture = this.scheduler.schedule(this.sendPing, this.keepAliveTimeInNanos - this.stopwatch.elapsed(TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS);
}
} else if (this.state == State.IDLE_AND_PING_SENT) {
this.state = State.PING_SENT;
}
}
}
public void onTransportIdle() {
synchronized (this) {
if (this.keepAliveDuringTransportIdle) {
return;
}
if (this.state == State.PING_SCHEDULED || this.state == State.PING_DELAYED) {
this.state = State.IDLE;
}
if (this.state == State.PING_SENT) {
this.state = State.IDLE_AND_PING_SENT;
}
}
}
public void onTransportTermination() {
synchronized (this) {
if (this.state != State.DISCONNECTED) {
this.state = State.DISCONNECTED;
ScheduledFuture<?> scheduledFuture = this.shutdownFuture;
if (scheduledFuture != null) {
scheduledFuture.cancel(false);
}
ScheduledFuture<?> scheduledFuture2 = this.pingFuture;
if (scheduledFuture2 != null) {
scheduledFuture2.cancel(false);
this.pingFuture = null;
}
}
}
}
public static long clampKeepAliveTimeInNanos(long j) {
return Math.max(j, MIN_KEEPALIVE_TIME_NANOS);
}
public static long clampKeepAliveTimeoutInNanos(long j) {
return Math.max(j, MIN_KEEPALIVE_TIMEOUT_NANOS);
}
/* loaded from: classes6.dex */
public static final class ClientKeepAlivePinger implements KeepAlivePinger {
private final ConnectionClientTransport transport;
public ClientKeepAlivePinger(ConnectionClientTransport connectionClientTransport) {
this.transport = connectionClientTransport;
}
@Override // io.grpc.internal.KeepAliveManager.KeepAlivePinger
public final void ping() {
this.transport.ping(new ClientTransport.PingCallback(this) { // from class: io.grpc.internal.KeepAliveManager.ClientKeepAlivePinger.1
final ClientKeepAlivePinger this$0;
@Override // io.grpc.internal.ClientTransport.PingCallback
public void onSuccess(long j) {
}
{
this.this$0 = this;
}
@Override // io.grpc.internal.ClientTransport.PingCallback
public void onFailure(Throwable th) {
this.this$0.transport.shutdownNow(Status.UNAVAILABLE.withDescription("Keepalive failed. The connection is likely gone"));
}
}, MoreExecutors.directExecutor());
}
@Override // io.grpc.internal.KeepAliveManager.KeepAlivePinger
public final void onPingTimeout() {
this.transport.shutdownNow(Status.UNAVAILABLE.withDescription("Keepalive failed. The connection is likely gone"));
}
}
}