package XnFJ5_9.Ft6VRog_36.FSaI4_7;

import XnFJ5_9.Ft6VRog_36.FSaI4_7.Nm6q_10;
import XnFJ5_9.JXypC6_34;
import java.io.IOException;
import java.lang.ref.Reference;
import java.net.Proxy;
import java.util.ArrayDeque;
import java.util.Deque;
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 javax.annotation.Nullable;
/* compiled from: RealConnectionPool.java */
/* loaded from: classes.dex */
public final class IMuAHx_6 {
    public static final Executor KQgRr5e_0 = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS, new SynchronousQueue(), XnFJ5_9.Ft6VRog_36.X6FRs_4.KQgRr5e_0("OkHttp ConnectionPool", true));

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final int f1458KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final long f1459KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public boolean f1463KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final Runnable f1461KQgRr5e_0 = new Runnable() { // from class: XnFJ5_9.Ft6VRog_36.FSaI4_7.KQgRr5e_0
        @Override // java.lang.Runnable
        public final void run() {
            IMuAHx_6.this.KQgRr5e_0();
        }
    };

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final Deque<IFxWkV_5> f1462KQgRr5e_0 = new ArrayDeque();

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final FSaI4_7 f1460KQgRr5e_0 = new FSaI4_7();

    public IMuAHx_6(int i, long j, TimeUnit timeUnit) {
        this.f1458KQgRr5e_0 = i;
        this.f1459KQgRr5e_0 = timeUnit.toNanos(j);
        if (j > 0) {
            return;
        }
        throw new IllegalArgumentException("keepAliveDuration <= 0: " + j);
    }

    public /* synthetic */ void KQgRr5e_0() {
        while (true) {
            long KQgRr5e_02 = KQgRr5e_0(System.nanoTime());
            if (KQgRr5e_02 == -1) {
                return;
            }
            if (KQgRr5e_02 > 0) {
                long j = KQgRr5e_02 / 1000000;
                long j2 = KQgRr5e_02 - (1000000 * j);
                synchronized (this) {
                    try {
                        wait(j, (int) j2);
                    } catch (InterruptedException unused) {
                    }
                }
            }
        }
    }

    public boolean KQgRr5e_0(XnFJ5_9.X6FRs_4 x6FRs_4, Nm6q_10 nm6q_10, @Nullable List<JXypC6_34> list, boolean z) {
        for (IFxWkV_5 iFxWkV_5 : this.f1462KQgRr5e_0) {
            if (!z || iFxWkV_5.m674KQgRr5e_0()) {
                if (iFxWkV_5.KQgRr5e_0(x6FRs_4, list)) {
                    nm6q_10.KQgRr5e_0(iFxWkV_5);
                    return true;
                }
            }
        }
        return false;
    }

    public void KQgRr5e_0(IFxWkV_5 iFxWkV_5) {
        if (!this.f1463KQgRr5e_0) {
            this.f1463KQgRr5e_0 = true;
            KQgRr5e_0.execute(this.f1461KQgRr5e_0);
        }
        this.f1462KQgRr5e_0.add(iFxWkV_5);
    }

    /* renamed from: KQgRr5e_0  reason: collision with other method in class */
    public boolean m675KQgRr5e_0(IFxWkV_5 iFxWkV_5) {
        if (!iFxWkV_5.f1456KQgRr5e_0 && this.f1458KQgRr5e_0 != 0) {
            notifyAll();
            return false;
        }
        this.f1462KQgRr5e_0.remove(iFxWkV_5);
        return true;
    }

    public long KQgRr5e_0(long j) {
        synchronized (this) {
            IFxWkV_5 iFxWkV_5 = null;
            long j2 = Long.MIN_VALUE;
            int i = 0;
            int i2 = 0;
            for (IFxWkV_5 iFxWkV_52 : this.f1462KQgRr5e_0) {
                if (KQgRr5e_0(iFxWkV_52, j) > 0) {
                    i2++;
                } else {
                    i++;
                    long j3 = j - iFxWkV_52.f1446KQgRr5e_0;
                    if (j3 > j2) {
                        iFxWkV_5 = iFxWkV_52;
                        j2 = j3;
                    }
                }
            }
            if (j2 < this.f1459KQgRr5e_0 && i <= this.f1458KQgRr5e_0) {
                if (i > 0) {
                    return this.f1459KQgRr5e_0 - j2;
                } else if (i2 > 0) {
                    return this.f1459KQgRr5e_0;
                } else {
                    this.f1463KQgRr5e_0 = false;
                    return -1L;
                }
            }
            this.f1462KQgRr5e_0.remove(iFxWkV_5);
            XnFJ5_9.Ft6VRog_36.X6FRs_4.KQgRr5e_0(iFxWkV_5.m672KQgRr5e_0());
            return 0L;
        }
    }

    public final int KQgRr5e_0(IFxWkV_5 iFxWkV_5, long j) {
        List<Reference<Nm6q_10>> list = iFxWkV_5.f1455KQgRr5e_0;
        int i = 0;
        while (i < list.size()) {
            Reference<Nm6q_10> reference = list.get(i);
            if (reference.get() != null) {
                i++;
            } else {
                XnFJ5_9.Ft6VRog_36.KNBe_11.X6FRs_4.QxJ9d_1().KQgRr5e_0("A connection to " + iFxWkV_5.m671KQgRr5e_0().KQgRr5e_0().KQgRr5e_0() + " was leaked. Did you forget to close a response body?", ((Nm6q_10.QxJ9d_1) reference).KQgRr5e_0);
                list.remove(i);
                iFxWkV_5.f1456KQgRr5e_0 = true;
                if (list.isEmpty()) {
                    iFxWkV_5.f1446KQgRr5e_0 = j - this.f1459KQgRr5e_0;
                    return 0;
                }
            }
        }
        return list.size();
    }

    public void KQgRr5e_0(JXypC6_34 jXypC6_34, IOException iOException) {
        if (jXypC6_34.m760KQgRr5e_0().type() != Proxy.Type.DIRECT) {
            XnFJ5_9.X6FRs_4 KQgRr5e_02 = jXypC6_34.KQgRr5e_0();
            KQgRr5e_02.m807KQgRr5e_0().connectFailed(KQgRr5e_02.KQgRr5e_0().m748KQgRr5e_0(), jXypC6_34.m760KQgRr5e_0().address(), iOException);
        }
        this.f1460KQgRr5e_0.QxJ9d_1(jXypC6_34);
    }
}
