okhttp的分发器和拦截器

117 阅读8分钟

使用

get

OkHttpClient client=new OkHttpClient();
Request request=new Request.Builder()
        .url("www.baidu.com")
        .build();
Call call=client.newCall(request);
Response response=call.execute();
ResponseBody responseBody=response.body();

post

OkHttpClient client=new OkHttpClient();
RequestBody requestBody=new FormBody.Builder().add("li","bo")
        .build();
Request request=new Request.Builder()
        .url("www.baidu.com")
        .post(requestBody)
        .build();
Call call=client.newCall(request);
Response response=call.execute();
ResponseBody responseBody=response.body();

调用流程

image.png

请求在执行前,先通过分发器再通过拦截器,最终执行的最后的操作

  • 分发器的作用:内部维护队列和线程池,来完成请求调配
  • 拦截器 五大拦截器进行钩子拦截 最终完成请求过程

分发器

异步请求

image.png

  • 根据当前的线程池的空闲数量来执行等待和立即执行
synchronized void enqueue(AsyncCall call) {
    //todo : 1、如果正在执行的请求小于64
    // 2、相同host的请求不能超过5个
    if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
        runningAsyncCalls.add(call);  //正在执行的请求
        executorService().execute(call); //线程池跑任务
    } else {
        readyAsyncCalls.add(call);
    }
}
  • 当运行数小于最大值,并且有等待执行的任务。检测遍历readyAsyncCalls。加入到runningAsyncCalls 并执行
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();
        // 同一Host请求只能同时有5个
        if (runningCallsForHost(call) < maxRequestsPerHost) {
            i.remove();
            runningAsyncCalls.add(call);
            executorService().execute(call);
        }

        if (runningAsyncCalls.size() >= maxRequests) return; // Reached max capacity.
    }
}

同步请求

立即执行,只判断IdleRunnable 是否执行

线程池

在runningAsyncCalls 线程池执行,无等待,最大程度并发

拦截器

在call 执行请求执行了拦截器 getResponseWithInterceptorChain() 获取请求的结果

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

image.png

retryAndFollowUpInterceptor

重试拦截器: 判断用户是否取消请求,在获得结果后,会根据响应码判断是否需要重定向,如果满足条件会重新启动所有的拦截器

public Response intercept(Chain chain) throws IOException {

    Request request = chain.request();
    RealInterceptorChain realChain = (RealInterceptorChain) chain;
    Call call = realChain.call();
    EventListener eventListener = realChain.eventListener();

    /**
     * todo  管理类,维护了 与服务器的连接、数据流与请求三者的关系。真正使用的拦截器为 Connect
     */
    StreamAllocation streamAllocation = new StreamAllocation(client.connectionPool(),
            createAddress(request.url()), call, eventListener, callStackTrace);
    this.streamAllocation = streamAllocation;

    int followUpCount = 0;
    Response priorResponse = null;
    while (true) {
        if (canceled) {
            streamAllocation.release();
            throw new IOException("Canceled");
        }

        Response response;
        boolean releaseConnection = true;
        try {
            //todo 请求出现了异常,那么releaseConnection依旧为true。
            response = realChain.proceed(request, streamAllocation, null, null);
            releaseConnection = false;
        } catch (RouteException e) {
            //todo 路由异常,连接未成功,请求还没发出去
            //The attempt to connect via a route failed. The request will not have been sent.
            if (!recover(e.getLastConnectException(), streamAllocation, false, request)) {
                throw e.getLastConnectException();
            }
            releaseConnection = false;
            continue;
        } catch (IOException e) {
            //todo 请求发出去了,但是和服务器通信失败了。(socket流正在读写数据的时候断开连接)
            // HTTP2才会抛出ConnectionShutdownException。所以对于HTTP1 requestSendStarted一定是true
            //An attempt to communicate with a server failed. The request may have been sent.
            boolean requestSendStarted = !(e instanceof ConnectionShutdownException);
            if (!recover(e, streamAllocation, requestSendStarted, request)) throw e;
            releaseConnection = false;
            continue;
        } finally {
            // We're throwing an unchecked exception. Release any resources.
            //todo 不是前两种的失败,那直接关闭清理所有资源
            if (releaseConnection) {
                streamAllocation.streamFailed(null);
                streamAllocation.release();
            }
        }
        //todo 如果进过重试/重定向才成功的,则在本次响应中记录上次响应的情况
        //Attach the prior response if it exists. Such responses never have a body.
        if (priorResponse != null) {
            response = response.newBuilder()
                    .priorResponse(
                            priorResponse.newBuilder()
                                    .body(null)
                                    .build()
                    )
                    .build();
        }
        //todo 处理3和4xx的一些状态码,如301 302重定向
        Request followUp = followUpRequest(response, streamAllocation.route());
        if (followUp == null) {
            if (!forWebSocket) {
                streamAllocation.release();
            }
            return response;
        }

        closeQuietly(response.body());

        //todo 限制最大 followup 次数为20次
        if (++followUpCount > MAX_FOLLOW_UPS) {
            streamAllocation.release();
            throw new ProtocolException("Too many follow-up requests: " + followUpCount);
        }

        if (followUp.body() instanceof UnrepeatableRequestBody) {
            streamAllocation.release();
            throw new HttpRetryException("Cannot retry streamed HTTP body", response.code());
        }
        //todo 判断是不是可以复用同一份连接
        if (!sameConnection(response, followUp.url())) {
            streamAllocation.release();
            streamAllocation = new StreamAllocation(client.connectionPool(),
                    createAddress(followUp.url()), call, eventListener, callStackTrace);
            this.streamAllocation = streamAllocation;
        } else if (streamAllocation.codec() != null) {
            throw new IllegalStateException("Closing the body of " + response
                    + " didn't close its backing stream. Bad interceptor?");
        }

        request = followUp;
        priorResponse = response;
    }
}

