OkHttp3源码解读一:接口请求层

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/www1575066083/article/details/81483571

1、基本介绍:

  • 接口层:接收用户的网络访问请求(同步请求/异步请求),发起实际的网络访问,接受服务器返回的结果。

2、重要类:

2.1、OkHttpClient:

  • OkHttp框架的客户端,更确切的说是一个用户面板,用户使用OkHttp进行各种设置,发起各种网络请求都是通过OkHttpClient完成的。
  • 每个OkHttpClient内部都维护了属于自己的任务队列Dispatcher,连接池ConnectionPool,缓存Cache,拦截器Interceptor等。
  • 在使用OkHttp作为网络框架时应该全局共享一个OkHttpClient实例。

2.2、Call:【每一个请求实例接口】

Call call  = okHttpClient.newCall(request);
  • Call本身只是一个接口,描述一个实际的HTTP访问请求,用户的每一个HTTP请求都是一个Call实例。
  • 每一个Call就是一个线程。
  • Call包含了怎么生成Request对象request()execute()enqueue()方法。

2.3、RealCall:

  • 具体的Call接口实现类,代表每一个HTTP请求。
  • 每一个RealCall内部有一个AsyncCall final类。

2.4、AsyncCall :

  • RealCall类的内部final类,实现了NamedRunnable类的execute()。
  • 继承于NamedRunnable类,NamedRunnable类实现了Runnable接口,并且有一个execute()抽象方法,这个抽象方法在Runnable的run()里执行。

2.5、Dispatcher:【管理Call的任务队列】

  • OkHttp的任务队列,其内部维护了一个线程池,进行线程分发,实现非阻塞,高可用,高并发。
  • 当有接收到一个Call时,Dispatcher负责在线程池中找到空闲的线程并执行其execute方法。
  • Okhttp采用Deque作为缓存队列,按照入队的顺序先进先出。
  • OkHttp最出彩的地方就是在try/finally中调用了finished函数,可以主动控制等待队列的移动,而不是采用锁或者wait/notify,极大减少了编码复杂性。

3、重要流程:

3.1、okHttpClient.newCall(request):

# OkHttpClient.java 

Call call  = okHttpClient.newCall(request);

//okHttpClient.newCall(request)会为每一个请求创建一个RealCall类实例。

3.2、RealCall.execute():

# RealCall.java
      @Override public Response execute() throws IOException {
        synchronized (this) {
          if (executed) throw new IllegalStateException("Already Executed");
          executed = true;
        }
        captureCallStackTrace();
        try {
          client.dispatcher().executed(this);
          Response result = getResponseWithInterceptorChain();
          if (result == null) throw new IOException("Canceled");
          return result;
        } finally {
          client.dispatcher().finished(this);
        }
      }

//1、调用Dispatcher类的executed():将对应任务加入分发器Dispatcher,执行任务。
//2、构建拦截器链,调用拦截器链的proceed():返回请求结果。
//3、调用Dispatcher类的finished():执行完成后通知dispatcher对应任务已完成,对应任务出队。

3.2、RealCall.enqueue():

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

//构建AsyncCall类实例并执行Dispatcher的enqueue():最终RealCall被转化成一个AsyncCall并被放入到任务队列Dispatcher中,启动Dispatcher中的线程池,等待合适的时机会执行AsyncCall的execute()。
 #  RealCall#AsyncCall.java
    @Override 
    protected void execute() {
          boolean signalledCallback = false;
          try {
                //执行耗时IO任务
            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);
          }
        }
      }
//1、调用getResponseWithInterceptorChain():构建了一个拦截器链,通过依次执行该拦截器链中的每一个拦截器最终得到服务器返回。
//2、通知任务分发器(client.dispatcher)该任务已结束。

3.3、getResponseWithInterceptorChain():【构建拦截器链】

# RealCall.java 
    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);
      }

//1、创建一系列拦截器,并将其放入一个拦截器数组中。这部分拦截器即包括用户自定义的拦截器也包括框架内部拦截器。
//2、创建一个拦截器链RealInterceptorChain,并执行拦截器链的proceed方法。

3.4、RealInterceptorChain#proceed():【调用下一个拦截器】

# RealInterceptorChain.java
     public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
          RealConnection connection) throws IOException {
        ......
        // Call the next interceptor in the chain.
        RealInterceptorChain next = new RealInterceptorChain(
            interceptors, streamAllocation, httpCodec, connection, index + 1, request);
        Interceptor interceptor = interceptors.get(index);
        Response response = interceptor.intercept(next);

        ...... 

        return response;
      }
//1、创建下一个拦截链。传入index + 1使得下一个拦截器链只能从下一个拦截器开始访问。
//2、执行索引为index的拦截器的intercept方法,并将下一个拦截器链传入该intercept方法。

4、Okhttp的任务队列:【Dispatcher】

4.1、Dispatcher任务分发:

# Dispatcher.java 
    public final class Dispatcher {
      private int maxRequests = 64;
      private int maxRequestsPerHost = 5;
      private Runnable idleCallback;

      /** Executes calls. Created lazily. */
      //任务队列线程池
      private ExecutorService executorService;

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

      /** Running asynchronous calls. Includes canceled calls that haven't finished yet. */ 
      //运行中异步任务队列【最大64】
      private final Deque<AsyncCall> runningAsyncCalls = new ArrayDeque<>();

      /** Running synchronous calls. Includes canceled calls that haven't finished yet. */
      //运行中同步任务队列
      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;
      }

      ...
    }

//1、在Okhttp中,构建了一个阀值为[0, Integer.MAX_VALUE]的线程池,它不保留任何最小线程数,随时创建更多的线程数,当线程空闲时只能活60秒,它使用了一个不存储元素的阻塞工作队列,一个叫做"OkHttp Dispatcher"的线程工厂。
//2、也就是说,在实际运行中,当收到10个并发请求时,线程池会创建十个线程,当工作完成后,线程池会在60s后相继关闭所有线程。

4.2、同步请求:

# Dispatcher.java

      synchronized void executed(RealCall call) {
        runningSyncCalls.add(call);
      }

4.3、异步请求:

# Dispatcher.java 
    synchronized void enqueue(AsyncCall call) {
        if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
          runningAsyncCalls.add(call);
             //线程池执行请求
          executorService().execute(call);
        } else {
          readyAsyncCalls.add(call);
        }
      }

//1、当前请求数小于最大请求数(64),对单一host的请求小于阈值(5):将该任务插入正在执行任务队列,并执行对应任务。
//2、如果不满足则将其放入待执行队列。
# Dispatcher.java  
    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();
        }
      }

//1、空闲出多余线程,调用promoteCalls调用待执行的任务
//2、如果当前整个线程池都空闲下来,执行空闲通知回调线程(idleCallback)
# Dispatcher.java 
    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.
        }
      }
//遍历待执行任务队列,将任务放入正在执行任务队列,并执行该任务。

猜你喜欢

转载自blog.csdn.net/www1575066083/article/details/81483571