package g;

import com.seewo.vcommons.constants.Platform;
import g.i0.f.g;
import java.lang.ref.Reference;
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;

/* JADX INFO: loaded from: classes.dex */
public final class h {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final Executor f6118a;

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final Runnable f6121d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final Deque<g.i0.f.c> f6122e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final g.i0.f.d f6123f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public boolean f6124g;

    public class a implements Runnable {
        public a() {
        }

        @Override // java.lang.Runnable
        public void run() {
            long j2;
            while (true) {
                h hVar = h.this;
                long jNanoTime = System.nanoTime();
                synchronized (hVar) {
                    g.i0.f.c cVar = null;
                    long j3 = Long.MIN_VALUE;
                    int i2 = 0;
                    int i3 = 0;
                    for (g.i0.f.c cVar2 : hVar.f6122e) {
                        if (hVar.a(cVar2, jNanoTime) > 0) {
                            i3++;
                        } else {
                            i2++;
                            long j4 = jNanoTime - cVar2.o;
                            if (j4 > j3) {
                                cVar = cVar2;
                                j3 = j4;
                            }
                        }
                    }
                    j2 = hVar.f6120c;
                    if (j3 >= j2 || i2 > hVar.f6119b) {
                        hVar.f6122e.remove(cVar);
                        g.i0.c.e(cVar.f6162e);
                        j2 = 0;
                    } else if (i2 > 0) {
                        j2 -= j3;
                    } else if (i3 <= 0) {
                        hVar.f6124g = false;
                        j2 = -1;
                    }
                }
                if (j2 == -1) {
                    return;
                }
                if (j2 > 0) {
                    long j5 = j2 / 1000000;
                    long j6 = j2 - (1000000 * j5);
                    synchronized (h.this) {
                        try {
                            h.this.wait(j5, (int) j6);
                        } catch (InterruptedException unused) {
                        }
                    }
                }
            }
        }
    }

    static {
        TimeUnit timeUnit = TimeUnit.SECONDS;
        SynchronousQueue synchronousQueue = new SynchronousQueue();
        byte[] bArr = g.i0.c.f6140a;
        f6118a = new ThreadPoolExecutor(0, Platform.UNKNOWN, 60L, timeUnit, synchronousQueue, new g.i0.d("OkHttp ConnectionPool", true));
    }

    public h() {
        TimeUnit timeUnit = TimeUnit.MINUTES;
        this.f6121d = new a();
        this.f6122e = new ArrayDeque();
        this.f6123f = new g.i0.f.d();
        this.f6119b = 5;
        this.f6120c = timeUnit.toNanos(5L);
    }

    public final int a(g.i0.f.c cVar, long j2) {
        List<Reference<g.i0.f.g>> list = cVar.n;
        int i2 = 0;
        while (i2 < list.size()) {
            Reference<g.i0.f.g> reference = list.get(i2);
            if (reference.get() != null) {
                i2++;
            } else {
                StringBuilder sbC = a.b.c.a.a.C("A connection to ");
                sbC.append(cVar.f6160c.f6115a.f6048a);
                sbC.append(" was leaked. Did you forget to close a response body?");
                g.i0.j.f.f6405a.m(sbC.toString(), ((g.a) reference).f6191a);
                list.remove(i2);
                cVar.k = true;
                if (list.isEmpty()) {
                    cVar.o = j2 - this.f6120c;
                    return 0;
                }
            }
        }
        return list.size();
    }
}
