OkHttp3.14 源码剖析系列(四)——连接的建立概述

本文是OkHttp3.14源码剖析系列的第四篇,主要介绍了连接建立的大体流程,包括获取连接、判断连接是否可用等。详细分析了寻找可用连接的步骤,如从连接池获取、路由选择和创建新连接。同时,解释了Exchange、ExchangeCodec和Transmitter在请求和响应过程中的作用。
摘要由CSDN通过智能技术生成

个人博客:https://blog.N0tExpectErr0r.cn

小专栏:https://xiaozhuanlan.com/N0tExpectErr0r

前面的文章分析完了 OkHttp 中的缓存机制,现在让我们继续来研究其在 ConnectInterceptor 中所进行的连接建立的相关原理。由于连接建立的过程涉及到很多在 OkHttp 中非常重要的机制,因此将分为多篇文章进行介绍,这篇文章主要是对连接建立的大体流程进行介绍。

系列索引

本系列文章基于 OkHttp3.14

OkHttp3.14 源码剖析系列(一)——请求的发起及拦截器机制概述

OkHttp3.14 源码剖析系列(二)——拦截器大体流程分析

OkHttp3.14 源码剖析系列(三)——缓存机制分析

OkHttp3.14 源码剖析系列(四)——连接的建立概述

OkHttp3.14 源码剖析系列(五)——路由选择机制

OkHttp3.14 源码剖析系列(六)——连接复用机制及连接的建立

OkHttp3.14 源码剖析系列(七)——请求的发起及响应的读取

连接建立流程概述

ConnectInterceptor.intercept 方法中真正实现了连接的建立的代码如下:

// 如果请求是GET格式,需要一些额外的检查
boolean doExtensiveHealthChecks = !request.method().equals("GET");
Exchange exchange = transmitter.newExchange(chain, doExtensiveHealthChecks);

根据上面的代码我们可以推测,这个 Exchange 类与我们的连接是有一些关系的,真正连接的建立过程在 transmitter.newExchange 中实现。

我们看到 transmitter.newExchange 方法:

/**
 * Returns a new exchange to carry a new request and response.
 */
Exchange newExchange(Interceptor.Chain chain, boolean doExtensiveHealthChecks) {
   
    synchronized (connectionPool) {
   
        if (noMoreExchanges) {
   
            throw new IllegalStateException("released");
        }
        if (exchange != null) {
   
            throw new IllegalStateException("cannot make a new request because the previous response "
                    + "is still open: please call response.close()");
        }
    }
    // 寻找ExchangeCodec对象
    ExchangeCodec codec = exchangeFinder.find(client, chain, doExtensiveHealthChecks);
    // 通过找到的codec对象构建Exchange对象
    Exchange result = new Exchange(this, call, eventListener, exchangeFinder, codec);
   	// 进行一些变量的赋值
   	synchronized (connectionPool) {
   
        this.exchange = result;
        this.exchangeRequestDone = false;
        this.exchangeResponseDone = false;
        return result;
    }
}

获取连接

上面首先通过 exchangeFinder.find 方法进行了对 ExchangeCodec 的查找,找到对应的 ExchangeCodec 对象,之后通过这个 codec 对象构建了一个 Exchange 对象并返回

那么什么是 ExchangeCodec 对象呢?我们先看到 exchangeFinder.find 方法:

public ExchangeCodec find(
        OkHttpClient client, Interceptor.Chain chain, boolean doExtensiveHealthChecks) {
   
    int connectTimeout = chain.connectTimeoutMillis();
    int readTimeout = chain.readTimeoutMillis();
    int writeTimeout = chain.writeTimeoutMillis();
    int pingIntervalMillis = client.pingIntervalMillis();
    boolean connectionRetryEnabled = client.retryOnConnectionFailure();
    try {
   
        RealConnection resultConnection = findHealthyConnection(connectTimeout, readTimeout,
                writeTimeout, pingIntervalMillis, connectionRetryEnabled, doExtensiveHealthChecks);
        return resultConnection.newCodec(client, chain);
    } catch (RouteException e) 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值