OkHttp请求源码解析

本次分析的OkHttp版本为OkHttp3.6.0,不同的版本会有略微差异。

OkHttp请求流程一般为:

1、创建OkHttpClient对象:

OkHttpClient client = new OkHttpClient();

或者

OkHttpClient client = new OkHttpClient.Builder().build();

2、创建Request对象:

Request request = new Request.Builder().build();

3、通过client的newCall方法生成Call对象:

Call call = client.newCall(request);

4、call通过execute方法执行同步调用或者enqueue方法执行异步调用

Response response = call.execute();
call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

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

            }
});

此时一个OkHttp的请求流程已经完成,接下来根据流程来分析源码:

  • 先看OkHttpClient的源码:
public OkHttpClient() {
    this(new Builder());
}

OkHttpClient构造方法内部会创建一个默认的Builder对象,然后把此对象传入内部构造方法,这里会把Builder的属性赋值给OkHttpClient。如果通过Builder创建OkHttpClient则是传入自定义的Builder,可以通过Builder设置一些参数,如自定义拦截器、超时时间等。

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.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;
  }

再进入Builder类,其构造方法代码如下:

public static final class Builder {
    public Builder() {
      dispatcher = new Dispatcher();
      protocols = DEFAULT_PROTOCOLS;
      connectionSpecs = DEFAULT_CONNECTION_SPECS;
      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;
    }

    public OkHttpClient build() {
      return new OkHttpClient(this);
    }
}

这里面主要创建了Dispatcher(调度器)以及设置了很多参数,这里看Dispatcher。

public final class Dispatcher {
  private int maxRequests = 64; //最大请求数量
  private int maxRequestsPerHost = 5; //每个主机最大请求数量
  private Runnable idleCallback; //空闲回调

  private ExecutorService executorService; //线程池

  /** 异步调用准备队列 */
  private final Deque<AsyncCall> readyAsyncCalls = new ArrayDeque<>();

  /** 异步调用执行队列 */
  private final Deque<AsyncCall> runningAsyncCalls = new ArrayDeque<>();

  /** 同步调用执行队列 */
  private final Deque<RealCall> runningSyncCalls = new ArrayDeque<>();
}

这里先看下Dispatcher的属性,默认最大请求数量为64,每个主机最大请求数量为5,空闲回调,处理异步调用的线程池,一个异步调用准备队列,一个异步调用执行队列,一个同步调用执行队列。

Dispatcher主要用来调度请求,具体方法在后面分析。

  • 接下来看Request的源码:

Request是通过Builder模式创建的,用来设置rul、请求方式、请求头、请求参数等。

public final class Request {
  final HttpUrl url;
  final String method;
  final Headers headers;
  final RequestBody body;
  final Object tag;

  private volatile CacheControl cacheControl; // Lazily initialized.

  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;
  }

  ......

  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();
    }

    Builder(Request request) {
      this.url = request.url;
      this.method = request.method;
      this.body = request.body;
      this.tag = request.tag;
      this.headers = request.headers.newBuilder();
    }

    ......

    public Request build() {
      if (url == null) throw new IllegalStateException("url == null");
      return new Request(this);
    }
  }
}
  • 然后获取Call对象:
public interface Call extends Cloneable {
  Request request();

  Response execute() throws IOException; //同步调用
  
  void enqueue(Callback responseCallback); //异步调用

  boolean isExecuted();

  boolean isCanceled();
 
  Call clone();

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

Call是个接口,里面有同步、异步调用方法,具体Call对象是通过OkHttPClient的newCall方法,传入Request生成的RealCall。

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

进入RealCall类,其构造方法如下:

RealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {
    this.client = client;
    this.originalRequest = originalRequest;
    this.forWebSocket = forWebSocket;
    this.retryAndFollowUpInterceptor = new RetryAndFollowUpInterceptor(client, forWebSocket);
}

这里会创建一个RetryAndFollowUpInterceptor(失败和重定向拦截器)。

  • 最后执行Call的同步、异步调用:

由于创建的Call对象是ReallCall,所以还是看里面的同步、异步调用方法。

先看同步调用execute:

@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);
    }
}

execute方法:

1、同步线程,判断是否执行过请求;

2、捕获调用堆栈的信息,用来分析连接泄露;

