package okhttp3.internal.connection;

import com.google.android.recaptcha.internal.a;
import java.lang.ref.Reference;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import kotlin.jvm.internal.o;
import okhttp3.Address;
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;

/* JADX INFO: loaded from: classes3.dex */
public final class RealConnectionPool {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final int f22357a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final long f22358b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final TaskQueue f22359c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final RealConnectionPool$cleanupTask$1 f22360d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final ConcurrentLinkedQueue f22361e;

    public static final class Companion {
        public /* synthetic */ Companion(int i6) {
            this();
        }

        private Companion() {
        }
    }

    static {
        new Companion(0);
    }

    /* JADX WARN: Type inference failed for: r4v2, types: [okhttp3.internal.connection.RealConnectionPool$cleanupTask$1] */
    public RealConnectionPool(TaskRunner taskRunner, int i6, long j4) {
        TimeUnit timeUnit = TimeUnit.MINUTES;
        o.h(taskRunner, "taskRunner");
        o.h(timeUnit, "timeUnit");
        this.f22357a = i6;
        this.f22358b = timeUnit.toNanos(j4);
        this.f22359c = taskRunner.e();
        final String strN = a.n(new StringBuilder(), Util.f22185g, " ConnectionPool");
        this.f22360d = new Task(strN) { // from class: okhttp3.internal.connection.RealConnectionPool$cleanupTask$1
            @Override // okhttp3.internal.concurrent.Task
            public final long a() {
                RealConnectionPool realConnectionPool = this.f22362e;
                long jNanoTime = System.nanoTime();
                int i7 = 0;
                long j6 = Long.MIN_VALUE;
                RealConnection realConnection = null;
                int i8 = 0;
                for (RealConnection connection : realConnectionPool.f22361e) {
                    o.g(connection, "connection");
                    synchronized (connection) {
                        if (realConnectionPool.b(connection, jNanoTime) > 0) {
                            i8++;
                        } else {
                            i7++;
                            long j7 = jNanoTime - connection.f22351q;
                            if (j7 > j6) {
                                realConnection = connection;
                                j6 = j7;
                            }
                        }
                    }
                }
                long j8 = realConnectionPool.f22358b;
                if (j6 < j8 && i7 <= realConnectionPool.f22357a) {
                    if (i7 > 0) {
                        return j8 - j6;
                    }
                    if (i8 > 0) {
                        return j8;
                    }
                    return -1L;
                }
                o.e(realConnection);
                synchronized (realConnection) {
                    if (!realConnection.f22350p.isEmpty()) {
                        return 0L;
                    }
                    if (realConnection.f22351q + j6 != jNanoTime) {
                        return 0L;
                    }
                    realConnection.f22345j = true;
                    realConnectionPool.f22361e.remove(realConnection);
                    Socket socket = realConnection.f22339d;
                    o.e(socket);
                    Util.d(socket);
                    if (realConnectionPool.f22361e.isEmpty()) {
                        realConnectionPool.f22359c.a();
                    }
                    return 0L;
                }
            }
        };
        this.f22361e = new ConcurrentLinkedQueue();
        if (j4 <= 0) {
            throw new IllegalArgumentException(A0.a.m("keepAliveDuration <= 0: ", j4).toString());
        }
    }

    public final boolean a(Address address, RealCall call, ArrayList arrayList, boolean z6) {
        o.h(call, "call");
        Iterator it = this.f22361e.iterator();
        while (true) {
            if (!it.hasNext()) {
                return false;
            }
            RealConnection connection = (RealConnection) it.next();
            o.g(connection, "connection");
            synchronized (connection) {
                if (z6) {
                    if (!(connection.f22342g != null)) {
                        continue;
                    }
                }
                if (connection.h(address, arrayList)) {
                    call.b(connection);
                    return true;
                }
            }
        }
    }

    public final int b(RealConnection realConnection, long j4) {
        byte[] bArr = Util.f22179a;
        ArrayList arrayList = realConnection.f22350p;
        int i6 = 0;
        while (i6 < arrayList.size()) {
            Reference reference = (Reference) arrayList.get(i6);
            if (reference.get() != null) {
                i6++;
            } else {
                Platform.Companion.get().logCloseableLeak("A connection to " + realConnection.f22337b.f22168a.f21896h + " was leaked. Did you forget to close a response body?", ((RealCall.CallReference) reference).f22335a);
                arrayList.remove(i6);
                realConnection.f22345j = true;
                if (arrayList.isEmpty()) {
                    realConnection.f22351q = j4 - this.f22358b;
                    return 0;
                }
            }
        }
        return arrayList.size();
    }
}