此拦while true 循环来等待异常冒泡,

  1. 请求异常,进行重试
  2. 路由异常,进行重试
  3. 不是1和2 释放所有资源
  4. 果进过重试/重定向才成功的,则在本次响应中记录上次响应的情况
  5. 处理3和4xx的一些状态码,如301 302重定向
  6. 限制最大 followup 次数为20次

BridgeInterceptor

侨接拦截器:将HTTP 协议的请求头加入其中,比如host,并添加一些默认行为比如 GZIP。在获得结果后,调用cookie ,并解析Gzip

ublic Response intercept(Chain chain) throws IOException {
    Request userRequest = chain.request();
    Request.Builder requestBuilder = userRequest.newBuilder();

    RequestBody body = userRequest.body();
    if (body != null) {
        MediaType contentType = body.contentType();
        if (contentType != null) {
            requestBuilder.header("Content-Type", contentType.toString());
        }

        long contentLength = body.contentLength();
        if (contentLength != -1) {
            requestBuilder.header("Content-Length", Long.toString(contentLength));
            requestBuilder.removeHeader("Transfer-Encoding");
        } else {
            requestBuilder.header("Transfer-Encoding", "chunked");
            requestBuilder.removeHeader("Content-Length");
        }
    }

    if (userRequest.header("Host") == null) {
        requestBuilder.header("Host", hostHeader(userRequest.url(), false));
    }

    if (userRequest.header("Connection") == null) {
        requestBuilder.header("Connection", "Keep-Alive");
    }

    // If we add an "Accept-Encoding: gzip" header field we're responsible for also
  // decompressing
    // the transfer stream.
    boolean transparentGzip = false;
    if (userRequest.header("Accept-Encoding") == null && userRequest.header("Range") == null) {
        transparentGzip = true;
        requestBuilder.header("Accept-Encoding", "gzip");
    }

    List<Cookie> cookies = cookieJar.loadForRequest(userRequest.url());
    if (!cookies.isEmpty()) {
        requestBuilder.header("Cookie", cookieHeader(cookies));
    }

    if (userRequest.header("User-Agent") == null) {
        requestBuilder.header("User-Agent", Version.userAgent());
    }

    Response networkResponse = chain.proceed(requestBuilder.build());

    HttpHeaders.receiveHeaders(cookieJar, userRequest.url(), networkResponse.headers());

    Response.Builder responseBuilder = networkResponse.newBuilder()
            .request(userRequest);

    if (transparentGzip
            && "gzip".equalsIgnoreCase(networkResponse.header("Content-Encoding"))
            && HttpHeaders.hasBody(networkResponse)) {
        GzipSource responseBody = new GzipSource(networkResponse.body().source());
        Headers strippedHeaders = networkResponse.headers().newBuilder()
                .removeAll("Content-Encoding")
                .removeAll("Content-Length")
                .build();
        responseBuilder.headers(strippedHeaders);
        String contentType = networkResponse.header("Content-Type");
        responseBuilder.body(new RealResponseBody(contentType, -1L, Okio.buffer(responseBody)));
    }

    return responseBuilder.build();
}