3、调度器调用同步方法;

4、使用拦截器链获取响应;

5、调度器调用完成方法。

Dispatcher中的executed方法:把Call放入同步调用执行队列里面

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

使用拦截器链获取响应:创建一个拦截器集合并添加各种拦截器,之后创建一个RealInterceptorChain(拦截器链)并执行proceed方法。

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)); //请求服务拦截器

    //获取Chain
    Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0, originalRequest);
    
    //执行chain.proceed方法返回response
    return chain.proceed(originalRequest);
}

Interceptor和Interceptor.Chain均为接口:Interceptor有一个intercept方法,Chain有request、proceed、connection方法

public interface Interceptor {
  Response intercept(Chain chain) throws IOException;

  interface Chain {
    Request request();

    Response proceed(Request request) throws IOException;

    Connection connection();
  }
}

查看RealInterceptorChain的proceed方法:创建新的RealInterceptorChain传入index+1,获取当前index下标的拦截器,执行拦截器的intercept方法。

public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
      Connection connection) throws IOException {
    if (index >= interceptors.size()) throw new AssertionError();

    ......

    //创建一个新的RealInterceptorChain,并且传入index+1,用于获取下个拦截器
    RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec, connection, index + 1, request);
    //获取当前index下标对应的拦截器
    Interceptor interceptor = interceptors.get(index);
    //执行该拦截器的intercept方法,并传入刚刚创建的RealInterceptorChain 
    Response response = interceptor.intercept(next);

    ......

    return response;
}

我们此处查看RetryAndFollowUpInterceptor拦截器,这里不分析具体实现只查看Chain相关内容:这里面获取Chain的Request,并调用其proceed方法调用下一个拦截器,直到CallServerInterceptor拦截器为止,最后返回Response。

public final class RetryAndFollowUpInterceptor implements Interceptor {
  
    ......

  @Override public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        
        ......
        
        while (true) {
        
            ......

            response = ((RealInterceptorChain) chain).proceed(request, streamAllocation, null, null);
    
            ......

            return response;
      
            ......

      }
  }

    ......

}

所有拦截器都执行完毕后返回Response,execute方法返回此响应,最后再执行Dispatcher的finished方法:同步调用执行队列移除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();
    }
}

到此Call的同步调用执行完毕,接下来继续看异步调用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));
}

enqueue方法:

1、同步线程,判断是否执行过请求;

2、捕获调用堆栈的信息,用来分析连接泄露;

3、调度器调用异步方法;

Dispatcher中的enqueue方法:先创建了一个AsyncCall对象,该对象作为参数传入enqueue方法,在enqueue方法中判断当前执行的异步调用是否小于设定值并且当前主机执行的异步调用是否小于设定值,如果都小于就把call添加进异步调用执行队列并通过线程池执行该调用,否则把call加入到异步调用等待队列。

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

我们来看下AsynCall类:AsynCall继承NamedRunnable,NamedRunnable是一个Runnable,其run方法中调用的execute方法。AsynCall重写了execute方法,方法里面调用了getResponseWithInterceptorChain返回Response,并通过callback回调返回响应,最后再执行Dispatcher的finished方法。

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();
}


final class AsyncCall extends NamedRunnable {
    
    ......

    @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);
      }
    }
}

异步调用Dispatcher的finished方法:与同步调用不同的是promoteCalls为true,因此要执行promoteCalls方法。

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

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();
    }
}

promoteCalls方法:这里面会去判断当前异步调用执行队列是否有空闲,有空闲就从等待队列中取出一个调用执行,直到没有等待调用或者执行队列已满。

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.
    }
}

 

总结:

拦截器是OkHttp的精髓,OkHttp中最核心的方法是getResponseWithInterceptorChain,这个方法对拦截器进行了链式调用。

Chain执行proceed方法,proceed方法从拦截器List中取出当前下标的拦截器,并创建拦截器下标加1的Chain,当前拦截器执行intercept方法,传入新的Chain。拦截器的intercept方法中Chain又执行proceed方法,从而按顺序调用拦截器,直到CallServerInterceptor。

同步调用执行完后直接返回Response,异步调用执行完后会判断执行队列个数并从等待队列中取出调用执行。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值