欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AsyncHttpClient KeepAliveStrategy源碼流程解讀

 更新時(shí)間:2023年12月12日 08:35:19   作者:codecraft  
這篇文章主要為大家介紹了AsyncHttpClient KeepAliveStrategy源碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

本文主要研究一下AsyncHttpClient的KeepAliveStrategy

KeepAliveStrategy

org/asynchttpclient/channel/KeepAliveStrategy.java

public interface KeepAliveStrategy {

  /**
   * Determines whether the connection should be kept alive after this HTTP message exchange.
   *
   * @param ahcRequest    the Request, as built by AHC
   * @param nettyRequest  the HTTP request sent to Netty
   * @param nettyResponse the HTTP response received from Netty
   * @return true if the connection should be kept alive, false if it should be closed.
   */
  boolean keepAlive(Request ahcRequest, HttpRequest nettyRequest, HttpResponse nettyResponse);
}
KeepAliveStrategy接口定義了keepAlive方法用于決定是否對(duì)該connection進(jìn)行keep alive

DefaultKeepAliveStrategy

org/asynchttpclient/channel/DefaultKeepAliveStrategy.java

/**
 * Connection strategy implementing standard HTTP 1.0/1.1 behavior.
 */
public class DefaultKeepAliveStrategy implements KeepAliveStrategy {

  /**
   * Implemented in accordance with RFC 7230 section 6.1 https://tools.ietf.org/html/rfc7230#section-6.1
   */
  @Override
  public boolean keepAlive(Request ahcRequest, HttpRequest request, HttpResponse response) {
    return HttpUtil.isKeepAlive(response)
            && HttpUtil.isKeepAlive(request)
            // support non standard Proxy-Connection
            && !response.headers().contains("Proxy-Connection", CLOSE, true);
  }
}
DefaultKeepAliveStrategy實(shí)現(xiàn)了KeepAliveStrategy接口,其keepAlive方法判根據(jù)HTTP 1.0/1.1協(xié)議的規(guī)定進(jìn)行判斷,需要request、response都是keep alive,且response header不包含Proxy-Connection: close才返回true

HttpUtil

io/netty/handler/codec/http/HttpUtil.java

/**
     * Returns {@code true} if and only if the connection can remain open and
     * thus 'kept alive'.  This methods respects the value of the.
     *
     * {@code "Connection"} header first and then the return value of
     * {@link HttpVersion#isKeepAliveDefault()}.
     */
    public static boolean isKeepAlive(HttpMessage message) {
        return !message.headers().containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE, true) &&
               (message.protocolVersion().isKeepAliveDefault() ||
                message.headers().containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE, true));
    }
isKeepAlive方法在HttpMessage沒(méi)有connection: close的header,且http協(xié)議默認(rèn)keep alive或者h(yuǎn)eader包含了connection: keep-alive才返回true

handleHttpResponse

org/asynchttpclient/netty/handler/HttpHandler.java

private void handleHttpResponse(final HttpResponse response, final Channel channel, final NettyResponseFuture<?> future, AsyncHandler<?> handler) throws Exception {
    HttpRequest httpRequest = future.getNettyRequest().getHttpRequest();
    logger.debug("\n\nRequest {}\n\nResponse {}\n", httpRequest, response);
    future.setKeepAlive(config.getKeepAliveStrategy().keepAlive(future.getTargetRequest(), httpRequest, response));
    NettyResponseStatus status = new NettyResponseStatus(future.getUri(), response, channel);
    HttpHeaders responseHeaders = response.headers();
    if (!interceptors.exitAfterIntercept(channel, future, handler, response, status, responseHeaders)) {
      boolean abort = abortAfterHandlingStatus(handler, status) || //
              abortAfterHandlingHeaders(handler, responseHeaders) || //
              abortAfterHandlingReactiveStreams(channel, future, handler);
      if (abort) {
        finishUpdate(future, channel, true);
      }
    }
  }
HttpHandler的handleHttpResponse方法會(huì)通過(guò)KeepAliveStrategy的keepAlive來(lái)判斷是否需要keep alive,然后寫(xiě)入到NettyResponseFuture中

exitAfterHandlingConnect

org/asynchttpclient/netty/handler/intercept/ConnectSuccessInterceptor.java

public boolean exitAfterHandlingConnect(Channel channel,
                                          NettyResponseFuture<?> future,
                                          Request request,
                                          ProxyServer proxyServer) {
    if (future.isKeepAlive())
      future.attachChannel(channel, true);
    Uri requestUri = request.getUri();
    LOGGER.debug("Connecting to proxy {} for scheme {}", proxyServer, requestUri.getScheme());
    channelManager.updatePipelineForHttpTunneling(channel.pipeline(), requestUri);
    future.setReuseChannel(true);
    future.setConnectAllowed(false);
    requestSender.drainChannelAndExecuteNextRequest(channel, future, new RequestBuilder(future.getTargetRequest()).build());
    return true;
  }
exitAfterHandlingConnect方法在NettyResponseFuture的keep alive為true時(shí)執(zhí)行future.attachChannel(channel, true)

attachChannel

org/asynchttpclient/netty/NettyResponseFuture.java

public void attachChannel(Channel channel, boolean reuseChannel) {
    // future could have been cancelled first
    if (isDone()) {
      Channels.silentlyCloseChannel(channel);
    }
    this.channel = channel;
    this.reuseChannel = reuseChannel;
  }
  public boolean isReuseChannel() {
    return reuseChannel;
  }
