OkHttp3实现原理分析(二)

概述

前言:前一节https://mp.csdn.net/postedit/84941253,总结了一下OkHttp3的简单使用教程。在项目中使用了这个网络框架,在看完基本的源码之后,还是想总结一下OkHttp的实现流程。在学习框架的过程中,从使用方法出发,首先是怎么使用,其次是我们使用的功能在内部是如何实现的,实现方案上有什么技巧,有什么范式。

OkHttp的整体流程

整个流程是:通过OkHttpClient将构建的Request转换为Call,然后在RealCall中进行异步或同步任务,最后通过一些的拦截器interceptor发出网络请求和得到返回的response。总体流程用下面的图表示

Okhttp3整体流程.png

拆组件

在整体流程中,主要的组件是OkHttpClient,其次有Call,RealCall,Disptcher,各种InterceptorsRequest和Response组件。Request和Response已经在上一篇的对其结构源码进行了分析。

1. OkHttpClient对象:网络请求的主要操控者

创建OkHttpClient对象

//通过Builder构造OkHttpClient
 OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS);
        return builder.build();

OkHttpClient.Builder类有很多变量,OkHttpClient有很多的成员变量:

final Dispatcher dispatcher;  //重要:分发器,分发执行和关闭由request构成的Call
    final Proxy proxy;  //代理
    final List<Protocol> protocols; //协议
    final List<ConnectionSpec> connectionSpecs; //传输层版本和连接协议
    final List<Interceptor> interceptors; //重要:拦截器
    final List<Interceptor> networkInterceptors; //网络拦截器
    final ProxySelector proxySelector; //代理选择
    final CookieJar cookieJar; //cookie
    final Cache cache; //缓存
    final InternalCache internalCache;  //内部缓存
    final SocketFactory socketFactory;  //socket 工厂
    final SSLSocketFactory sslSocketFactory; //安全套接层socket 工厂,用于HTTPS
    final CertificateChainCleaner certificateChainCleaner; // 验证确认响应证书 适用 HTTPS 请求连接的主机名。
    final HostnameVerifier hostnameVerifier;    //  主机名字确认
    final CertificatePinner certificatePinner;  //  证书链
    final Authenticator proxyAuthenticator;     //代理身份验证
    final Authenticator authenticator;      // 本地身份验证
    final ConnectionPool connectionPool;    //连接池,复用连接
    final Dns dns;  //域名
    final boolean followSslRedirects;  //安全套接层重定向
    final boolean followRedirects;  //本地重定向
    final boolean retryOnConnectionFailure; //重试连接失败
    final int connectTimeout;    //连接超时
    final int readTimeout; //read 超时
    final int writeTimeout; //write 超时

OkHttpClient完成整个请求设计到很多参数,都可以通过OkHttpClient.builder使用创建者模式构建。事实上,你能够通过它来设置改变一些参数,因为他是通过建造者模式实现的,因此你可以通过builder()来设置。如果不进行设置,在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;
    }

2,RealCall:真正的请求执行者

2.1之前文章中的Http发起同步请求的代码:

 Request request = new Request.Builder()
      .url(url)
      .build();
  Response response = client.newCall(request).execute();

client.newCall(request).execute()创建了Call执行了网络请求获得response响应。重点看一看这个执行的请求者的内部是什么鬼。

/**
   * Prepares the {@code request} to be executed at some point in the future.
   */
//OkHttpClient中的方法,可以看出RealCall的真面目
  @Override public Call newCall(Request request) {
    return RealCall.newRealCall(this, request, false /* for web socket */);
  }

RealCall的构造函数:

 private RealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {
    //client请求
    this.client = client;
    //我们构造的请求
    this.originalRequest = originalRequest;
    this.forWebSocket = forWebSocket;
    //负责重试和重定向拦截器
    this.retryAndFollowUpInterceptor = new RetryAndFollowUpInterceptor(client, forWebSocket);
  }

