OkHttp的原理解析

前言:

OKHttp是square出品的开源通信框架,也是我们安卓开发目前用的最多的一套网络框架。本文我想从OKHttp的基本使用开始讲起,这里将涉及到这么几个类:OKHttpClient,Request,Response,RealCall以及Dispatcher。

配置OKHttpClient

首先我们需要配置一个OKHttpClient,这里简单的创建个。

 val builder = OkHttpClient.Builder();
 val client = builder.build();

首先我们创建了一个builder,初始化了一些属性值。

OkHttpClient.Builder

    public Builder() {
      dispatcher = new Dispatcher();
      protocols = DEFAULT_PROTOCOLS;
      connectionSpecs = DEFAULT_CONNECTION_SPECS;
      eventListenerFactory = EventListener.factory(EventListener.NONE);
      proxySelector = ProxySelector.getDefault();
      cookieJar = CookieJar.NO_COOKIES;
      socketFactory = SocketFactory.getDefault();
      hostnameVerifier = OkHostnameVerifier.INSTANCE;
      certificatePinner = CertificatePinner.DEFAULT;
      proxyAuthenticator = Authenticator.NONE;
      authenticator = Authenticator.NONE;
      connectionPool = new ConnectionPool();
      dns = Dns.SYSTEM;
      followSslRedirects = true;
      followRedirects = true;
      retryOnConnectionFailure = true;
      connectTimeout = 10_000;
      readTimeout = 10_000;
      writeTimeout = 10_000;
      pingInterval = 0;
    }

接着我们调用了builder的build方法,build代码追进去我们看到它创建并返回了一个OkHttpClient对象。

  OkHttpClient(Builder builder) {
    this.dispatcher = builder.dispatcher;
    this.proxy = builder.proxy;
    this.protocols = builder.protocols;
    this.connectionSpecs = builder.connectionSpecs;
    this.interceptors = Util.immutableList(builder.interceptors);
    this.networkInterceptors = Util.immutableList(builder.networkInterceptors);
    this.eventListenerFactory = builder.eventListenerFactory;
    this.proxySelector = builder.proxySelector;
    this.cookieJar = builder.cookieJar;
    this.cache = builder.cache;
    this.internalCache = builder.internalCache;
    this.socketFactory = builder.socketFactory;

    boolean isTLS = false;
    for (ConnectionSpec spec : connectionSpecs) {
      isTLS = isTLS || spec.isTls();
    }

    if (builder.sslSocketFactory != null || !isTLS) {
      this.sslSocketFactory = builder.sslSocketFactory;
      this.certificateChainCleaner = builder.certificateChainCleaner;
    } else {
      X509TrustManager trustManager = systemDefaultTrustManager();
      this.sslSocketFactory = systemDefaultSslSocketFactory(trustManager);
      this.certificateChainCleaner = CertificateChainCleaner.get(trustManager);
    }

    this.hostnameVerifier = builder.hostnameVerifier;
    this.certificatePinner = builder.certificatePinner.withCertificateChainCleaner(
        certificateChainCleaner);
    this.proxyAuthenticator = builder.proxyAuthenticator;
    this.authenticator = builder.authenticator;
    this.connectionPool = builder.connectionPool;
    this.dns = builder.dns;
    this.followSslRedirects = builder.followSslRedirects;
    this.followRedirects = builder.followRedirects;
    this.retryOnConnectionFailure = builder.retryOnConnectionFailure;
    this.connectTimeout = builder.connectTimeout;
    this.readTimeout = builder.readTimeout;
    this.writeTimeout = builder.writeTimeout;
    this.pingInterval = builder.pingInterval;
  }

从代码中我们不难看出里面有很多属性值给我们去设置,可扩展性很强,这也是我们为什么用OKHttp的原因之一。到了这里OkHttpClient创建部分就完结了。

发送请求

请求分两种,同步请求和异步请求,同步请求需要在子线程执行。

第一种同步的方式:

        val builder = Request.Builder()
        val request = builder.url("https://www.baidu.com").build()
        val newCall = client.newCall(request)
            service.execute {
                val response = newCall.execute()
            }

