package okhttp3.internal.connection; import androidx.core.app.NotificationCompat; import io.sentry.SentryLockReason; import io.sentry.okhttp.SentryOkHttpEventListener; 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 kotlin.Metadata; import kotlin.Unit; import kotlin.collections.CollectionsKt; import kotlin.jvm.internal.DefaultConstructorMarker; import kotlin.jvm.internal.Intrinsics; 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; /* compiled from: RealConnectionPool.kt */ @Metadata(bv = {1, 0, 3}, d1 = {"\u0000c\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010\t\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000b\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010 \n\u0002\u0018\u0002\n\u0002\b\u0007\n\u0002\u0010\u0002\n\u0002\b\u0005*\u0001\u000e\u0018\u0000 (2\u00020\u0001:\u0001(B%\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005\u0012\u0006\u0010\u0006\u001a\u00020\u0007\u0012\u0006\u0010\b\u001a\u00020\t¢\u0006\u0002\u0010\nJ.\u0010\u0014\u001a\u00020\u00152\u0006\u0010\u0016\u001a\u00020\u00172\u0006\u0010\u0018\u001a\u00020\u00192\u000e\u0010\u001a\u001a\n\u0012\u0004\u0012\u00020\u001c\u0018\u00010\u001b2\u0006\u0010\u001d\u001a\u00020\u0015J\u000e\u0010\u001e\u001a\u00020\u00072\u0006\u0010\u001f\u001a\u00020\u0007J\u000e\u0010 \u001a\u00020\u00152\u0006\u0010!\u001a\u00020\u0012J\u0006\u0010\"\u001a\u00020\u0005J\u0006\u0010#\u001a\u00020$J\u0006\u0010%\u001a\u00020\u0005J\u0018\u0010&\u001a\u00020\u00052\u0006\u0010!\u001a\u00020\u00122\u0006\u0010\u001f\u001a\u00020\u0007H\u0002J\u000e\u0010'\u001a\u00020$2\u0006\u0010!\u001a\u00020\u0012R\u000e\u0010\u000b\u001a\u00020\fX\u0082\u0004¢\u0006\u0002\n\u0000R\u0010\u0010\r\u001a\u00020\u000eX\u0082\u0004¢\u0006\u0004\n\u0002\u0010\u000fR\u0014\u0010\u0010\u001a\b\u0012\u0004\u0012\u00020\u00120\u0011X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0013\u001a\u00020\u0007X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0004\u001a\u00020\u0005X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006)"}, d2 = {"Lokhttp3/internal/connection/RealConnectionPool;", "", "taskRunner", "Lokhttp3/internal/concurrent/TaskRunner;", "maxIdleConnections", "", "keepAliveDuration", "", "timeUnit", "Ljava/util/concurrent/TimeUnit;", "(Lokhttp3/internal/concurrent/TaskRunner;IJLjava/util/concurrent/TimeUnit;)V", "cleanupQueue", "Lokhttp3/internal/concurrent/TaskQueue;", "cleanupTask", "okhttp3/internal/connection/RealConnectionPool$cleanupTask$1", "Lokhttp3/internal/connection/RealConnectionPool$cleanupTask$1;", "connections", "Ljava/util/concurrent/ConcurrentLinkedQueue;", "Lokhttp3/internal/connection/RealConnection;", "keepAliveDurationNs", "callAcquirePooledConnection", "", SentryLockReason.JsonKeys.ADDRESS, "Lokhttp3/Address;", NotificationCompat.CATEGORY_CALL, "Lokhttp3/internal/connection/RealCall;", "routes", "", "Lokhttp3/Route;", "requireMultiplexed", "cleanup", "now", "connectionBecameIdle", SentryOkHttpEventListener.CONNECTION_EVENT, "connectionCount", "evictAll", "", "idleConnectionCount", "pruneAndGetAllocationCount", "put", "Companion", "okhttp"}, k = 1, mv = {1, 4, 0}) /* loaded from: classes3.dex */ public final class RealConnectionPool { /* renamed from: Companion, reason: from kotlin metadata */ public static final Companion INSTANCE = new Companion(null); private final TaskQueue cleanupQueue; private final RealConnectionPool$cleanupTask$1 cleanupTask; private final ConcurrentLinkedQueue 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) { Intrinsics.checkNotNullParameter(taskRunner, "taskRunner"); Intrinsics.checkNotNullParameter(timeUnit, "timeUnit"); this.maxIdleConnections = i; this.keepAliveDurationNs = timeUnit.toNanos(j); this.cleanupQueue = taskRunner.newQueue(); final String str = Util.okHttpName + " ConnectionPool"; this.cleanupTask = new Task(str) { // from class: okhttp3.internal.connection.RealConnectionPool$cleanupTask$1 @Override // okhttp3.internal.concurrent.Task public long runOnce() { return RealConnectionPool.this.cleanup(System.nanoTime()); } }; this.connections = new ConcurrentLinkedQueue<>(); if (!(j > 0)) { throw new IllegalArgumentException(("keepAliveDuration <= 0: " + j).toString()); } } public final int idleConnectionCount() { boolean isEmpty; ConcurrentLinkedQueue concurrentLinkedQueue = this.connections; int i = 0; if (!(concurrentLinkedQueue instanceof Collection) || !concurrentLinkedQueue.isEmpty()) { for (RealConnection it : concurrentLinkedQueue) { Intrinsics.checkNotNullExpressionValue(it, "it"); synchronized (it) { isEmpty = it.getCalls().isEmpty(); } if (isEmpty && (i = i + 1) < 0) { CollectionsKt.throwCountOverflow(); } } } return i; } public final int connectionCount() { return this.connections.size(); } public final boolean callAcquirePooledConnection(Address address, RealCall call, List routes, boolean requireMultiplexed) { Intrinsics.checkNotNullParameter(address, "address"); Intrinsics.checkNotNullParameter(call, "call"); Iterator it = this.connections.iterator(); while (it.hasNext()) { RealConnection connection = it.next(); Intrinsics.checkNotNullExpressionValue(connection, "connection"); synchronized (connection) { if (requireMultiplexed) { if (!connection.isMultiplexed$okhttp()) { Unit unit = Unit.INSTANCE; } } if (connection.isEligible$okhttp(address, routes)) { call.acquireConnectionNoEvents(connection); return true; } Unit unit2 = Unit.INSTANCE; } } return false; } public final void evictAll() { Socket socket; Iterator it = this.connections.iterator(); Intrinsics.checkNotNullExpressionValue(it, "connections.iterator()"); while (it.hasNext()) { RealConnection connection = it.next(); Intrinsics.checkNotNullExpressionValue(connection, "connection"); synchronized (connection) { if (connection.getCalls().isEmpty()) { it.remove(); connection.setNoNewExchanges(true); socket = connection.socket(); } else { socket = null; } } if (socket != null) { Util.closeQuietly(socket); } } if (this.connections.isEmpty()) { this.cleanupQueue.cancelAll(); } } public final long cleanup(long now) { RealConnection realConnection = null; Iterator it = this.connections.iterator(); int i = 0; long j = Long.MIN_VALUE; int i2 = 0; while (it.hasNext()) { RealConnection connection = it.next(); Intrinsics.checkNotNullExpressionValue(connection, "connection"); synchronized (connection) { if (pruneAndGetAllocationCount(connection, now) > 0) { Integer.valueOf(i2); i2++; } else { i++; long idleAtNs = now - connection.getIdleAtNs(); if (idleAtNs > j) { Unit unit = Unit.INSTANCE; realConnection = connection; j = idleAtNs; } else { Unit unit2 = Unit.INSTANCE; } } } } long j2 = this.keepAliveDurationNs; if (j < j2 && i <= this.maxIdleConnections) { if (i > 0) { return j2 - j; } if (i2 > 0) { return j2; } return -1L; } Intrinsics.checkNotNull(realConnection); synchronized (realConnection) { if (!realConnection.getCalls().isEmpty()) { return 0L; } if (realConnection.getIdleAtNs() + j != now) { return 0L; } realConnection.setNoNewExchanges(true); this.connections.remove(realConnection); Util.closeQuietly(realConnection.socket()); if (this.connections.isEmpty()) { this.cleanupQueue.cancelAll(); } return 0L; } } /* compiled from: RealConnectionPool.kt */ @Metadata(bv = {1, 0, 3}, d1 = {"\u0000\u0018\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\b\u0086\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002J\u000e\u0010\u0003\u001a\u00020\u00042\u0006\u0010\u0005\u001a\u00020\u0006¨\u0006\u0007"}, d2 = {"Lokhttp3/internal/connection/RealConnectionPool$Companion;", "", "()V", "get", "Lokhttp3/internal/connection/RealConnectionPool;", "connectionPool", "Lokhttp3/ConnectionPool;", "okhttp"}, k = 1, mv = {1, 4, 0}) /* loaded from: classes3.dex */ public static final class Companion { private Companion() { } public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } public final RealConnectionPool get(ConnectionPool connectionPool) { Intrinsics.checkNotNullParameter(connectionPool, "connectionPool"); return connectionPool.getDelegate(); } } public final void put(RealConnection connection) { Intrinsics.checkNotNullParameter(connection, "connection"); if (!Util.assertionsEnabled || Thread.holdsLock(connection)) { this.connections.add(connection); TaskQueue.schedule$default(this.cleanupQueue, this.cleanupTask, 0L, 2, null); } else { StringBuilder sb = new StringBuilder("Thread "); Thread currentThread = Thread.currentThread(); Intrinsics.checkNotNullExpressionValue(currentThread, "Thread.currentThread()"); throw new AssertionError(sb.append(currentThread.getName()).append(" MUST hold lock on ").append(connection).toString()); } } public final boolean connectionBecameIdle(RealConnection connection) { Intrinsics.checkNotNullParameter(connection, "connection"); if (!Util.assertionsEnabled || Thread.holdsLock(connection)) { if (connection.getNoNewExchanges() || this.maxIdleConnections == 0) { connection.setNoNewExchanges(true); this.connections.remove(connection); 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 "); Thread currentThread = Thread.currentThread(); Intrinsics.checkNotNullExpressionValue(currentThread, "Thread.currentThread()"); throw new AssertionError(sb.append(currentThread.getName()).append(" MUST hold lock on ").append(connection).toString()); } private final int pruneAndGetAllocationCount(RealConnection connection, long now) { if (!Util.assertionsEnabled || Thread.holdsLock(connection)) { List> calls = connection.getCalls(); int i = 0; while (i < calls.size()) { Reference reference = calls.get(i); if (reference.get() != null) { i++; } else { if (reference == null) { throw new NullPointerException("null cannot be cast to non-null type okhttp3.internal.connection.RealCall.CallReference"); } Platform.INSTANCE.get().logCloseableLeak("A connection to " + connection.getRoute().address().url() + " was leaked. Did you forget to close a response body?", ((RealCall.CallReference) reference).getCallStackTrace()); calls.remove(i); connection.setNoNewExchanges(true); if (calls.isEmpty()) { connection.setIdleAtNs$okhttp(now - this.keepAliveDurationNs); return 0; } } } return calls.size(); } StringBuilder sb = new StringBuilder("Thread "); Thread currentThread = Thread.currentThread(); Intrinsics.checkNotNullExpressionValue(currentThread, "Thread.currentThread()"); throw new AssertionError(sb.append(currentThread.getName()).append(" MUST hold lock on ").append(connection).toString()); } }