Call对象其实是一个接口,Call的源码:


public interface Call extends Cloneable {
  /** Returns the original request that initiated this call. */
  //用于返回Call对象中的request对象
  Request request();
  //用于执行同步请求的方法
  Response execute() throws IOException;
 //用于执行异步请求的方法,通过responseCallback回调结果
  void enqueue(Callback responseCallback);

  /** Cancels the request, if possible. Requests that are already complete cannot be canceled. */
  //取消这个call,当call被取消时请求不在执行,抛出异常。可以用于终止请求
  void cancel();

  /**
   * Returns true if this call has been either {@linkplain #execute() executed} or {@linkplain
   * #enqueue(Callback) enqueued}. It is an error to execute a call more than once.
   */
 //是否被执行
  boolean isExecuted();
 //是否被取消
  boolean isCanceled();

  /**
   * Create a new, identical call to this one which can be enqueued or executed even if this call
   * has already been.
   */
  Call clone();

  interface Factory {
    Call newCall(Request request);
  }
}

Realcall是Call的实现类。显然重要的执行任务就交个RealCall对象execute()和enqueue(Callback responseCallback)方法了。
我们首先看 RealCall#execute

 @Override public Response execute() throws IOException {
    //(1)
    synchronized (this) {
      if (executed) throw new IllegalStateException("Already Executed");
      executed = true;
    }
    captureCallStackTrace();
    //事件监听器调
    eventListener.callStart(this);
    try {
      //(2)
      client.dispatcher().executed(this);
      //(3)
      Response result = getResponseWithInterceptorChain();
      if (result == null) throw new IOException("Canceled");
      return result;
    } catch (IOException e) {
      eventListener.callFailed(this, e);
      throw e;
    } finally {
      //(4)
      client.dispatcher().finished(this);
    }
  }

(1)检查这个 call 是否已经被执行了,每个 call 只能被执行一次,如果想要一个完全一样的 call,可以利用 call#clone 方法进行克隆。
(2)利用 client.dispatcher().executed(this) 来进行实际执行,分发器负责分发。dispatcher 是刚才看到的 OkHttpClient.Builder 的成员之一,它的文档说自己是异步 HTTP 请求的执行策略,现在看来,同步请求它也有掺和。
(3)调用 getResponseWithInterceptorChain() 函数获取 HTTP 返回结果,从函数名可以看出,这一步还会进行一系列“拦截”操作。
(4)最后还要通知 dispatcher 自己已经执行完毕

dispatcher 这里我们不过度关注,在同步执行的流程中,涉及到 dispatcher 的内容只不过是告知它我们的执行状态,比如开始执行了(调用 executed),比如执行完毕了(调用 finished)在异步执行流程中它会有更多的参与。

Dispatcher的源码主要在异步请求时参与多,这里有执行异步请求的线程池

/**
 * Policy on when async requests are executed.
 *
 * <p>Each dispatcher uses an {@link ExecutorService} to run calls internally. If you supply your
 * own executor, it should be able to run {@linkplain #getMaxRequests the configured maximum} number
 * of calls concurrently.
 */
//请求分发器,**主要在异步请求时参与多,这里有执行异步请求的线程池**
public final class Dispatcher {
  //最大的请求数量
  private int maxRequests = 64;
  //每个主机的请求数量,默认在摸个主机上同时请求5个
  private int maxRequestsPerHost = 5;
  private @Nullable Runnable idleCallback;

  /** Executes calls. Created lazily. */
 //执行异步call时的线程池,就在这儿
  private @Nullable ExecutorService executorService;

  /** Ready async calls in the order they'll be run. */
 //即将被执行的异步call队列
  private final Deque<AsyncCall> readyAsyncCalls = new ArrayDeque<>();

  /** Running asynchronous calls. Includes canceled calls that haven't finished yet. */
 //正在运行的异步call,包括被取消的还没有完成的
  private final Deque<AsyncCall> runningAsyncCalls = new ArrayDeque<>();