Request.Builder

  public static class Builder {
    HttpUrl url;
    String method;
    Headers.Builder headers;
    RequestBody body;
    Object tag;

    public Builder() {
      this.method = "GET";
      this.headers = new Headers.Builder();
      }
       ...省略
    }

从源码中我们可以看出,默认构造的是get请求,以及属性有url、method、headers、body、tag。
接着我们调用了Request#Builder的build方法,build代码追进去我们看到它创建并返回了一个Request对象。

  Request(Builder builder) {
    this.url = builder.url;
    this.method = builder.method;
    this.headers = builder.headers.build();
    this.body = builder.body;
    this.tag = builder.tag != null ? builder.tag : this;
  }

和之前的一样,也是初始化了一些属性值。

newCall

  @Override public Call newCall(Request request) {
    return new RealCall(this, request, false /* for web socket */);
  }

这就返回了我们另外一个主角RealCall,

RealCall

  RealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {
    final EventListener.Factory eventListenerFactory = client.eventListenerFactory();

    this.client = client;
    this.originalRequest = originalRequest;
    this.forWebSocket = forWebSocket;
    this.retryAndFollowUpInterceptor = new RetryAndFollowUpInterceptor(client, forWebSocket);

    // TODO(jwilson): this is unsafe publication and not threadsafe.
    this.eventListener = eventListenerFactory.create(this);
  }

这里主要包含原始的request,以及事件监听。这里想先小小总结下:
OkhttpClient可以理解为整个Okhttp框架的工具类。通过okhttp来进行网络请求,每个请求都会封装成一个Call任务。OkhttpClient的主要目的包括将请求封装为Call,为所有的请求提供通用的配置和监听,并负责具体任务的执行调度。
Okhttpclient在newCall的时候,还会给Call提供一个EventListener。EventListener是对每个具体网络请求的监听,一个完整的HTTP请求过程,可以拆分成很多小步骤,比如建立连接,发送请求头,发送请求实体,读取响应头等等。这些关键的事件,都可以在EventListener被监听到,Okhttp会在关键的步骤调用相应的监听函数。

开始请求流程RealCall. execute()

  @Override public Response execute() throws IOException {
    synchronized (this) {
      if (executed) throw new IllegalStateException("Already Executed");
      executed = true;
    }
    captureCallStackTrace();
    try {
      //开始请求接口,先将请求加入到runningSyncCalls中
      client.dispatcher().executed(this);
      Response result = getResponseWithInterceptorChain();
      if (result == null) throw new IOException("Canceled");
      return result;
    } finally {
      client.dispatcher().finished(this);
    }
  }

由于当前是同步请求,所以Dispatcher的execute只是把任务存到集合中:

  /** Used by {@code Call#execute} to signal it is in-flight. */
  synchronized void executed(RealCall call) {
    runningSyncCalls.add(call);
  }

然后继续调用RealCall的getResponseWithInterceptorChain函数:

  Response getResponseWithInterceptorChain() throws IOException {
    // Build a full stack of interceptors.
    List<Interceptor> interceptors = new ArrayList<>();
    interceptors.addAll(client.interceptors());
    interceptors.add(retryAndFollowUpInterceptor);
    interceptors.add(new BridgeInterceptor(client.cookieJar()));
    interceptors.add(new CacheInterceptor(client.internalCache()));
    interceptors.add(new ConnectInterceptor(client));
    if (!forWebSocket) {
      interceptors.addAll(client.networkInterceptors());
    }
    interceptors.add(new CallServerInterceptor(forWebSocket));

    Interceptor.Chain chain = new RealInterceptorChain(
        interceptors, null, null, null, 0, originalRequest);
    return chain.proceed(originalRequest);
  }

这个时候就涉及到了okhttp的核心概念:拦截器。在okhttp中,将一个完整的Http请求拆分成了好几个步骤,然后每个步骤都通过拦截器来完成。一个HTTP请求的完整路径,会经过一系列拦截器的渗透和过滤。拦截器可以分为四种,有先后顺序,依次为:应用拦截器,okhttp核心拦截器,网络拦截器以及最后的call server拦截器。所谓的call server也就是真正的向服务器发送请求,按照HTTP协议的规范,顺序的向服务器发送请求头和请求实体,然后在顺序的读取响应头和响应实体。这些拦截器会形成一个拦截器链,对应的类型为Interceptor.Chain。

