Android:OKHttp

特点

  • 支持HTTP2/SPDY
  • Socket自动选择最好路线,并支持自动重连
  • 拥有自动维护的Socket连接池,减少握手次数
  • 拥有队列线程池,轻松写并发
  • 拥有Interceptors轻松处理请求与响应(比如透明GZIP压缩)
  • 实现基于Headers的缓存策略

基本使用

同步请求

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

异步请求

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
      .url(url)
      .build();
client.newCall(request).enqueue(new Callback() {
    
    
            @Override
            public void onFailure(Call call, IOException e) {
    
    
                Log.e("DEBUG", "##### onFailure: ", e);
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
    
    
                Log.d("DEBUG", "##### response: " + response.body().string());
            }
        });

源码分析

Builder

OkHttpClient client = new OkHttpClient();

public OkHttpClient() {
    
    
    this(new Builder());
}

请求流程

在这里插入图片描述

同步请求

client.newCall(request).execute();//RealCall的execute方法

@Override public Response execute() throws IOException {
    
    
  synchronized (this) {
    
    //说明请求只能被执行一次
    if (executed) throw new IllegalStateException("Already Executed");
    executed = true;
  }
  transmitter.timeoutEnter();
  transmitter.callStart();
  try {
    
    
    client.dispatcher().executed(this);//由dispatcher这个核心调度类将请求加入队列
    return getResponseWithInterceptorChain();//获取HTTP请求结果,并会进行一系列拦截操作
  } finally {
    
    
    client.dispatcher().finished(this);//执行完毕操作,将线程从同步线程队列中移除
  }
 }

由dispatcher这个核心调度类将请求加入队列
getResponseWithInterceptorChain获取HTTP请求结果,并会进行一系列拦截操作

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

执行完毕操作,将线程从同步线程队列中移除

void finished(RealCall call) {
    
    
   finished(runningSyncCalls, call);
 }
private <T> void finished(Deque<T> calls, T call) {
    
    
  Runnable idleCallback;
  synchronized (this) {
    
    
    if (!calls.remove(call)) throw new AssertionError("Call wasn't in-flight!");
    idleCallback = this.idleCallback;
  }
  //异步方法中调用
  boolean isRunning = promoteAndExecute();
  if (!isRunning && idleCallback != null) {
    
    
    idleCallback.run();
  }
}

异步请求

将AsyncCall对象加入readyAsyncCalss队列中等待执行

@Override public void enqueue(Callback responseCallback) {
    
    
  synchronized (this) {
    
    
    if (executed) throw new IllegalStateException("Already Executed");
    executed = true;
  }
  transmitter.callStart();
  //将AsyncCall对象加入readyAsyncCalss队列中等待执行
  client.dispatcher().enqueue(new AsyncCall(responseCallback));
}

AsyncCall是RealCall的内部类,并且是NamedRunnable线程类
getResponseWithInterceptorChain()一样是获取HTTP请求结果,并会进行一系列拦截操作
client.dispatcher().finished(this)和同步方法中调用类似,但是异步的流程则完全不同

@Override protected void execute() {
    
    
  boolean signalledCallback = false;
  transmitter.timeoutEnter();
  try {
    
    
    Response response = getResponseWithInterceptorChain();
    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);
  }
}
void finished(AsyncCall call) {
    
    
  call.callsPerHost().decrementAndGet();
  finished(runningAsyncCalls, call);
}

private <T> void finished(Deque<T> calls, T call) {
    
    
  Runnable idleCallback;
  synchronized (this) {
    
    
    if (!calls.remove(call)) throw new AssertionError("Call wasn't in-flight!");
    idleCallback = this.idleCallback;
  }
  //异步方法中调用
  boolean isRunning = promoteAndExecute();
  if (!isRunning && idleCallback != null) {
    
    
    idleCallback.run();
  }
}

会遍历异步等待线程队列,并对正在执行的异步线程队列进行最大请求size,以及每个host最大请求size进行检查。

把异步等待线程放到正在执行线程队列中,并在等待线程队列中删除该线程,这样就把等待线程变成正在执行线程。

