package okhttp3; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; import java.util.Iterator; import java.util.concurrent.ExecutorService; import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import okhttp3.RealCall; import okhttp3.internal.Util; /* loaded from: classes5.dex */ public final class Dispatcher { public ExecutorService executorService; public Runnable idleCallback; public int maxRequests = 64; public int maxRequestsPerHost = 5; public final Deque readyAsyncCalls = new ArrayDeque(); public final Deque runningAsyncCalls = new ArrayDeque(); public final Deque runningSyncCalls = new ArrayDeque(); public synchronized ExecutorService executorService() { try { if (this.executorService == null) { this.executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue(), Util.threadFactory("OkHttp Dispatcher", false)); } } catch (Throwable th) { throw th; } return this.executorService; } public void enqueue(RealCall.AsyncCall asyncCall) { RealCall.AsyncCall findExistingCallWithHost; synchronized (this) { try { this.readyAsyncCalls.add(asyncCall); if (!asyncCall.get().forWebSocket && (findExistingCallWithHost = findExistingCallWithHost(asyncCall.host())) != null) { asyncCall.reuseCallsPerHostFrom(findExistingCallWithHost); } } catch (Throwable th) { throw th; } } promoteAndExecute(); } public final RealCall.AsyncCall findExistingCallWithHost(String str) { for (RealCall.AsyncCall asyncCall : this.runningAsyncCalls) { if (asyncCall.host().equals(str)) { return asyncCall; } } for (RealCall.AsyncCall asyncCall2 : this.readyAsyncCalls) { if (asyncCall2.host().equals(str)) { return asyncCall2; } } return null; } public final boolean promoteAndExecute() { int i; boolean z; ArrayList arrayList = new ArrayList(); synchronized (this) { try { Iterator it = this.readyAsyncCalls.iterator(); while (it.hasNext()) { RealCall.AsyncCall asyncCall = (RealCall.AsyncCall) it.next(); if (this.runningAsyncCalls.size() >= this.maxRequests) { break; } if (asyncCall.callsPerHost().get() < this.maxRequestsPerHost) { it.remove(); asyncCall.callsPerHost().incrementAndGet(); arrayList.add(asyncCall); this.runningAsyncCalls.add(asyncCall); } } z = runningCallsCount() > 0; } catch (Throwable th) { throw th; } } int size = arrayList.size(); for (i = 0; i < size; i++) { ((RealCall.AsyncCall) arrayList.get(i)).executeOn(executorService()); } return z; } public synchronized void executed(RealCall realCall) { this.runningSyncCalls.add(realCall); } public void finished(RealCall.AsyncCall asyncCall) { asyncCall.callsPerHost().decrementAndGet(); finished(this.runningAsyncCalls, asyncCall); } public void finished(RealCall realCall) { finished(this.runningSyncCalls, realCall); } public final void finished(Deque deque, Object obj) { Runnable runnable; synchronized (this) { if (!deque.remove(obj)) { throw new AssertionError("Call wasn't in-flight!"); } runnable = this.idleCallback; } if (promoteAndExecute() || runnable == null) { return; } runnable.run(); } public synchronized int runningCallsCount() { return this.runningAsyncCalls.size() + this.runningSyncCalls.size(); } }