package okhttp3; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collections; import java.util.Deque; import java.util.Iterator; import java.util.List; 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: classes2.dex */ public final class Dispatcher { private ExecutorService executorService; private Runnable idleCallback; private int maxRequests = 64; private int maxRequestsPerHost = 5; private final Deque readyAsyncCalls = new ArrayDeque(); private final Deque runningAsyncCalls = new ArrayDeque(); private final Deque runningSyncCalls = new ArrayDeque(); public Dispatcher(ExecutorService executorService) { this.executorService = executorService; } private void promoteCalls() { if (this.runningAsyncCalls.size() < this.maxRequests && !this.readyAsyncCalls.isEmpty()) { Iterator it = this.readyAsyncCalls.iterator(); while (it.hasNext()) { RealCall.AsyncCall next = it.next(); if (runningCallsForHost(next) < this.maxRequestsPerHost) { it.remove(); this.runningAsyncCalls.add(next); executorService().execute(next); } if (this.runningAsyncCalls.size() >= this.maxRequests) { return; } } } } private int runningCallsForHost(RealCall.AsyncCall asyncCall) { int i = 0; for (RealCall.AsyncCall asyncCall2 : this.runningAsyncCalls) { if (!asyncCall2.get().forWebSocket && asyncCall2.host().equals(asyncCall.host())) { i++; } } return i; } public synchronized void cancelAll() { Iterator it = this.readyAsyncCalls.iterator(); while (it.hasNext()) { it.next().get().cancel(); } Iterator it2 = this.runningAsyncCalls.iterator(); while (it2.hasNext()) { it2.next().get().cancel(); } Iterator it3 = this.runningSyncCalls.iterator(); while (it3.hasNext()) { it3.next().cancel(); } } synchronized void enqueue(RealCall.AsyncCall asyncCall) { if (this.runningAsyncCalls.size() >= this.maxRequests || runningCallsForHost(asyncCall) >= this.maxRequestsPerHost) { this.readyAsyncCalls.add(asyncCall); } else { this.runningAsyncCalls.add(asyncCall); executorService().execute(asyncCall); } } synchronized void executed(RealCall realCall) { this.runningSyncCalls.add(realCall); } public synchronized ExecutorService executorService() { if (this.executorService == null) { this.executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue(), Util.threadFactory("OkHttp Dispatcher", false)); } return this.executorService; } void finished(RealCall.AsyncCall asyncCall) { finished(this.runningAsyncCalls, asyncCall, true); } public synchronized int getMaxRequests() { return this.maxRequests; } public synchronized int getMaxRequestsPerHost() { return this.maxRequestsPerHost; } public synchronized List queuedCalls() { ArrayList arrayList; arrayList = new ArrayList(); Iterator it = this.readyAsyncCalls.iterator(); while (it.hasNext()) { arrayList.add(it.next().get()); } return Collections.unmodifiableList(arrayList); } public synchronized int queuedCallsCount() { return this.readyAsyncCalls.size(); } public synchronized List runningCalls() { ArrayList arrayList; arrayList = new ArrayList(); arrayList.addAll(this.runningSyncCalls); Iterator it = this.runningAsyncCalls.iterator(); while (it.hasNext()) { arrayList.add(it.next().get()); } return Collections.unmodifiableList(arrayList); } public synchronized int runningCallsCount() { return this.runningAsyncCalls.size() + this.runningSyncCalls.size(); } public synchronized void setIdleCallback(Runnable runnable) { this.idleCallback = runnable; } public synchronized void setMaxRequests(int i) { if (i < 1) { throw new IllegalArgumentException("max < 1: " + i); } this.maxRequests = i; promoteCalls(); } public synchronized void setMaxRequestsPerHost(int i) { if (i < 1) { throw new IllegalArgumentException("max < 1: " + i); } this.maxRequestsPerHost = i; promoteCalls(); } void finished(RealCall realCall) { finished(this.runningSyncCalls, realCall, false); } private void finished(Deque deque, T t, boolean z) { int runningCallsCount; Runnable runnable; synchronized (this) { if (deque.remove(t)) { if (z) { promoteCalls(); } runningCallsCount = runningCallsCount(); runnable = this.idleCallback; } else { throw new AssertionError("Call wasn't in-flight!"); } } if (runningCallsCount != 0 || runnable == null) { return; } runnable.run(); } public Dispatcher() { } }