private boolean promoteAndExecute() {
    
    
  assert (!Thread.holdsLock(this));
  List<AsyncCall> executableCalls = new ArrayList<>();
  boolean isRunning;
  synchronized (this) {
    
    
    for (Iterator<AsyncCall> i = readyAsyncCalls.iterator(); i.hasNext(); ) {
    
    
      AsyncCall asyncCall = i.next();
      if (runningAsyncCalls.size() >= maxRequests) break; // Max capacity.
      if (asyncCall.callsPerHost().get() >= maxRequestsPerHost) continue; // Host max capacity.
      i.remove();
      asyncCall.callsPerHost().incrementAndGet();
      executableCalls.add(asyncCall);
      runningAsyncCalls.add(asyncCall);
    }
    isRunning = runningCallsCount() > 0;
  }
  for (int i = 0, size = executableCalls.size(); i < size; i++) {
    
    
    AsyncCall asyncCall = executableCalls.get(i);
    asyncCall.executeOn(executorService());
  }
  return isRunning;
}

Dispatcher

Dispatcher在builder中完成初始化

  • private int maxRequests = 64 最大请求并发请求数64
  • private int maxRequestsPerHost = 5 每个主机的最大请求数5
  • private @Nullable Runnable idleCallback;
  • private @Nullable ExecutorService executorService 线程池,懒汉模式创建

三条队列

  • private final Deque readyAsyncCalls = new ArrayDeque<>() 异步等待线程队列,按顺序执行
  • private final Deque runningAsyncCalls = new ArrayDeque<>() 正在运行的异步线程队列,运行异步调用,包括尚未完成的已取消呼叫
  • private final Deque runningSyncCalls = new ArrayDeque<>() 正在运行的同步线程队列,运行同步调用,包括尚未完成的已取消呼叫

execute会将请求加入runningSyncCalls
enqueue则是将检查是否已经到达最大并发数和主机最大并发数,到达则加入等待异步队列,不然则是执行异步队列

请求执行完后都会遍历异步等待队列查询可执行请求

ExecutorService

在OKHttp中,设置了不设上限的线程,不保留最小线程,线程空闲时,最大存活时间为60s,保证I/O任务中高阻塞低占用的过程,不会长时间卡在阻塞上。并通过maxRequests和maxRequestsPerHost来控制并发最大请求数。

public synchronized ExecutorService executorService() {
    
    
  if (executorService == null) {
    
    
    executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS,
        new SynchronousQueue<>(), Util.threadFactory("OkHttp Dispatcher", false));
  }
  return executorService;
}

拦截器

在这里插入图片描述

Response getResponseWithInterceptorChain() throws IOException {
    
    
  // Build a full stack of interceptors.
  List<Interceptor> interceptors = new ArrayList<>();
  interceptors.addAll(client.interceptors());
  interceptors.add(new RetryAndFollowUpInterceptor(client));
  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, transmitter, null, 0,
      originalRequest, this, client.connectTimeoutMillis(),
      client.readTimeoutMillis(), client.writeTimeoutMillis());
  boolean calledNoMoreExchanges = false;
  try {
    
    
    Response response = chain.proceed(originalRequest);
    if (transmitter.isCanceled()) {
    
    
      closeQuietly(response);
      throw new IOException("Canceled");
    }
    return response;
  } catch (IOException e) {
    
    
    calledNoMoreExchanges = true;
    throw transmitter.noMoreExchanges(e);
  } finally {
    
    
    if (!calledNoMoreExchanges) {
    
    
      transmitter.noMoreExchanges(null);
    }
  }
}
  • RetryAndFollowUpInterceptor:负责失败重试以及重定向

构建一个StreamAllocation对象,然后调用下一个拦截器获取结果,从返回结果中取得重定向的request,若重定向的request不为空且未超过重定向最大次数就进行重定向,否则返回结果
StreamAllocation在第一个拦截器就创建了,是为了取消请求以及出错释放资源
StreamAllocation负责统筹管理Connection、Stream、Call三个实体类
为一个Call(RealCall)寻找一个Connection(RealConnection)和获取一个Stream(HttpCode)

  • BridgeInterceptor:负责把用户构造的请求转换为发送到服务器的请求、把服务器返回的响应转换为用户友好的响应

对request添加Content-Type、Content-Length、Cookie、Connection、Host等请求头字段,以及对返回结果进行解压、保持Cookie等

  • CacheInterceptor:负责读取缓存直接返回、更新缓存

缓存策略为http的缓存策略 即先检查缓存是否过期(Expires/Cache-control),未过期则直接使用缓存,若过期则要先后检查是否有Etag和Last-Modified

Etag:第一次请求时服务器会返回一个Etag,第二次发起同一个请求时则客户端就会同时发送一个If-None-Match,值为Etag,服务器会返回一个If-None-Match值,若为true则客户端解析服务器传来的数据,若为false则使用本地缓存

Last-Modified:表示响应资源在服务器最后修改时间,但只能精准到秒级

  • ConnectInterceptor:负责和服务器建立连接

