OkHttpClient参数设置

OkHttpClient.Builder默认参数

public Builder() {
    // dispatch。负责分配处理异步任务
    this.dispatcher = new Dispatcher();
    // 支持的协议,默认支持http2和http1.1
    this.protocols = OkHttpClient.DEFAULT_PROTOCOLS;
    // 指定socket链接配置。默认:MODERN_TLS和CLEARTEXT
    this.connectionSpecs = OkHttpClient.DEFAULT_CONNECTION_SPECS;
    // 监听http请求过程中涉及的各种事件。包括:开始请求、dns解析开始/结束、链接开始等等。默认是的Listener
    this.eventListenerFactory = EventListener.factory(EventListener.NONE);
    // 设置代理
    this.proxySelector = ProxySelector.getDefault();
    // 设置cookie
    this.cookieJar = CookieJar.NO_COOKIES;
    // 设置创建Socket链接的Factory
    this.socketFactory = SocketFactory.getDefault();
    // 验证域名和证书的域名是否匹配
    this.hostnameVerifier = OkHostnameVerifier.INSTANCE;
    // 设置信任的证书,不信任之外的证书,默认是空的
    this.certificatePinner = CertificatePinner.DEFAULT;
    // 与身份认证有关。详见:https://square.github.io/okhttp/3.x/okhttp/okhttp3/Authenticator.html
    this.proxyAuthenticator = Authenticator.NONE;
    // 与身份认证有关。详见:https://square.github.io/okhttp/3.x/okhttp/okhttp3/Authenticator.html
    this.authenticator = Authenticator.NONE;
    // 连接池-缓存链接和清理空闲的连接
    this.connectionPool = new ConnectionPool();
    // 域名解析
    this.dns = Dns.SYSTEM;
    // 是否支持ssl重定向
    this.followSslRedirects = true;
    // 是否支持重定向
    this.followRedirects = true;
    // 失败后是否重试
    this.retryOnConnectionFailure = true;
    // 链接超时时间
    this.connectTimeout = 10000;
    // 读超时时间
    this.readTimeout = 10000;
    // 写超时时间
    this.writeTimeout = 10000;
    // ping的时间间隔。如果使用WebSocket请求设置pingInterval,以保活
    this.pingInterval = 0;
}

Dispatcher-任务调度器

public final class Dispatcher {
    // 最大的异步请求数
    private int maxRequests = 64;
    // 单个host最大的请求数
    private int maxRequestsPerHost = 5;
    @Nullable
    private Runnable idleCallback;
    @Nullable
    // 执行异步请求的线程池
    private ExecutorService executorService;
    // 尚未执行的请求
    private final Deque<AsyncCall> readyAsyncCalls = new ArrayDeque();
    // 正在执行的异步请求
    private final Deque<AsyncCall> runningAsyncCalls = new ArrayDeque();
    // 正在执行的同步请求
    private final Deque<RealCall> runningSyncCalls = new ArrayDeque();
    // 设置异步线程池
    public Dispatcher(ExecutorService executorService) {
        this.executorService = executorService;
    }
    public Dispatcher() {
    }
    public synchronized ExecutorService executorService() {
        if (this.executorService == null) {
            // 默认的异步线程池
            this.executorService = new ThreadPoolExecutor(0, 2147483647, 60L, TimeUnit.SECONDS, new SynchronousQueue(), Util.threadFactory("OkHttp Dispatcher", false));
        }
        return this.executorService;
    }
    // 异步任务,判断异步任务数和单个host的请求数
    synchronized void enqueue(AsyncCall call) {
        if (this.runningAsyncCalls.size() < this.maxRequests && this.runningCallsForHost(call) < this.maxRequestsPerHost) {
            this.runningAsyncCalls.add(call);
            this.executorService().execute(call);
        } else {
            this.readyAsyncCalls.add(call);
        }
    }
}

ConnectionTool-缓存链接和清理空闲的连接

public final class ConnectionPool {
    // 负责清理空闲连接的线程池
    private static final Executor executor;
    // 最大空闲的连接数-默认值:5
    private final int maxIdleConnections;
    // 最大的空闲时间-默认值:5分钟
    private final long keepAliveDurationNs;
    // 负责清理空闲连接的Runnable
    private final Runnable cleanupRunnable;
    // 所有的连接
    private final Deque<RealConnection> connections;
    final RouteDatabase routeDatabase;
    boolean cleanupRunning;
    
    public ConnectionPool() {
        this(5, 5L, TimeUnit.MINUTES);
    }
    
    public ConnectionPool(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) {
        ////
    }
    
    static {
        // 负责清理空闲连接的线程池
        executor = new ThreadPoolExecutor(0, 2147483647, 60L, TimeUnit.SECONDS, new SynchronousQueue(), Util.threadFactory("OkHttp ConnectionPool", true));
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容