200 lines
6.6 KiB
Java
200 lines
6.6 KiB
Java
package okhttp3.repackaged;
|
|
|
|
import java.lang.ref.Reference;
|
|
import java.util.ArrayDeque;
|
|
import java.util.ArrayList;
|
|
import java.util.Deque;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.concurrent.Executor;
|
|
import java.util.concurrent.SynchronousQueue;
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.logging.Logger;
|
|
import okhttp3.repackaged.internal.Internal;
|
|
import okhttp3.repackaged.internal.RouteDatabase;
|
|
import okhttp3.repackaged.internal.Util;
|
|
import okhttp3.repackaged.internal.http.StreamAllocation;
|
|
import okhttp3.repackaged.internal.io.RealConnection;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public final class ConnectionPool {
|
|
static final boolean $assertionsDisabled = false;
|
|
private static final Executor ahj = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS, new SynchronousQueue(), Util.threadFactory("OkHttp ConnectionPool", true));
|
|
private final int ahk;
|
|
private final long ahl;
|
|
private final Runnable ahm;
|
|
private final Deque<RealConnection> ahn;
|
|
final RouteDatabase aho;
|
|
boolean cleanupRunning;
|
|
|
|
public ConnectionPool() {
|
|
this(5, 5L, TimeUnit.MINUTES);
|
|
}
|
|
|
|
public ConnectionPool(int i, long j, TimeUnit timeUnit) {
|
|
this.ahm = new Runnable(this) { // from class: okhttp3.repackaged.ConnectionPool.1
|
|
final ConnectionPool ahp;
|
|
|
|
{
|
|
this.ahp = this;
|
|
}
|
|
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
while (true) {
|
|
long cleanup = this.ahp.cleanup(System.nanoTime());
|
|
if (cleanup == -1) {
|
|
return;
|
|
}
|
|
if (cleanup > 0) {
|
|
long j2 = cleanup / 1000000;
|
|
synchronized (this.ahp) {
|
|
try {
|
|
this.ahp.wait(j2, (int) (cleanup - (1000000 * j2)));
|
|
} catch (InterruptedException unused) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
this.ahn = new ArrayDeque();
|
|
this.aho = new RouteDatabase();
|
|
this.ahk = i;
|
|
this.ahl = timeUnit.toNanos(j);
|
|
if (j <= 0) {
|
|
throw new IllegalArgumentException("keepAliveDuration <= 0: ".concat(String.valueOf(j)));
|
|
}
|
|
}
|
|
|
|
public final int idleConnectionCount() {
|
|
int i;
|
|
synchronized (this) {
|
|
Iterator<RealConnection> it = this.ahn.iterator();
|
|
i = 0;
|
|
while (it.hasNext()) {
|
|
if (it.next().allocations.isEmpty()) {
|
|
i++;
|
|
}
|
|
}
|
|
}
|
|
return i;
|
|
}
|
|
|
|
public final int connectionCount() {
|
|
int size;
|
|
synchronized (this) {
|
|
size = this.ahn.size();
|
|
}
|
|
return size;
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
public final RealConnection a(Address address, StreamAllocation streamAllocation) {
|
|
for (RealConnection realConnection : this.ahn) {
|
|
if (realConnection.allocations.size() < realConnection.allocationLimit && address.equals(realConnection.route().address) && !realConnection.noNewStreams) {
|
|
streamAllocation.acquire(realConnection);
|
|
return realConnection;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
public final void a(RealConnection realConnection) {
|
|
if (!this.cleanupRunning) {
|
|
this.cleanupRunning = true;
|
|
ahj.execute(this.ahm);
|
|
}
|
|
this.ahn.add(realConnection);
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
public final boolean b(RealConnection realConnection) {
|
|
if (realConnection.noNewStreams || this.ahk == 0) {
|
|
this.ahn.remove(realConnection);
|
|
return true;
|
|
}
|
|
notifyAll();
|
|
return false;
|
|
}
|
|
|
|
public final void evictAll() {
|
|
ArrayList arrayList = new ArrayList();
|
|
synchronized (this) {
|
|
Iterator<RealConnection> it = this.ahn.iterator();
|
|
while (it.hasNext()) {
|
|
RealConnection next = it.next();
|
|
if (next.allocations.isEmpty()) {
|
|
next.noNewStreams = true;
|
|
arrayList.add(next);
|
|
it.remove();
|
|
}
|
|
}
|
|
}
|
|
Iterator it2 = arrayList.iterator();
|
|
while (it2.hasNext()) {
|
|
Util.closeQuietly(((RealConnection) it2.next()).socket());
|
|
}
|
|
}
|
|
|
|
final long cleanup(long j) {
|
|
synchronized (this) {
|
|
RealConnection realConnection = null;
|
|
long j2 = Long.MIN_VALUE;
|
|
int i = 0;
|
|
int i2 = 0;
|
|
for (RealConnection realConnection2 : this.ahn) {
|
|
if (a(realConnection2, j) > 0) {
|
|
i2++;
|
|
} else {
|
|
i++;
|
|
long j3 = j - realConnection2.idleAtNanos;
|
|
if (j3 > j2) {
|
|
realConnection = realConnection2;
|
|
j2 = j3;
|
|
}
|
|
}
|
|
}
|
|
long j4 = this.ahl;
|
|
if (j2 >= j4 || i > this.ahk) {
|
|
this.ahn.remove(realConnection);
|
|
Util.closeQuietly(realConnection.socket());
|
|
return 0L;
|
|
}
|
|
if (i > 0) {
|
|
return j4 - j2;
|
|
}
|
|
if (i2 > 0) {
|
|
return j4;
|
|
}
|
|
this.cleanupRunning = false;
|
|
return -1L;
|
|
}
|
|
}
|
|
|
|
private int a(RealConnection realConnection, long j) {
|
|
List<Reference<StreamAllocation>> list = realConnection.allocations;
|
|
int i = 0;
|
|
while (i < list.size()) {
|
|
if (list.get(i).get() != null) {
|
|
i++;
|
|
} else {
|
|
Logger logger = Internal.logger;
|
|
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?");
|
|
logger.warning(sb.toString());
|
|
list.remove(i);
|
|
realConnection.noNewStreams = true;
|
|
if (list.isEmpty()) {
|
|
realConnection.idleAtNanos = j - this.ahl;
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
return list.size();
|
|
}
|
|
}
|