使用StreamAllocation.newStream来和服务端建立连接,并返回输入输出流(HttpCodec),实际上是通过StreamAllocation中的findConnection寻找一个可用的Connection,然后调用Connection的connect方法,使用socket与服务端建立连接

  • CallServerInterceptor:负责向服务器发送请求数据、从服务器读取响应数据

把请求的Request写入到服务端,然后从服务端读取Response。
写入请求头、写入请求体、读取响应头、读取响应体

责任链模式,通过Interceptor,把Request转换为Response,每个Interceptor都有各自的责任和逻辑。

加入自定义拦截器

interceptors.addAll(client.interceptors());
......
if (!forWebSocket) {
    
    
    interceptors.addAll(client.networkInterceptors());
  }

连接池

OkHttp支持5个并发KeeoAlive,默认链路生命为5分钟(即空闲时间),通过ConnectionPool进行回收和管理
ConnectionPool在内部使用一个异步线程来清理连接

清理流程

当连接池中有链接时,就会调用cleanup方法完成,每次执行清理后返回下次需要清理的间隔时间,调用wait释放锁

没有连接时,cleanup返回-1,跳出循环,当连接加入时才开启清理线程

清理线程会通过for循环查找最长空闲时间的连接以及对应空闲时间,判断该连接是否超过最大空闲连接数或最大空闲时间,满足其一则清除,否则返回等待时间:keepAliveDurationNs-longestIdleDurationNs或keepAliveDurationNs,前者为最大空闲时间减去该连接已空闲时间,即有空闲连接时,后者则是没有空闲链接时。

判断空闲

StreamAllocation创建或者复用一个Connection后,会将自己添加到Connection的connection.allocations列表中,数据读取完毕之后,会将自己从Connection的connection.allocations中移除,所以判读一个Connection是否是空闲连接可以采用引用计数法,判断connection.allocations列表中是否有StreamAllocation,如果没有就是空闲连接,否则不是。

设计模式

责任链模式:拦截器链

单例模式:线程池

观察者模式:各种回调监听

策略模式:缓存策略

Builder模式:OkHttpClient的构建过程

外观模式:OkHttpClient封装了很多类对象

工厂模式:Socket的生产

HTTP实现

OKHttp主要依靠ConnectIntercepter和CallServerIntercepter
ConnectIntercepter建立与服务器的连接
CallServerIntercepter发送请求和读取响应

流程如下:

  • 根据请求的URL,createAddress()创建一个Address
  • 检查Address和Routes,是否可以从ConnectionPool获取一个链接
  • 如果获取链接失败,就会进行下一个路由选择,并重新尝试从ConnectionPool获取一个链接,若重新获取失败则会重新创建一个链接
  • 获取链接后,会与服务器建立一个直接的Socket链接,使用TLS安全通道或直接TLS链接
  • 发送HTTP请求,并获取响应

ConnectInterceptor

@Override public Response intercept(Chain chain) throws IOException {
    
    
  RealInterceptorChain realChain = (RealInterceptorChain) chain;
  Request request = realChain.request();
  Transmitter transmitter = realChain.transmitter();
  // We need the network to satisfy this request. Possibly for validating a conditional GET.
  boolean doExtensiveHealthChecks = !request.method().equals("GET");
  Exchange exchange = transmitter.newExchange(chain, doExtensiveHealthChecks);
  return realChain.proceed(request, transmitter, exchange);
}

Exchange可以传输HTTP请求和响应,并管理连接和事件

/** Returns a new exchange to carry a new request and response. */
Exchange newExchange(Interceptor.Chain chain, boolean doExtensiveHealthChecks) {
    
    
  synchronized (connectionPool) {
    
    
    if (noMoreExchanges) {
    
    
      throw new IllegalStateException("released");
    }
    if (exchange != null) {
    
    
      throw new IllegalStateException("cannot make a new request because the previous response "
          + "is still open: please call response.close()");
    }
  }
  ExchangeCodec codec = exchangeFinder.find(client, chain, doExtensiveHealthChecks);
  Exchange result = new Exchange(this, call, eventListener, exchangeFinder, codec);
  ......
  }
}

find方法会最终执行ExchangeFinder的findConnection方法,在发送HTTP请求之前的逻辑,都是这个方法中实现。

