// 最大支持同时请求数 private int maxRequests = 64; // 单个域名最大请求数 private int maxRequestsPerHost = 5;
// 请求线程池 private ExecutorService executorService;
// 等待执行的异步请求 private final Deque<AsyncCall> readyAsyncCalls = new ArrayDeque<>();
// 正在执行的异步请求 private final Deque<AsyncCall> runningAsyncCalls = new ArrayDeque<>();
// 正在执行的同步请求 private final Deque<RealCall> runningSyncCalls = new ArrayDeque<>();
例:更改同时支持最大请求数和单个域名同时最大请求数:
1 2 3 4 5 6
Dispatcher dispatcher = new Dispatcher(); dispatcher.setMaxRequests(20); dispatcher.setMaxRequestsPerHost(3); httpClient = new OkHttpClient.Builder() .dispatcher(dispatcher) .build();
Protocol 默认支持的请求协议
Protocol.HTTP_2
Protocol.SPDY_3
Protocol.HTTP_1_1
ConnectionPool Socket链接池
1 2 3 4 5 6 7 8
// 最大缓存socket链接个数,默认5 private final int maxIdleConnections; // 空闲回收时间,默认 5分钟 private final long keepAliveDurationNs; // 缓存socket链接 private final Deque<RealConnection> connections = new ArrayDeque<>(); // 用于网络请求异常时自动切换请求ip final RouteDatabase routeDatabase = new RouteDatabase();