美文网首页
okhttp——RetryAndFollowUpIntercep

okhttp——RetryAndFollowUpIntercep

作者: oceanLong | 来源:发表于2019-05-06 16:55 被阅读0次

    简介

    okhttp的网络请求采用interceptors链的模式。每一级interceptor只处理自己的工作,然后将剩余的工作,交给下一级interceptor。本文将主要阅读okhttp中的RetryAndFollowUpInterceptor,了解它的作用和工作原理。

    RetryAndFollowUpInterceptor

    顾名思义,RetryAndFollowUpInterceptor负责okhttp的请求失败的恢复和重定向。

    核心的intercept方法分两段阅读:

    
      @Override public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        RealInterceptorChain realChain = (RealInterceptorChain) chain;
        Transmitter transmitter = realChain.transmitter();
    
        int followUpCount = 0;
        Response priorResponse = null;
        while (true) {
          transmitter.prepareToConnect(request);
    
          if (transmitter.isCanceled()) {
            throw new IOException("Canceled");
          }
    
          Response response;
          boolean success = false;
          try {
            response = realChain.proceed(request, transmitter, null);
            success = true;
          } catch (RouteException e) {
            // The attempt to connect via a route failed. The request will not have been sent.
            if (!recover(e.getLastConnectException(), transmitter, false, request)) {
              throw e.getFirstConnectException();
            }
            continue;
          } catch (IOException e) {
            // An attempt to communicate with a server failed. The request may have been sent.
            boolean requestSendStarted = !(e instanceof ConnectionShutdownException);
            if (!recover(e, transmitter, requestSendStarted, request)) throw e;
            continue;
          } finally {
            // The network call threw an exception. Release any resources.
            if (!success) {
              transmitter.exchangeDoneDueToException();
            }
          }
          ...
        }
      }
    

    前半段的逻辑中,RetryAndFollowUpInterceptor做了几件事:

    • 通过Transmitter准备连接
    • 执行请求链下一级
    • 处理了下一级请求链中的RouteExceptionIOException

    Transmitter的实现,以后的章节再单独讲解。此处略过。我们重点看一下,RetryAndFollowUpInterceptor如何处理两个异常。

    RouteException

    从注释中,我们可以看到,RouteException表示客户端连接路由失败。此时会调用recover方法,如果recover方法再失败,会抛出RouteException中的FirstConnectException

    我们看一下recover方法的实现:

      /**
       * Report and attempt to recover from a failure to communicate with a server. Returns true if
       * {@code e} is recoverable, or false if the failure is permanent. Requests with a body can only
       * be recovered if the body is buffered or if the failure occurred before the request has been
       * sent.
       */
      private boolean recover(IOException e, Transmitter transmitter,
          boolean requestSendStarted, Request userRequest) {
        // The application layer has forbidden retries.
        if (!client.retryOnConnectionFailure()) return false;
    
        // We can't send the request body again.
        if (requestSendStarted && requestIsOneShot(e, userRequest)) return false;
    
        // This exception is fatal.
        if (!isRecoverable(e, requestSendStarted)) return false;
    
        // No more routes to attempt.
        if (!transmitter.canRetry()) return false;
    
        // For failure recovery, use the same route selector with a new connection.
        return true;
      }
    

    首先我们调用应用层的失败回调,如果应用层返回false,就不再进行重试。

    然后,我们判断请求的返回,如果请求已经开始或请求限定,只能请求一次,我们也不再进行重试。其中,只能请求一次,可能是客户端自行设定的,也可能是请求返回了404。明确告知了文件不存在,也不会再重复请求。

    接下来,是okhttp认为的致命错误,不会再重复请求的,都会在isRecoverable方法中。致命错误包括:协议错误、SSL校验错误等。

      private boolean isRecoverable(IOException e, boolean requestSendStarted) {
        // If there was a protocol problem, don't recover.
        if (e instanceof ProtocolException) {
          return false;
        }
    
        // If there was an interruption don't recover, but if there was a timeout connecting to a route
        // we should try the next route (if there is one).
        if (e instanceof InterruptedIOException) {
          return e instanceof SocketTimeoutException && !requestSendStarted;
        }
    
        // Look for known client-side or negotiation errors that are unlikely to be fixed by trying
        // again with a different route.
        if (e instanceof SSLHandshakeException) {
          // If the problem was a CertificateException from the X509TrustManager,
          // do not retry.
          if (e.getCause() instanceof CertificateException) {
            return false;
          }
        }
        if (e instanceof SSLPeerUnverifiedException) {
          // e.g. a certificate pinning error.
          return false;
        }
    
        // An example of one we might want to retry with a different route is a problem connecting to a
        // proxy and would manifest as a standard IOException. Unless it is one we know we should not
        // retry, we return true and try a new route.
        return true;
      }
    

    最后,在底层中寻找是否还有其他的Router可以尝试。

    IOException

    IOException表示连接已经建立,但读取内容时失败了。我们同样会进行recover尝试,由于代码逻辑一样,不再重复阅读。

    在finally中,Transmitter会释放所有资源。


    followUpRequest

    接下来,我们看一下RetryAndFollowUpInterceptorintercept后半段的实现:

      @Override public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        RealInterceptorChain realChain = (RealInterceptorChain) chain;
        Transmitter transmitter = realChain.transmitter();
    
        int followUpCount = 0;
        Response priorResponse = null;
        while (true) {
         ...
    
          // 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();
          }
    
          Exchange exchange = Internal.instance.exchange(response);
          Route route = exchange != null ? exchange.connection().route() : null;
          Request followUp = followUpRequest(response, route);
    
          if (followUp == null) {
            if (exchange != null && exchange.isDuplex()) {
              transmitter.timeoutEarlyExit();
            }
            return response;
          }
    
          RequestBody followUpBody = followUp.body();
          if (followUpBody != null && followUpBody.isOneShot()) {
            return response;
          }
    
          closeQuietly(response.body());
          if (transmitter.hasExchange()) {
            exchange.detachWithViolence();
          }
    
          if (++followUpCount > MAX_FOLLOW_UPS) {
            throw new ProtocolException("Too many follow-up requests: " + followUpCount);
          }
    
          request = followUp;
          priorResponse = response;
        }
      }
    

    我们拆开来看这段复杂的逻辑。大体上来说,这段逻辑主要是通过上次请求的返回,生成followUp。然后根据followUp的内容,判断是不是有效的返回。如果返回是有效的,就直接return请求的返回。如果返回无效,则request=followUp,重走while循环,重新请求。

    所以这一段的核心逻辑在于followUpRequest方法。我们来看下followUpRequest的实现。

      /**
       * Figures out the HTTP request to make in response to receiving {@code userResponse}. This will
       * either add authentication headers, follow redirects or handle a client request timeout. If a
       * follow-up is either unnecessary or not applicable, this returns null.
       */
      private Request followUpRequest(Response userResponse, @Nullable Route route) throws IOException {
        if (userResponse == null) throw new IllegalStateException();
        int responseCode = userResponse.code();
    
        final String method = userResponse.request().method();
        switch (responseCode) {
          case HTTP_PROXY_AUTH:
            Proxy selectedProxy = route != null
                ? route.proxy()
                : client.proxy();
            if (selectedProxy.type() != Proxy.Type.HTTP) {
              throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy");
            }
            return client.proxyAuthenticator().authenticate(route, userResponse);
    
          case HTTP_UNAUTHORIZED:
            return client.authenticator().authenticate(route, userResponse);
    
          case HTTP_PERM_REDIRECT:
          case HTTP_TEMP_REDIRECT:
            // "If the 307 or 308 status code is received in response to a request other than GET
            // or HEAD, the user agent MUST NOT automatically redirect the request"
            if (!method.equals("GET") && !method.equals("HEAD")) {
              return null;
            }
            // fall-through
          case HTTP_MULT_CHOICE:
          case HTTP_MOVED_PERM:
          case HTTP_MOVED_TEMP:
          case HTTP_SEE_OTHER:
            // Does the client allow redirects?
            if (!client.followRedirects()) return null;
    
            String location = userResponse.header("Location");
            if (location == null) return null;
            HttpUrl url = userResponse.request().url().resolve(location);
    
            // Don't follow redirects to unsupported protocols.
            if (url == null) return null;
    
            // If configured, don't follow redirects between SSL and non-SSL.
            boolean sameScheme = url.scheme().equals(userResponse.request().url().scheme());
            if (!sameScheme && !client.followSslRedirects()) return null;
    
            // Most redirects don't include a request body.
            Request.Builder requestBuilder = userResponse.request().newBuilder();
            if (HttpMethod.permitsRequestBody(method)) {
              final boolean maintainBody = HttpMethod.redirectsWithBody(method);
              if (HttpMethod.redirectsToGet(method)) {
                requestBuilder.method("GET", null);
              } else {
                RequestBody requestBody = maintainBody ? userResponse.request().body() : null;
                requestBuilder.method(method, requestBody);
              }
              if (!maintainBody) {
                requestBuilder.removeHeader("Transfer-Encoding");
                requestBuilder.removeHeader("Content-Length");
                requestBuilder.removeHeader("Content-Type");
              }
            }
    
            // When redirecting across hosts, drop all authentication headers. This
            // is potentially annoying to the application layer since they have no
            // way to retain them.
            if (!sameConnection(userResponse.request().url(), url)) {
              requestBuilder.removeHeader("Authorization");
            }
    
            return requestBuilder.url(url).build();
    
          case HTTP_CLIENT_TIMEOUT:
            // 408's are rare in practice, but some servers like HAProxy use this response code. The
            // spec says that we may repeat the request without modifications. Modern browsers also
            // repeat the request (even non-idempotent ones.)
            if (!client.retryOnConnectionFailure()) {
              // The application layer has directed us not to retry the request.
              return null;
            }
    
            RequestBody requestBody = userResponse.request().body();
            if (requestBody != null && requestBody.isOneShot()) {
              return null;
            }
    
            if (userResponse.priorResponse() != null
                && userResponse.priorResponse().code() == HTTP_CLIENT_TIMEOUT) {
              // We attempted to retry and got another timeout. Give up.
              return null;
            }
    
            if (retryAfter(userResponse, 0) > 0) {
              return null;
            }
    
            return userResponse.request();
    
          case HTTP_UNAVAILABLE:
            if (userResponse.priorResponse() != null
                && userResponse.priorResponse().code() == HTTP_UNAVAILABLE) {
              // We attempted to retry and got another timeout. Give up.
              return null;
            }
    
            if (retryAfter(userResponse, Integer.MAX_VALUE) == 0) {
              // specifically received an instruction to retry without delay
              return userResponse.request();
            }
    
            return null;
    
          default:
            return null;
        }
      }
    

    这段代码非常长,大部分是switch/case的各种返回码处理。followUpRequest方法从宏观上来讲,是输入response,生成新的requests。如果response的内容不需要重试,则直接返回null。如果需要重试,则根据response的内容,生成重试策略,返回重试发出的request。

    其中,重定向和超时是最主要的重试情况。在处理重定向和超时时,okhttp进行了很多判断,排除了一些不必要重试的情况。如,location不存在,或者重定向的url协议头不一致等情况。

    followUpCount则是为了限制okhttp的重试次数。


    总结

    RetryAndFollowUpInterceptorokhttp中承担了重试和重定向的逻辑。其中包括了,建立连接、读取内容失败的重试 和 完整读取请求返回后的重定向。针对各种返回码,okhttp对无需重试的一些场景进行了裁剪,减少了无效重试的概率。同时,对不规范的重定向返回进行的过滤和校验。

    网络请求的场景复杂,在设计网络框架时,对于各种未知情况的处理,是一项比较有挑战的工作。okhttp作为一个高可用的网络框架,在RetryAndFollowUpInterceptor这一拦截器中,提供了一个异常处理的优秀范本。

    当读者需要自己设计网络库时,可以参考okhttpRetryAndFollowUpInterceptor对于异常处理的做法,避免一些难以预测和重现的问题。

    如有问题,欢迎指正。

    相关文章

      网友评论

          本文标题:okhttp——RetryAndFollowUpIntercep

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