findConnection会返回一个链接,优先已存在的链接,次之从链接池中取出,最后才是重新创建链接
流程和前面提到的一样

  /**
   * Returns a connection to host a new stream. This prefers the existing connection if it exists,
   * then the pool, finally building a new connection.
   */
  private RealConnection findConnection(int connectTimeout, int readTimeout, int writeTimeout,
      int pingIntervalMillis, boolean connectionRetryEnabled) throws IOException {
    
    
    boolean foundPooledConnection = false;
    RealConnection result = null;
    Route selectedRoute = null;
    RealConnection releasedConnection;
    Socket toClose;
    synchronized (connectionPool) {
    
    
      if (transmitter.isCanceled()) throw new IOException("Canceled");
      ......

      if (result == null) {
    
    
        //2.根据 Address 从连接池获取连接
        // Attempt to get a connection from the pool.
        if (connectionPool.transmitterAcquirePooledConnection(address, transmitter, null, false)) {
    
    
          foundPooledConnection = true;
          result = transmitter.connection;
        } else if (nextRouteToTry != null) {
    
    
          selectedRoute = nextRouteToTry;
          nextRouteToTry = null;
        } else if (retryCurrentRoute()) {
    
    
          selectedRoute = transmitter.connection.route();
        }
      }
    }
    ......
    // 3. 重新选择路由
    // If we need a route selection, make one. This is a blocking operation.
    boolean newRouteSelection = false;
    if (selectedRoute == null && (routeSelection == null || !routeSelection.hasNext())) {
    
    
      newRouteSelection = true;
      routeSelection = routeSelector.next();
    }

    List<Route> routes = null;
    synchronized (connectionPool) {
    
    
      if (transmitter.isCanceled()) throw new IOException("Canceled");

      if (newRouteSelection) {
    
    
        // Now that we have a set of IP addresses, make another attempt at getting a connection from
        // the pool. This could match due to connection coalescing.
        routes = routeSelection.getAll();
        if (connectionPool.transmitterAcquirePooledConnection(
            address, transmitter, routes, false)) {
    
    
          foundPooledConnection = true;
          result = transmitter.connection;
        }
      }

      if (!foundPooledConnection) {
    
    
        if (selectedRoute == null) {
    
    
          selectedRoute = routeSelection.next();
        }
      
        // 3. 重新选择路由,创建新的 `RealConnection`
        // Create a connection and assign it to this allocation immediately. This makes it possible
        // for an asynchronous cancel() to interrupt the handshake we're about to do.
        result = new RealConnection(connectionPool, selectedRoute);
        connectingConnection = result;
      }
    }

    ......
    // 4. 进行 Socket 连接
    // Do TCP + TLS handshakes. This is a blocking operation.
    result.connect(connectTimeout, readTimeout, writeTimeout, pingIntervalMillis,
        connectionRetryEnabled, call, eventListener);
    connectionPool.routeDatabase.connected(result.route());

    Socket socket = null;
    synchronized (connectionPool) {
    
    
      connectingConnection = null;
      // Last attempt at connection coalescing, which only occurs if we attempted multiple
      // concurrent connections to the same host.
      if (connectionPool.transmitterAcquirePooledConnection(address, transmitter, routes, true)) {
    
    
        // We lost the race! Close the connection we created and return the pooled connection.
        result.noNewExchanges = true;
        socket = result.socket();
        result = transmitter.connection;
      } else {
    
    
        //把连接放入连接池中
        connectionPool.put(result);
        transmitter.acquireConnectionNoEvents(result);
      }
    }
    ......
    return result;
  }

HTTP的链接由result.connect完成
分为是否需要隧道链接
connectSocket连接socket,establishProtocol根据HTTP协议版本进行连接处理。

public void connect(int connectTimeout, int readTimeout, int writeTimeout,
      int pingIntervalMillis, boolean connectionRetryEnabled, Call call,
      EventListener eventListener){
    
    
   if (protocol != null) throw new IllegalStateException("already connected");
   ......
   while (true) {
    
    
      try {
    
    
        if (route.requiresTunnel()) {
    
    
          connectTunnel(connectTimeout, readTimeout, writeTimeout, call, eventListener);
          if (rawSocket == null) {
    
    
            // We were unable to connect the tunnel but properly closed down our resources.
            break;
          }
        } else {
    
    
          connectSocket(connectTimeout, readTimeout, call, eventListener);
        }
        establishProtocol(connectionSpecSelector, pingIntervalMillis, call, eventListener);
        eventListener.connectEnd(call, route.socketAddress(), route.proxy(), protocol);
        break;
      } catch (IOException e) {
    
    
        ......
      }
    }
    ......
}

ConnectSocket
使用 Okio,封装了Socket的读写操作, 建立连接后,就可以发送请求和获取响应。

