OkHttp_3.12.0源码分析

作者: wgyscsf | 来源:发表于2019-07-31 22:33 被阅读4次

    OkHttp_3.12.0源码分析

    基于OkHttp:3.12.0:https://github.com/square/okhttp/tree/okhttp_3.12.x

    RTFSC

    Read The Fucking Source Code

    流程分析

    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:
      1. RealCall#getResponseWithInterceptorChain会把用户自定义的拦截器、系统级别的拦截器放到list中组装好,给一个new RealInterceptorChain。这里需要注意拦截器加入的顺序,用户拦截器优先级最高->系统一些中间拦截->最后网络请求。
      2. RealInterceptorChain#proceed在这里会构造一个新的RealInterceptorChain:next,注意新的RealInterceptorChain中index+1;另外根据当前的index取得当前拦截器,执行拦截器中唯一的方法intercept(next),具体执行的哪一个呢,就看上层实现的是哪一个具体实现。
      3. 我们随便看一个实现,假如BridgeInterceptor:会在BridgeInterceptor中执行BridgeInterceptor相关的实现,最关键的会执行chain.proceed(requestBuilder.build()),注意Chain是上一个创建的RealInterceptorChain,所以又跑到了第二步,同时index这个时候已经+1,如此完成递归操作!
      4. 经过2、3两步,通过迭代操作,完整所有拦截器的执行。至于说网络请求、缓存、重试、日志等等核心包括外围功能,全部通过拦截器实现,并且责任分明,高度解耦合,实现方式:责任链模式。
    #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();
      }
    
    

    相关文章

      网友评论

        本文标题:OkHttp_3.12.0源码分析

        本文链接:https://www.haomeiwen.com/subject/zxnxdctx.html