第二种异步的方式:

        Request.Builder builder = new Request.Builder();
        Request request = builder.url("https://www.baidu.com").build();
        Call newCall = client.newCall(request);
        newCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });

前面的准备都是一样的,就不重复分析了,直接看enqueue方法:
#开始请求流程RealCall. enqueue()

  @Override public void enqueue(Callback responseCallback) {
    synchronized (this) {
      if (executed) throw new IllegalStateException("Already Executed");
      executed = true;
    }
    captureCallStackTrace();
    client.dispatcher().enqueue(new AsyncCall(responseCallback));
  }

这里调用了调度器Dispatcher#enqueue

Dispatcher#enqueue

  synchronized void enqueue(AsyncCall call) {
    if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
      runningAsyncCalls.add(call);
      executorService().execute(call);
    } else {
      readyAsyncCalls.add(call);
    }
  }

这里就很明显了,首先对请求数量以及host做了判断不超过maxRequests(默认值64)和maxRequestsPerHost(默认值5)才会直接将请求加入到runningAsyncCalls中并且开始请求,否则加入到等待队列中。接着分析executorService().execute(call)

  public synchronized ExecutorService executorService() {
    if (executorService == null) {
      executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS,
          new SynchronousQueue<Runnable>(), Util.threadFactory("OkHttp Dispatcher", false));
    }
    return executorService;
  }

这里构建了一个线程池,这里就不细拆线程池了感兴趣的可以查资料再去了解,获取到线程池后,就可以交给其call任务,让其去执行。最终会调用到AsyncCall的的execute方法。
可能有人会问为什么会执行AsyncCall的execute方法???
AsyncCall继承自NameRunnable,NameRunnable继承自Runnable。NameRunnable中的实现如下,所以会执行NameRunnable的execute方法,execute的实现在AsyncCall中。

AsyncCall

  final class AsyncCall extends NamedRunnable {
    private final Callback responseCallback;

    AsyncCall(Callback responseCallback) {
      super("OkHttp %s", redactedUrl());
      this.responseCallback = responseCallback;
    }

    String host() {
      return originalRequest.url().host();
    }

    Request request() {
      return originalRequest;
    }

    RealCall get() {
      return RealCall.this;
    }

    @Override protected void execute() {
      boolean signalledCallback = false;
      try {
        Response response = getResponseWithInterceptorChain();
        if (retryAndFollowUpInterceptor.isCanceled()) {
          signalledCallback = true;
          responseCallback.onFailure(RealCall.this, new IOException("Canceled"));
        } else {
          signalledCallback = true;
          responseCallback.onResponse(RealCall.this, response);
        }
      } catch (IOException e) {
        if (signalledCallback) {
          // Do not signal the callback twice!
          Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);
        } else {
          responseCallback.onFailure(RealCall.this, e);
        }
      } finally {
        client.dispatcher().finished(this);
      }
    }
  }

NamedRunnable

public abstract class NamedRunnable implements Runnable {
  protected final String name;

  public NamedRunnable(String format, Object... args) {
    this.name = Util.format(format, args);
  }

  @Override public final void run() {
    String oldName = Thread.currentThread().getName();
    Thread.currentThread().setName(name);
    try {
      execute();
    } finally {
      Thread.currentThread().setName(oldName);
    }
  }

  protected abstract void execute();
}

接着看下AsyncCall的execute,它也是调用getResponseWithInterceptorChain,链式调用(责任链五层拦截器模式)获取Response最终通过responseCallback回调,完成异步请求。
拦截器部分没有展开讲解,后面有时间会另外开一篇讲解,此处就说明下OkHttp的初始化以及请求、回调的源码剖析。

OKHttp中默认包含五层责任链,分别为:
RetryAndFollowUpInterceptor:重试跟进拦截器。
BridgeInterceptor:桥接拦截器。
CacheInterceptor:缓存拦截器。
ConnectInterceptor:连接池拦截器。
CallServerInterceptor:请求拦截器。

一个请求的完整发送流程,会从上向下依次执行,如果某一个拦截器判断执行完成,则一层一层向上返回最终结果。

谢谢各位的阅读,觉得可以的小伙伴点个赞哦。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值