CacheInterceptor

缓存拦截器: 是否使用缓存的数据,拿到结果是否缓存

public Response intercept(Chain chain) throws IOException {
    //todo 通过url的md5数据 从文件缓存查找 (GET请求才有缓存)
    Response cacheCandidate = cache != null
            ? cache.get(chain.request())
            : null;

    long now = System.currentTimeMillis();

    //todo 缓存策略:根据各种条件(请求头)组成 请求与缓存
    CacheStrategy strategy =
            new CacheStrategy.Factory(now, chain.request(), cacheCandidate).get();
    //
    Request networkRequest = strategy.networkRequest;
    Response cacheResponse = strategy.cacheResponse;

    if (cache != null) {
        cache.trackResponse(strategy);
    }

    if (cacheCandidate != null && cacheResponse == null) {
        closeQuietly(cacheCandidate.body()); // The cache candidate wasn't applicable. Close it.
    }

    //todo 没有网络请求也没有缓存
    //If we're forbidden from using the network and the cache is insufficient, fail.
    if (networkRequest == null && cacheResponse == null) {
        return new Response.Builder()
                .request(chain.request())
                .protocol(Protocol.HTTP_1_1)
                .code(504)
                .message("Unsatisfiable Request (only-if-cached)")
                .body(Util.EMPTY_RESPONSE)
                .sentRequestAtMillis(-1L)
                .receivedResponseAtMillis(System.currentTimeMillis())
                .build();
    }

    //todo 没有请求,肯定就要使用缓存
    //If we don't need the network, we're done.
    if (networkRequest == null) {
        return cacheResponse.newBuilder()
                .cacheResponse(stripBody(cacheResponse))
                .build();
    }

    //todo 去发起请求
    Response networkResponse = null;
    try {
        networkResponse = chain.proceed(networkRequest);
    } finally {
        // If we're crashing on I/O or otherwise, don't leak the cache body.
        if (networkResponse == null && cacheCandidate != null) {
            closeQuietly(cacheCandidate.body());
        }
    }

    // If we have a cache response too, then we're doing a conditional get.
    if (cacheResponse != null) {
        //todo 服务器返回304无修改,那就使用缓存的响应修改了时间等数据后作为本次请求的响应
        if (networkResponse.code() == HTTP_NOT_MODIFIED) {
            Response response = cacheResponse.newBuilder()
                    .headers(combine(cacheResponse.headers(), networkResponse.headers()))
                    .sentRequestAtMillis(networkResponse.sentRequestAtMillis())
                    .receivedResponseAtMillis(networkResponse.receivedResponseAtMillis())
                    .cacheResponse(stripBody(cacheResponse))
                    .networkResponse(stripBody(networkResponse))
                    .build();
            networkResponse.body().close();

            // Update the cache after combining headers but before stripping the
            // Content-Encoding header (as performed by initContentStream()).
            cache.trackConditionalCacheHit();
            cache.update(cacheResponse, response);
            return response;
        } else {
            closeQuietly(cacheResponse.body());
        }
    }

    //todo 走到这里说明缓存不可用 那就使用网络的响应
    Response response = networkResponse.newBuilder()
            .cacheResponse(stripBody(cacheResponse))
            .networkResponse(stripBody(networkResponse))
            .build();
    //todo 进行缓存
    if (cache != null) {
        if (HttpHeaders.hasBody(response) && CacheStrategy.isCacheable(response,
                networkRequest)) {
            // Offer this request to the cache.
            CacheRequest cacheRequest = cache.put(response);
            return cacheWritingResponse(cacheRequest, response);
        }

        if (HttpMethod.invalidatesCache(networkRequest.method())) {
            try {
                cache.remove(networkRequest);
            } catch (IOException ignored) {
                // The cache cannot be written.
            }
        }
    }

    return response;
}

决定是否需要使用缓存数据,networkRequest,cacheResponse 来决定的

networkRequestcacheResponse说明
nullnot null直接使用缓存
not nullnull发起网路---
not nullnot null发起缓存,得到304 更新缓存返回
nullnull直接返回504

ConnectInterceptor

链接拦截器:负责找到链接,获得对应的Socket流,不进行额外的处理

image.png