  /** Running synchronous calls. Includes canceled calls that haven't finished yet. */
 //正在运行的同步call。包括被取消的还没有完成的
  private final Deque<RealCall> runningSyncCalls = new ArrayDeque<>();
 //可以执行自定义线程池,传进来
  public Dispatcher(ExecutorService executorService) {
    this.executorService = executorService;
  }

  public Dispatcher() {
  }
 //构造线程池
  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;
  }

  /**
   * Set the maximum number of requests to execute concurrently. Above this requests queue in
   * memory, waiting for the running calls to complete.
   *
   * <p>If more than {@code maxRequests} requests are in flight when this is invoked, those requests
   * will remain in flight.
   */
  public synchronized void setMaxRequests(int maxRequests) {
    if (maxRequests < 1) {
      throw new IllegalArgumentException("max < 1: " + maxRequests);
    }
    this.maxRequests = maxRequests;
    promoteCalls();
  }

  public synchronized int getMaxRequests() {
    return maxRequests;
  }

  /**
   * Set the maximum number of requests for each host to execute concurrently. This limits requests
   * by the URL's host name. Note that concurrent requests to a single IP address may still exceed
   * this limit: multiple hostnames may share an IP address or be routed through the same HTTP
   * proxy.
   *
   * <p>If more than {@code maxRequestsPerHost} requests are in flight when this is invoked, those
   * requests will remain in flight.
   */
  public synchronized void setMaxRequestsPerHost(int maxRequestsPerHost) {
    if (maxRequestsPerHost < 1) {
      throw new IllegalArgumentException("max < 1: " + maxRequestsPerHost);
    }
    this.maxRequestsPerHost = maxRequestsPerHost;
    promoteCalls();
  }

  public synchronized int getMaxRequestsPerHost() {
    return maxRequestsPerHost;
  }

  /**
   * Set a callback to be invoked each time the dispatcher becomes idle (when the number of running
   * calls returns to zero).
   *
   * <p>Note: The time at which a {@linkplain Call call} is considered idle is different depending
   * on whether it was run {@linkplain Call#enqueue(Callback) asynchronously} or
   * {@linkplain Call#execute() synchronously}. Asynchronous calls become idle after the
   * {@link Callback#onResponse onResponse} or {@link Callback#onFailure onFailure} callback has
   * returned. Synchronous calls become idle once {@link Call#execute() execute()} returns. This
   * means that if you are doing synchronous calls the network layer will not truly be idle until
   * every returned {@link Response} has been closed.
   */
  public synchronized void setIdleCallback(@Nullable Runnable idleCallback) {
    this.idleCallback = idleCallback;
  }
  //分发异步执行的call,是提交到线程池
  synchronized void enqueue(AsyncCall call) {
    if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
      runningAsyncCalls.add(call);
     //提交到线程此执行
      executorService().execute(call);
    } else {
      readyAsyncCalls.add(call);
    }
  }

  /**
   * Cancel all calls currently enqueued or executing. Includes calls executed both {@linkplain
   * Call#execute() synchronously} and {@linkplain Call#enqueue asynchronously}.
   */
  public synchronized void cancelAll() {
    for (AsyncCall call : readyAsyncCalls) {
      call.get().cancel();
    }

    for (AsyncCall call : runningAsyncCalls) {
      call.get().cancel();
    }

    for (RealCall call : runningSyncCalls) {
      call.cancel();
    }
  }

  private void promoteCalls() {
    if (runningAsyncCalls.size() >= maxRequests) return; // Already running max capacity.
    if (readyAsyncCalls.isEmpty()) return; // No ready calls to promote.

    for (Iterator<AsyncCall> i = readyAsyncCalls.iterator(); i.hasNext(); ) {
      AsyncCall call = i.next();

      if (runningCallsForHost(call) < maxRequestsPerHost) {
        i.remove();
        runningAsyncCalls.add(call);
        executorService().execute(call);
      }

      if (runningAsyncCalls.size() >= maxRequests) return; // Reached max capacity.
    }
  }

  /** Returns the number of running calls that share a host with {@code call}. */
  private int runningCallsForHost(AsyncCall call) {
    int result = 0;
    for (AsyncCall c : runningAsyncCalls) {
      if (c.host().equals(call.host())) result++;
    }
    return result;
  }

  /** Used by {@code Call#execute} to signal it is in-flight. */
 //分发同步call,只加入到正在运行同步call的队列
  synchronized void executed(RealCall call) {
    runningSyncCalls.add(call);
  }

  /** Used by {@code AsyncCall#run} to signal completion. */

  void finished(AsyncCall call) {
    finished(runningAsyncCalls, call, true);
  }

  /** Used by {@code Call#execute} to signal completion. */
  //同步call已经完成,移除队列
  void finished(RealCall call) {
    finished(runningSyncCalls, call, false);
  }

  private <T> void finished(Deque<T> calls, T call, boolean promoteCalls) {
    int runningCallsCount;
    Runnable idleCallback;
    synchronized (this) {
      if (!calls.remove(call)) throw new AssertionError("Call wasn't in-flight!");
      if (promoteCalls) promoteCalls();
      runningCallsCount = runningCallsCount();
      idleCallback = this.idleCallback;
    }

    if (runningCallsCount == 0 && idleCallback != null) {
      idleCallback.run();
    }
  }

  /** Returns a snapshot of the calls currently awaiting execution. */
 //返回等待执行call的集合
  public synchronized List<Call> queuedCalls() {
    List<Call> result = new ArrayList<>();
    for (AsyncCall asyncCall : readyAsyncCalls) {
      result.add(asyncCall.get());
    }
    return Collections.unmodifiableList(result);
  }

  /** Returns a snapshot of the calls currently being executed. */
  public synchronized List<Call> runningCalls() {
    List<Call> result = new ArrayList<>();
    result.addAll(runningSyncCalls);
    for (AsyncCall asyncCall : runningAsyncCalls) {
      result.add(asyncCall.get());
    }
    return Collections.unmodifiableList(result);
  }

  public synchronized int queuedCallsCount() {
    return readyAsyncCalls.size();
  }

  public synchronized int runningCallsCount() {
    return runningAsyncCalls.size() + runningSyncCalls.size();
  }
}

