最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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沒有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ì)通過KeepAliveStrategy的keepAlive來判斷是否需要keep alive,然后寫入到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(),否則通過pollPooledChannel從連接池中獲取

小結(jié)

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

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

相關(guān)文章

  • java日志LoggerFactory.getLogger的用法及說明

    java日志LoggerFactory.getLogger的用法及說明

    這篇文章主要介紹了java日志LoggerFactory.getLogger的用法及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Java日期工具類操作字符串Date和LocalDate互轉(zhuǎn)

    Java日期工具類操作字符串Date和LocalDate互轉(zhuǎn)

    這篇文章主要介紹了Java日期工具類操作字符串Date和LocalDate互轉(zhuǎn),文章首先通過需要先引入坐標(biāo)展開主題的相關(guān)內(nèi)容介紹,需要的朋友可以參一下
    2022-06-06
  • 如何解決多版本jar包沖突問題

    如何解決多版本jar包沖突問題

    本文介紹了如何通過修改jar包全限定名來解決多版本jar包沖突問題,具體步驟包括:準(zhǔn)備jarjar工具和規(guī)則文件、將工具和文件放在同一目錄下、運(yùn)行jarjar工具并生成新的jar包,這種方法有效地解決了由于類全限定名沖突導(dǎo)致的jar包不兼容問題
    2026-02-02
  • java解析XML Node與Element的區(qū)別(推薦)

    java解析XML Node與Element的區(qū)別(推薦)

    下面小編就為大家分享一篇java解析XML Node與Element的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • vue 實(shí)現(xiàn)刪除對(duì)象的元素 delete

    vue 實(shí)現(xiàn)刪除對(duì)象的元素 delete

    這篇文章主要介紹了vue 實(shí)現(xiàn)刪除對(duì)象的元素delete,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • springboot啟動(dòng)掃描不到dao層接口的解決方案

    springboot啟動(dòng)掃描不到dao層接口的解決方案

    這篇文章主要介紹了springboot啟動(dòng)掃描不到dao層接口的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringBoot配置文件啟動(dòng)加載順序的方法步驟

    SpringBoot配置文件啟動(dòng)加載順序的方法步驟

    SpringBoot的啟動(dòng)加載順序涉及多個(gè)步驟和組件,通過分層和優(yōu)先級(jí)機(jī)制加載配置文件,確保在啟動(dòng)時(shí)正確配置應(yīng)用程序,本文就來介紹一下SpringBoot配置文件啟動(dòng)加載順序的方法步驟,感興趣的可以了解一下
    2024-11-11
  • java類成員中的訪問級(jí)別淺析

    java類成員中的訪問級(jí)別淺析

    在本篇文章里小編給大家整理的是一篇關(guān)于java類成員中的訪問級(jí)別淺析內(nèi)容,有興趣的朋友們跟著學(xué)習(xí)下。
    2021-01-01
  • Java中如何將list轉(zhuǎn)為樹形結(jié)構(gòu)

    Java中如何將list轉(zhuǎn)為樹形結(jié)構(gòu)

    這篇文章主要介紹了Java中如何將list轉(zhuǎn)為樹形結(jié)構(gòu),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • java調(diào)用百度的接口獲取起-止位置的距離

    java調(diào)用百度的接口獲取起-止位置的距離

    本文主要介紹了java調(diào)用百度的接口獲取起-止位置的距離,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04

最新評(píng)論

德保县| 忻州市| 南皮县| 蚌埠市| 光山县| 水城县| 建始县| 贵南县| 邳州市| 大兴区| 武胜县| 新乡市| 临桂县| 淮北市| 安国市| 延川县| 德安县| 方山县| 邻水| 湖州市| 吉木乃县| 石狮市| 唐海县| 衡东县| 上饶市| 荥经县| 法库县| 阿鲁科尔沁旗| 治县。| 翁牛特旗| 德阳市| 平邑县| 萝北县| 开封县| 班戈县| 鄄城县| 县级市| 乐安县| 仪陇县| 会宁县| 巩留县|