@Override 
public Response intercept(Chain chain) throws IOException {
   RealInterceptorChain realChain = (RealInterceptorChain) chain;
   Request request = realChain.request();
    //获取可复用流
   StreamAllocation streamAllocation = realChain.streamAllocation();
   boolean doExtensiveHealthChecks = !request.method().equals("GET");
    //创建输出流
   HttpCodec httpCodec = streamAllocation.newStream(client, doExtensiveHealthChecks);
   //根据HTTP/1.x(keep-alive)和HTTP/2(流复用)的复用机制,发起连接
   RealConnection connection = streamAllocation.connection();
   return realChain.proceed(request, streamAllocation, httpCodec, connection);
 }

寻找健康的链接链接,可复用的复用,不可复用重新创建链接;并开启定期清理无用链接线程池

image.png

CallServerInterceptor

请求服务拦截器: 和服务器进行真正的通信,向服务器发送数据吗,解析读取响应的数据

public final class CallServerInterceptor implements Interceptor 
    @Override
    public Response intercept(Chain chain) throws IOException {
        //这些对象在前面的Interceptor都已经创建完毕
        RealInterceptorChain realChain = (RealInterceptorChain) chain;
        //流处理工具
        HttpCodec httpCodec = realChain.httpStream();
        //流分配调度
        StreamAllocation streamAllocation = realChain.streamAllocation();
        //真正的链接
        RealConnection connection = (RealConnection) realChain.connection();
        //请求
        Request request = realChain.request();

        long sentRequestMillis = System.currentTimeMillis();
        //回调接口
        realChain.eventListener().requestHeadersStart(realChain.call());
        //1. 写入请求头
        //这个方法分析一下,这个httpCodec我们走http2.0分支的
        httpCodec.writeRequestHeaders(request);
        realChain.eventListener().requestHeadersEnd(realChain.call(), request);

        Response.Builder responseBuilder = null;
        //检测一下是否是允许的请求方式,body不为空
        if (HttpMethod.permitsRequestBody(request.method()) && request.body() != null) {
             //一个小知识点,关于"Expect: 100-continue"的
             //1、http 100-continue用于客户端在发送POST数据给服务器前,征询服务器情况,看服务器是否处理POST的数据,如果不处理,客户端则不上传POST数据,如果处理,则POST上传数据。在现实应用中,通过在POST大数据时,才会使用100-continue协议。

          //2、客户端策略。
      //1)如果客户端有POST数据要上传,可以考虑使用100-continue协议。加入头{"Expect":"100-continue"}
      //2)如果没有POST数据,不能使用100-continue协议,因为这会让服务端造成误解。
      //3)并不是所有的Server都会正确实现100-continue协议,如果Client发送Expect:100-continue消息后,在timeout时间内无响应,Client需要立马上传POST数据。
      //4)有些Server会错误实现100-continue协议,在不需要此协议时返回100,此时客户端应该忽略。
    //3、服务端策略。
      //1)正确情况下,收到请求后,返回100或错误码。
      //2)如果在发送100-continue前收到了POST数据(客户端提前发送POST数据),则不发送100响应码(略去)。
            //如果请求中存在“Expect:100-continue”标头,请在发送请求主体之前等待“HTTP / 1.1 100 Continue”响应。
            // 如果我们没有得到,请返回我们得到的内容(例如4xx响应),不继续发送请求主体。
            if ("100-continue".equalsIgnoreCase(request.header("Expect"))) {
                //flush请求头
                httpCodec.flushRequest();
                
                realChain.eventListener().responseHeadersStart(realChain.call());
              //这个方法会进入等待,当后台返回100应答的时候responseBuilder 为空
                //点进去就是调用了stream的takeHeaders()方法获取读到的header
                responseBuilder = httpCodec.readResponseHeaders(true);
            }
            //2 写入请求体
            if (responseBuilder == null) {
                // Write the request body if the "Expect: 100-continue" expectation was met.
                realChain.eventListener().requestBodyStart(realChain.call());
                long contentLength = request.body().contentLength();
                CountingSink requestBodyOut =
                        new CountingSink(httpCodec.createRequestBody(request, contentLength));
                BufferedSink bufferedRequestBody = Okio.buffer(requestBodyOut);

                request.body().writeTo(bufferedRequestBody);
                bufferedRequestBody.close();
                realChain.eventListener()
                        .requestBodyEnd(realChain.call(), requestBodyOut.successfulCount);
            } else if (!connection.isMultiplexed()) {
                // If the "Expect: 100-continue" expectation wasn't met, prevent the HTTP/1 connection
                // from being reused. Otherwise we're still obligated to transmit the request body to
                // leave the connection in a consistent state.
                streamAllocation.noNewStreams();
            }
        }
        //关闭request输出
        httpCodec.finishRequest();

        if (responseBuilder == null) {
            realChain.eventListener().responseHeadersStart(realChain.call());
            responseBuilder = httpCodec.readResponseHeaders(false);
        }

        Response response = responseBuilder
                .request(request)
                .handshake(streamAllocation.connection().handshake())
                .sentRequestAtMillis(sentRequestMillis)
                .receivedResponseAtMillis(System.currentTimeMillis())
                .build();
        //4 读取响应体
        int code = response.code();
        if (code == 100) {
            // server sent a 100-continue even though we did not request one.
            // try again to read the actual response
            responseBuilder = httpCodec.readResponseHeaders(false);

            response = responseBuilder
                    .request(request)
                    .handshake(streamAllocation.connection().handshake())
                    .sentRequestAtMillis(sentRequestMillis)
                    .receivedResponseAtMillis(System.currentTimeMillis())
                    .build();

            code = response.code();
        }

        realChain.eventListener()
                .responseHeadersEnd(realChain.call(), response);
        //空的返回
        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 {
            //组织body
            response = response.newBuilder()
                    .body(httpCodec.openResponseBody(response))
                    .build();
        }
        //标记noNewStreams
        if ("close".equalsIgnoreCase(response.request().header("Connection"))
                || "close".equalsIgnoreCase(response.header("Connection"))) {
            streamAllocation.noNewStreams();
        }
        //协议异常
        if ((code == 204 || code == 205) && response.body().contentLength() > 0) {
            throw new ProtocolException(
                    "HTTP " + code + " had non-zero Content-Length: " + response.body().contentLength());
        }
        //最终返回
        return response;
    }
}