在上面的同步call中,真正发出网络请求,解析返回结果的,还是getResponseWithInterceptorChain

//重要的拦截器的责任链
 Response getResponseWithInterceptorChain() throws IOException {
    // Build a full stack of interceptors.
    List<Interceptor> interceptors = new ArrayList<>();
    interceptors.addAll(client.interceptors());                                //(1)
    interceptors.add(retryAndFollowUpInterceptor);                             //(2)
    interceptors.add(new BridgeInterceptor(client.cookieJar()));               //(3)
    interceptors.add(new CacheInterceptor(client.internalCache()));            //(4)
    interceptors.add(new ConnectInterceptor(client));                          //(5)
    if (!forWebSocket) {
      interceptors.addAll(client.networkInterceptors());
    }
    interceptors.add(new CallServerInterceptor(forWebSocket));

    Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
        originalRequest, this, eventListener, client.connectTimeoutMillis(),
        client.readTimeoutMillis(), client.writeTimeoutMillis());

    return chain.proceed(originalRequest);
  }

3,在获得相应之前经过的最后一关就是拦截器Interceptor

the whole thing is just a stack of built-in interceptors.

可见 Interceptor 是 OkHttp 最核心的一个东西,不要误以为它只负责拦截请求进行一些额外的处理(例如 cookie),实际上它把实际的网络请求、缓存、透明压缩等功能都统一了起来,每一个功能都只是一个 Interceptor,它们再连接成一个 Interceptor.Chain,环环相扣,最终圆满完成一次网络请求。

