转载

OkHttp_3.12.0源码分析

  1. 用户层面调用形式:
OkHttpClient#newCall(new Request())#enquenue(new Callback(){})

复制代码
  1. OkHttpClient#newCall(new Request()) :OkHttpClient根据client、request创建RealCall,RealCall里面包含所有请求的各种配置信息:拦截器、请求连接、调度器Dispatcher、ConnectionPool等
//OkHttpClient
@Override public Call newCall(Request request) {
    return RealCall.newRealCall(this, request, false /* for web socket */);
  }
复制代码
  1. RealCall#enqueue(new Callback(){}) :
    1. 通过RealCall中的client间接获取Dispatcher--维护一个线程池、readyAsyncCalls、runningAsyncCalls、runningSyncCalls三个请求队列。
    2. Dispatcher的enqueue会把当前的responseCallback包装成AsyncCall的形式放入readyAsyncCalls队列中,注意AsyncCall实现了Runnable接口。
//RealCall
@Override public void enqueue(Callback responseCallback) {
     //其它信息...    
     client.dispatcher().enqueue(new AsyncCall(responseCallback));
  }
  
复制代码
// Dispatcher    
 void enqueue(AsyncCall call) {
    synchronized (this) {
         readyAsyncCalls.add(call);
        }
        promoteAndExecute();
    } 
复制代码
  1. Dispatcher#promoteAndExecute:这一步会遍历readyAsyncCalls把准备队列中的AsyncCall放到runningAsyncCalls运行队列中;同时放到立马执行的一个列表中:executableCalls。这里一些额外的判断是:,请求是否超过了最大请求数maxRequests,同一个host是否大于maxRequestsPerHostmaxRequests。
//Dispatcher
    private boolean promoteAndExecute() {
        //...
        List<AsyncCall> executableCalls = new ArrayList<>();
        //伪代码...
        if(满足一定条件){executableCalls.addAll(readyAsyncCalls)}
    
        for (int i = 0, size = executableCalls.size(); i < size; i++) {
          AsyncCall asyncCall = executableCalls.get(i);
          asyncCall.executeOn(executorService());
        }
    
        return isRunning;
      }
复制代码
  1. AsyncCall#executeOn(executorService()) :

    1. AsyncCall#executeOn(ExecutorService):通过线程池执行当前对象,会调用上层NamedRunnable的run()方法。 重点 :这里完成线程切换逻辑,因为下面就要开始去执行耗时操作。
    2. NamedRunnable#run方法会调用execute(),具体逻辑在子类AsyncCall实现。
    3. AsyncCall#execute()是真正获取Response值、把值回调给responseCallback的步骤。
//AsyncCall 
   void executeOn(ExecutorService executorService) {
       //...
       executorService.execute(this);//这里会调用父类的run方法
       //...
   }
复制代码
//NamedRunnable, AsyncCall的父类  
    public abstract class NamedRunnable implements Runnable {
      //...    
      @Override public final void run() {
        //...
        execute();
      }
      //子类AsyncCall做对应实现
      protected abstract void execute();
    }        
        
复制代码
#AsyncCall
   @Override protected void execute() {
       boolean signalledCallback = false;
       timeout.enter();
       try {
       //关键,去获取response对象,真正的耗时也就在这
       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) {
           e = timeoutExit(e);
           if (signalledCallback) {
             // Do not signal the callback twice!
             Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);
           } else {
             eventListener.callFailed(RealCall.this, e);
             //失败,回调
             responseCallback.onFailure(RealCall.this, e);
       }
         } finally {
           //完整结束当前请求,做一些收尾操作
           client.dispatcher().finished(this);
         }
       }
     }
     
复制代码
  1. RealCall#getResponseWithInterceptorChain :
    chain.proceed(requestBuilder.build())
    
#RealCall
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, this, eventListener, client.connectTimeoutMillis(),
        client.readTimeoutMillis(), client.writeTimeoutMillis());

    return chain.proceed(originalRequest);
  }
  
复制代码
#RealInterceptorChain
@Override public Response proceed(Request request) throws IOException {
    return proceed(request, streamAllocation, httpCodec, connection);
  }

  public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
      RealConnection connection) throws IOException {
    //...
    
    //注意这里又构造了一个RealInterceptorChain,重点index+1
    RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
        connection, index + 1, request, call, eventListener, connectTimeout, readTimeout,
        writeTimeout);
        
    //取当前index对应的拦截器
    Interceptor interceptor = interceptors.get(index);
    //执行取到的拦截器
    Response response = interceptor.intercept(next);
    
    //...
    return response;
  }

复制代码
# BridgeInterceptor
@Override public Response intercept(Chain chain) throws IOException {
    //根据上游chain继续处理当前拦截器。注意:chain的源头:RealInterceptorChain#proceed中创建的next,一个新的RealInterceptorChain
    Request userRequest = chain.request();
    Request.Builder requestBuilder = userRequest.newBuilder();

    //各种逻辑,跳过只看核心...
    
    //核心,这里又回到了RealInterceptorChain#proceed
    Response networkResponse = chain.proceed(requestBuilder.build());

    //后续逻辑...

    return responseBuilder.build();
  }

复制代码
原文  https://juejin.im/post/5d41a727e51d4561b76c7407
正文到此结束
Loading...