what-the-bank/sources/okhttp3/internal/connection/RealConnectionPool.java

260 lines
9.6 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package okhttp3.internal.connection;
import java.lang.ref.Reference;
import java.net.Socket;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import o.C14866gag;
import o.C14953gcr;
import o.C14957gcv;
import okhttp3.Address;
import okhttp3.ConnectionPool;
import okhttp3.Route;
import okhttp3.internal.Util;
import okhttp3.internal.concurrent.Task;
import okhttp3.internal.concurrent.TaskQueue;
import okhttp3.internal.concurrent.TaskRunner;
import okhttp3.internal.connection.RealCall;
import okhttp3.internal.platform.Platform;
/* loaded from: classes.dex */
public final class RealConnectionPool {
public static final Companion Companion = new Companion(null);
private final TaskQueue cleanupQueue;
private final RealConnectionPool$cleanupTask$1 cleanupTask;
private final ConcurrentLinkedQueue<RealConnection> connections;
private final long keepAliveDurationNs;
private final int maxIdleConnections;
/* JADX WARN: Type inference failed for: r3v2, types: [okhttp3.internal.connection.RealConnectionPool$cleanupTask$1] */
public RealConnectionPool(TaskRunner taskRunner, int i, long j, TimeUnit timeUnit) {
C14957gcv.e(taskRunner, "");
C14957gcv.e(timeUnit, "");
this.maxIdleConnections = i;
this.keepAliveDurationNs = timeUnit.toNanos(j);
this.cleanupQueue = taskRunner.newQueue();
final String c = C14957gcv.c(Util.okHttpName, (Object) " ConnectionPool");
this.cleanupTask = new Task(this, c) { // from class: okhttp3.internal.connection.RealConnectionPool$cleanupTask$1
final RealConnectionPool this$0;
/* JADX INFO: Access modifiers changed from: package-private */
{
this.this$0 = this;
}
@Override // okhttp3.internal.concurrent.Task
public final long runOnce() {
return this.this$0.cleanup(System.nanoTime());
}
};
this.connections = new ConcurrentLinkedQueue<>();
if (j <= 0) {
throw new IllegalArgumentException(C14957gcv.c("keepAliveDuration <= 0: ", Long.valueOf(j)).toString());
}
}
public final int idleConnectionCount() {
boolean isEmpty;
ConcurrentLinkedQueue<RealConnection> concurrentLinkedQueue = this.connections;
int i = 0;
if (!(concurrentLinkedQueue instanceof Collection) || !concurrentLinkedQueue.isEmpty()) {
for (RealConnection realConnection : concurrentLinkedQueue) {
C14957gcv.c(realConnection, "");
synchronized (realConnection) {
isEmpty = realConnection.getCalls().isEmpty();
}
if (isEmpty && (i = i + 1) < 0) {
throw new ArithmeticException("Count overflow has happened.");
}
}
}
return i;
}
public final int connectionCount() {
return this.connections.size();
}
public final boolean callAcquirePooledConnection(Address address, RealCall realCall, List<Route> list, boolean z) {
C14957gcv.e(address, "");
C14957gcv.e(realCall, "");
Iterator<RealConnection> it = this.connections.iterator();
while (it.hasNext()) {
RealConnection next = it.next();
C14957gcv.c(next, "");
synchronized (next) {
if (z) {
if (!next.isMultiplexed$okhttp()) {
C14866gag c14866gag = C14866gag.c;
}
}
if (next.isEligible$okhttp(address, list)) {
realCall.acquireConnectionNoEvents(next);
return true;
}
C14866gag c14866gag2 = C14866gag.c;
}
}
return false;
}
public final void evictAll() {
Socket socket;
Iterator<RealConnection> it = this.connections.iterator();
C14957gcv.c(it, "");
while (it.hasNext()) {
RealConnection next = it.next();
C14957gcv.c(next, "");
synchronized (next) {
if (next.getCalls().isEmpty()) {
it.remove();
next.setNoNewExchanges(true);
socket = next.socket();
} else {
socket = null;
}
}
if (socket != null) {
Util.closeQuietly(socket);
}
}
if (this.connections.isEmpty()) {
this.cleanupQueue.cancelAll();
}
}
public final long cleanup(long j) {
Iterator<RealConnection> it = this.connections.iterator();
int i = 0;
RealConnection realConnection = null;
long j2 = Long.MIN_VALUE;
int i2 = 0;
while (it.hasNext()) {
RealConnection next = it.next();
C14957gcv.c(next, "");
synchronized (next) {
if (pruneAndGetAllocationCount(next, j) > 0) {
i2++;
} else {
i++;
long idleAtNs$okhttp = j - next.getIdleAtNs$okhttp();
if (idleAtNs$okhttp > j2) {
realConnection = next;
j2 = idleAtNs$okhttp;
}
C14866gag c14866gag = C14866gag.c;
}
}
}
long j3 = this.keepAliveDurationNs;
if (j2 < j3 && i <= this.maxIdleConnections) {
if (i > 0) {
return j3 - j2;
}
if (i2 > 0) {
return j3;
}
return -1L;
}
C14957gcv.e(realConnection);
synchronized (realConnection) {
if (!realConnection.getCalls().isEmpty()) {
return 0L;
}
if (realConnection.getIdleAtNs$okhttp() + j2 != j) {
return 0L;
}
realConnection.setNoNewExchanges(true);
this.connections.remove(realConnection);
Util.closeQuietly(realConnection.socket());
if (this.connections.isEmpty()) {
this.cleanupQueue.cancelAll();
}
return 0L;
}
}
/* loaded from: classes.dex */
public static final class Companion {
private Companion() {
}
public final RealConnectionPool get(ConnectionPool connectionPool) {
C14957gcv.e(connectionPool, "");
return connectionPool.getDelegate$okhttp();
}
public /* synthetic */ Companion(C14953gcr c14953gcr) {
this();
}
}
public final void put(RealConnection realConnection) {
C14957gcv.e(realConnection, "");
if (!Util.assertionsEnabled || Thread.holdsLock(realConnection)) {
this.connections.add(realConnection);
TaskQueue.schedule$default(this.cleanupQueue, this.cleanupTask, 0L, 2, null);
} else {
StringBuilder sb = new StringBuilder("Thread ");
sb.append((Object) Thread.currentThread().getName());
sb.append(" MUST hold lock on ");
sb.append(realConnection);
throw new AssertionError(sb.toString());
}
}
public final boolean connectionBecameIdle(RealConnection realConnection) {
C14957gcv.e(realConnection, "");
if (!Util.assertionsEnabled || Thread.holdsLock(realConnection)) {
if (realConnection.getNoNewExchanges() || this.maxIdleConnections == 0) {
realConnection.setNoNewExchanges(true);
this.connections.remove(realConnection);
if (this.connections.isEmpty()) {
this.cleanupQueue.cancelAll();
}
return true;
}
TaskQueue.schedule$default(this.cleanupQueue, this.cleanupTask, 0L, 2, null);
return false;
}
StringBuilder sb = new StringBuilder("Thread ");
sb.append((Object) Thread.currentThread().getName());
sb.append(" MUST hold lock on ");
sb.append(realConnection);
throw new AssertionError(sb.toString());
}
private final int pruneAndGetAllocationCount(RealConnection realConnection, long j) {
if (!Util.assertionsEnabled || Thread.holdsLock(realConnection)) {
List<Reference<RealCall>> calls = realConnection.getCalls();
int i = 0;
while (i < calls.size()) {
Reference<RealCall> reference = calls.get(i);
if (reference.get() != null) {
i++;
} else {
StringBuilder sb = new StringBuilder("A connection to ");
sb.append(realConnection.route().address().url());
sb.append(" was leaked. Did you forget to close a response body?");
Platform.Companion.get().logCloseableLeak(sb.toString(), ((RealCall.CallReference) reference).getCallStackTrace());
calls.remove(i);
realConnection.setNoNewExchanges(true);
if (calls.isEmpty()) {
realConnection.setIdleAtNs$okhttp(j - this.keepAliveDurationNs);
return 0;
}
}
}
return calls.size();
}
StringBuilder sb2 = new StringBuilder("Thread ");
sb2.append((Object) Thread.currentThread().getName());
sb2.append(" MUST hold lock on ");
sb2.append(realConnection);
throw new AssertionError(sb2.toString());
}
}