getResponseWithInterceptorChain 函数我们可以看到,Interceptor.Chain 的分布依次是:

image.png

(1)在配置 OkHttpClient时设置的interceptors
(2)负责失败重试以及重定向的 RetryAndFollowUpInterceptor
(3)负责把用户构造的请求转换为发送到服务器的请求、把服务器返回的响应转换为用户友好的响应的BridgeInterceptor
(4)负责读取缓存直接返回、更新缓存的 CacheInterceptor
(5)负责和服务器建立连接的ConnectInterceptor
(6)配置 OkHttpClient 时设置的 networkInterceptors;
(7)负责向服务器发送请求数据、从服务器读取响应数据CallServerInterceptor

在这里,位置决定了功能,最后一个 Interceptor 一定是负责和服务器实际通讯的重定向、缓存等一定是在实际通讯之前的

2.2 在2.1中我们深入讨论了同步请求的过程,下面讲讲异步请求原理

代码:

Request request = new Request.Builder()
        .url("http://publicobject.com/helloworld.txt")
        .build();
    //用request新建的call使用enqueue异步请求
    client.newCall(request).enqueue(new Callback() {
      @Override 
      public void onFailure(Call call, IOException e) {
        e.printStackTrace();
      }

      @Override 
      public void onResponse(Call call, Response response) throws IOException {
        //相应成功回调,response,非主线程
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

        Headers responseHeaders = response.headers();
        for (int i = 0, size = responseHeaders.size(); i < size; i++) {
          System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
        }

        System.out.println(response.body().string());
      }
    });

由代码中client.newCall(request).enqueue(Callback),开始我们知道client.newCall(request)方法返回的是RealCall对象,接下来继续向下看enqueue()方法:

//异步任务使用
    @Override 
    public void enqueue(Callback responseCallback) {
        synchronized (this) {
            if (executed) throw new IllegalStateException("Already Executed");
            executed = true;
        }
        //送给分发器Dispatcher分发,其实Dispatcher中有线程池,把AsyncCall这个任务提交到线程池执行,通过responseCallback回调
        client.dispatcher().enqueue(new AsyncCall(responseCallback));
    }

我们先看一下上面的Dispatcher类中的enqueue(Call )方法,在看看AsyncCall类:

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

如果中的runningAsynCalls不满,且call占用的host小于最大数量,则将call加入到runningAsyncCalls中执行,同时利用线程池执行call;否者将call加入到readyAsyncCalls中。runningAsyncCalls和readyAsyncCalls是什么呢?在把上面将同步Http请求时讲过了,可以瞄一眼。

call加入到线程池中执行了。现在再看AsynCall的代码,它是RealCall中的内部类

//异步请求,显然是继承了NamedRunnable ,在NamedRunnable 的run方法中执行继承的execute() 方法
    final class AsyncCall extends NamedRunnable {
        private final Callback responseCallback;

        private 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 {
                //告诉分发器Dispatcher请求执行完成
                client.dispatcher().finished(this);
            }
        }
    }

AysncCall中的execute()中的方法,同样是通过Response response = getResponseWithInterceptorChain();来获得response,这样异步任务也同样通过了interceptor,剩下的就想看看上面的几个拦截器是什么鬼。

责任链拦截器Interceptor

RetryAndFollowUpInterceptor:负责失败重试以及重定向
BridgeInterceptor:负责把用户构造的请求转换为发送到服务器的请求、把服务器返回的响应转换为用户友好的响应的 。
ConnectInterceptor:建立连接
NetworkInterceptors:配置OkHttpClient时设置的 NetworkInterceptors
CallServerInterceptor:发送和接收数据



作者:木有粗面_9602
链接:https://www.jianshu.com/p/9f2c982cd500
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

猜你喜欢

转载自blog.csdn.net/a2250742657/article/details/84954994