attachChannel這里維護(hù)了reuseChannel屬性

getOpenChannel

org/asynchttpclient/netty/request/NettyRequestSender.java

private Channel getOpenChannel(NettyResponseFuture<?> future, Request request, ProxyServer proxyServer,
                                 AsyncHandler<?> asyncHandler) {
    if (future != null && future.isReuseChannel() && Channels.isChannelActive(future.channel())) {
      return future.channel();
    } else {
      return pollPooledChannel(request, proxyServer, asyncHandler);
    }
  }
  private Channel pollPooledChannel(Request request, ProxyServer proxy, AsyncHandler<?> asyncHandler) {
    try {
      asyncHandler.onConnectionPoolAttempt();
    } catch (Exception e) {
      LOGGER.error("onConnectionPoolAttempt crashed", e);
    }
    Uri uri = request.getUri();
    String virtualHost = request.getVirtualHost();
    final Channel channel = channelManager.poll(uri, virtualHost, proxy, request.getChannelPoolPartitioning());
    if (channel != null) {
      LOGGER.debug("Using pooled Channel '{}' for '{}' to '{}'", channel, request.getMethod(), uri);
    }
    return channel;
  }
getOpenChannel先判斷NettyResponseFuture是否是reuse的,以及是否active,若是則直接返回future.channel(),否則通過(guò)pollPooledChannel從連接池中獲取

小結(jié)

AsyncHttpClient的KeepAliveStrategy定義了keepAlive方法用于決定是否對(duì)該connection進(jìn)行keep alive;HttpHandler的handleHttpResponse方法會(huì)通過(guò)KeepAliveStrategy的keepAlive來(lái)判斷是否需要keep alive,然后寫(xiě)入到NettyResponseFuture中;getOpenChannel先判斷NettyResponseFuture是否是reuse的,以及是否active,若是則直接返回future.channel(),否則通過(guò)pollPooledChannel從連接池中獲取。

以上就是AsyncHttpClient KeepAliveStrategy源碼流程解讀的詳細(xì)內(nèi)容,更多關(guān)于AsyncHttpClient KeepAliveStrategy的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java獲取視頻時(shí)長(zhǎng)及截取幀截圖詳解

    Java獲取視頻時(shí)長(zhǎng)及截取幀截圖詳解

    這篇文章主要介紹了Java獲取視頻時(shí)長(zhǎng)及截取幀截圖詳解,以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。,需要的朋友可以參考下
    2019-06-06
  • mybatis項(xiàng)目兼容mybatis-plus問(wèn)題

    mybatis項(xiàng)目兼容mybatis-plus問(wèn)題

    這篇文章主要介紹了mybatis項(xiàng)目兼容mybatis-plus問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • spring打包到j(luò)ar包的問(wèn)題解決

    spring打包到j(luò)ar包的問(wèn)題解決

    這篇文章主要給大家介紹了關(guān)于spring打包到j(luò)ar包遇到的問(wèn)題的解決方法,文中通過(guò)實(shí)例代碼結(jié)束的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用spring打包具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Hibernate原理及應(yīng)用

    Hibernate原理及應(yīng)用

    本文主要介紹了Hibernate原理及應(yīng)用。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-02-02
  • Spring?Data?JPA注解Entity使用示例詳解

    Spring?Data?JPA注解Entity使用示例詳解

    這篇文章主要為大家介紹了Spring?Data?JPA注解Entity使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Spring中屬性注入詳解

    Spring中屬性注入詳解

    這篇文章主要為大家詳細(xì)介紹了Spring中屬性注入,演示了int、String、數(shù)組、list等屬性的注入,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • 關(guān)于spring中事務(wù)的傳播機(jī)制

    關(guān)于spring中事務(wù)的傳播機(jī)制

    這篇文章主要介紹了關(guān)于spring中事務(wù)的傳播機(jī)制,所謂事務(wù)傳播機(jī)制,也就是在事務(wù)在多個(gè)方法的調(diào)用中是如何傳遞的,是重新創(chuàng)建事務(wù)還是使用父方法的事務(wù),需要的朋友可以參考下
    2023-05-05
  • Java實(shí)現(xiàn)簡(jiǎn)單訂餐系統(tǒng)

    Java實(shí)現(xiàn)簡(jiǎn)單訂餐系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單訂餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • SpringBoot項(xiàng)目中分頁(yè)插件PageHelper無(wú)效的問(wèn)題及解決方法

    SpringBoot項(xiàng)目中分頁(yè)插件PageHelper無(wú)效的問(wèn)題及解決方法

    這篇文章主要介紹了解決SpringBoot項(xiàng)目中分頁(yè)插件PageHelper無(wú)效的問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Java后端學(xué)習(xí)精華之TCP通信傳輸協(xié)議詳解

    Java后端學(xué)習(xí)精華之TCP通信傳輸協(xié)議詳解

    TCP/IP是一種面向連接的、可靠的、基于字節(jié)流的傳輸層通信協(xié)議,它會(huì)保證數(shù)據(jù)不丟包、不亂序。TCP全名是Transmission Control Protocol,它是位于網(wǎng)絡(luò)OSI模型中的第四層
    2021-09-09

最新評(píng)論