private void connectSocket(int connectTimeout, int readTimeout, Call call,
   EventListener eventListener) throws IOException {
    
    
   ......
   try {
    
    
      //连接 socket
      Platform.get().connectSocket(rawSocket, route.socketAddress(), connectTimeout);
    } catch (ConnectException e) {
    
    
      ConnectException ce = new ConnectException("Failed to connect to " + route.socketAddress());
      ce.initCause(e);
      throw ce;
    }
   try {
    
    
      source = Okio.buffer(Okio.source(rawSocket));
      sink = Okio.buffer(Okio.sink(rawSocket));
    } catch (NullPointerException npe) {
    
    
      if (NPE_THROW_WITH_NULL.equals(npe.getMessage())) {
    
    
        throw new IOException(npe);
      }
    }
}

CallServerInterceptor

CallServerInterceptor的intercept()方法里负责发送请求和获取响应。

具体操作都是通过Exchange来执行,Exchange通过各个功能模块再进行分发处理。
通过 Socket 发送 HTTP消息,会按照以下声明周期:

writeRequestHeaders发送 request Headers
如果有 request body,就通过 Sink 发送request body,然后关闭 Sink

readResponseHeaders获取 response Headers
通过Source读取 response body,然后关闭 Source

writeRequestHeaders

public void writeRequestHeaders(Request request) throws IOException {
    
    
    try {
    
    
      eventListener.requestHeadersStart(call);
      codec.writeRequestHeaders(request);
      eventListener.requestHeadersEnd(call, request);
    } catch (IOException e) {
    
    
      eventListener.requestFailed(call, e);
      trackFailure(e);
      throw e;
    }
  }
  
//实际执行的方法codec实现类Http1ExchangeCodec(前面根据HTTP协议版本选择)的writeRequest方法
/** Returns bytes of a request header for sending on an HTTP transport. */
public void writeRequest(Headers headers, String requestLine) throws IOException {
    
    
  if (state != STATE_IDLE) throw new IllegalStateException("state: " + state);
  sink.writeUtf8(requestLine).writeUtf8("\r\n");
  for (int i = 0, size = headers.size(); i < size; i++) {
    
    
    sink.writeUtf8(headers.name(i))
        .writeUtf8(": ")
        .writeUtf8(headers.value(i))
        .writeUtf8("\r\n");
  }
  sink.writeUtf8("\r\n");
  state = STATE_OPEN_REQUEST_BODY;
}

readResponseHeaders

@Override public Response.Builder readResponseHeaders(boolean expectContinue) throws IOException {
    
    
  if (state != STATE_OPEN_REQUEST_BODY && state != STATE_READ_RESPONSE_HEADERS) {
    
    
    throw new IllegalStateException("state: " + state);
  }
  try {
    
    
    StatusLine statusLine = StatusLine.parse(readHeaderLine());
    Response.Builder responseBuilder = new Response.Builder()
        .protocol(statusLine.protocol)
        .code(statusLine.code)
        .message(statusLine.message)//StatusLine解析HTTP版本信息
        .headers(readHeaders());//readHeaders()读取response header信息。
    if (expectContinue && statusLine.code == HTTP_CONTINUE) {
    
    
      return null;
    } else if (statusLine.code == HTTP_CONTINUE) {
    
    
      state = STATE_READ_RESPONSE_HEADERS;
      return responseBuilder;
    }
    state = STATE_OPEN_RESPONSE_BODY;
    return responseBuilder;
  } catch (EOFException e) {
    
    
    // Provide more context if the server ends the stream before sending a response.
    String address = "unknown";
    if (realConnection != null) {
    
    
      address = realConnection.route().address().url().redact();
    }
    throw new IOException("unexpected end of stream on "
        + address, e);
  }
}

response Body

if (forWebSocket && code == 101) {
    
    
  // Connection is upgrading, but we need to ensure interceptors see a non-null response body.
  response = response.newBuilder()
      .body(Util.EMPTY_RESPONSE)
      .build();
} else {
    
    
  response = response.newBuilder()
      .body(exchange.openResponseBody(response))
      .build();
}

public ResponseBody openResponseBody(Response response) throws IOException {
    
    
  try {
    
    
    eventListener.responseBodyStart(call);
    String contentType = response.header("Content-Type");
    long contentLength = codec.reportedContentLength(response);
    Source rawSource = codec.openResponseBodySource(response);
    ResponseBodySource source = new ResponseBodySource(rawSource, contentLength);
    return new RealResponseBody(contentType, contentLength, Okio.buffer(source));
  } catch (IOException e) {
    
    
    eventListener.responseFailed(call, e);
    trackFailure(e);
    throw e;
  }
}

猜你喜欢

转载自blog.csdn.net/weixin_51109304/article/details/131195521