//Http2Codec的writeRequestHeaders()方法
 @Override public void writeRequestHeaders(Request request) throws IOException {
    if (stream != null) return;
  
    boolean hasRequestBody = request.body() != null;
    //组装一下header
    List<Header> requestHeaders = http2HeadersList(request);
    //newStream方法是Http2Connection的,继续追踪下去
    stream = connection.newStream(requestHeaders, hasRequestBody);
    stream.readTimeout().timeout(chain.readTimeoutMillis(), TimeUnit.MILLISECONDS);
    stream.writeTimeout().timeout(chain.writeTimeoutMillis(), TimeUnit.MILLISECONDS);
  }

  /**
   * Returns a new locally-initiated stream.
   * @param out true to create an output stream that we can use to send data to the remote peer.
   * Corresponds to {@code FLAG_FIN}.
   */
//Http2Connection的newStream()方法
  public Http2Stream newStream(List<Header> requestHeaders, boolean out) throws IOException {
    return newStream(0, requestHeaders, out);
  }

  private Http2Stream newStream(
      int associatedStreamId, List<Header> requestHeaders, boolean out) throws IOException {
    boolean outFinished = !out;
    boolean inFinished = false;
    boolean flushHeaders;
    Http2Stream stream;
    int streamId;

    synchronized (writer) {
      synchronized (this) {
        if (nextStreamId > Integer.MAX_VALUE / 2) {
          shutdown(REFUSED_STREAM);
        }
        if (shutdown) {
          throw new ConnectionShutdownException();
        }
        streamId = nextStreamId;
        nextStreamId += 2;
        stream = new Http2Stream(streamId, this, outFinished, inFinished, null);
        flushHeaders = !out || bytesLeftInWriteWindow == 0L || stream.bytesLeftInWriteWindow == 0L;
        if (stream.isOpen()) {
          //这个new出来的stream也保存在streams里,是个map
          //同时会返回赋值给Http2Codec的成员变量stream
          //每次读到数据都会调用Http2Connection的readers()方法把数据存到相应的stream
          streams.put(streamId, stream);
        }
      }
      if (associatedStreamId == 0) {
        //走着里,完成写header操作
        writer.synStream(outFinished, streamId, associatedStreamId, requestHeaders);
      } else if (client) {
        throw new IllegalArgumentException("client streams shouldn't have associated stream IDs");
      } else { // HTTP/2 has a PUSH_PROMISE frame.
        writer.pushPromise(associatedStreamId, streamId, requestHeaders);
      }
    }

    if (flushHeaders) {
      writer.flush();
    }

    return stream;
  }