聊聊AsyncHttpClient的KeepAliveStrategy

本文主要研究一下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方法用于決定是否對該connection進行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*/@Overridepublic 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實現了KeepAliveStrategy接口,其keepAlive方法判根據HTTP 1.0/1.1協議的規定進行判斷,需要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協議默認keep alive或者header包含了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方法會通過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時執行future.attachChannel(channel, true)

attachChannel

org/asynchttpclient/netty/NettyResponseFuture.java

  public void attachChannel(Channel channel, boolean reuseChannel) {// future could have been cancelled firstif (isDone()) {Channels.silentlyCloseChannel(channel);}this.channel = channel;this.reuseChannel = reuseChannel;}public boolean isReuseChannel() {return reuseChannel;}  

attachChannel這里維護了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從連接池中獲取

小結

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/214304.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/214304.shtml
英文地址,請注明出處:http://en.pswp.cn/news/214304.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

進程的相關知識

進程基本概念&#xff1a;1、進程是程序的一次執行過程&#xff0c;進程是資源分配的基本單位&#xff1b;2、每個進程都會分配自己的0至3G的內存空間&#xff0c;這個0至3G的內存空間可以有多份&#xff0c;但是3G至4G的內核空間獨一份&#xff1b;3、進程其實是內核創建的&am…

gitee對接使用

1.創建一個文件夾 2.進入Gitee接受對方項目編輯 3.打開終端初始化一開始創建的文件夾 git init 3.1打開終端 3.2輸入git.init 4.克隆對方的項目 4.1進入Gitee復制對方項目的路徑 4.2在編輯器終端內克隆對方項目 git clone 網址 如此你的編輯器就會出現對方的項目 …

小紅書AI文章寫作工具,免費的小紅書AI寫作工具有哪些

社交媒體已經成為人們交流、分享生活和獲取信息的主要平臺之一。而在這眾多社交媒體中&#xff0c;小紅書以其獨特的社區氛圍和內容特色而備受矚目。如何更高效地進行小紅書文章創作&#xff0c;本文將深入研究小紅書文章AI寫作工具。 小紅書文章AI寫作工具背后的技術 隨著人工…

Java基于Rest Assured自動化測試接口詳解

前言 不知道大家的項目是否都有對接口API進行自動化測試&#xff0c;反正像我們這種小公司是沒有的。由于最近一直被吐槽項目質量糟糕&#xff0c;只能研發自己看看有什么接口測試方案。那么在本文中&#xff0c;我將探索如何使用 Rest Assured 自動化 API 測試&#xff0c;Re…

基于Java SSM框架實現寵物醫院信息管理系統項目【項目源碼】計算機畢業設計

基于java的SSM框架實現寵物醫院信息管理系統演示 java簡介 Java語言是在二十世紀末由Sun公司發布的&#xff0c;而且公開源代碼&#xff0c;這一優點吸引了許多世界各地優秀的編程愛好者&#xff0c;也使得他們開發出當時一款又一款經典好玩的小游戲。Java語言是純面向對象語言…

關于加密解密,加簽驗簽那些事

面對MD5、SHA、DES、AES、RSA等等這些名詞你是否有很多問號&#xff1f;這些名詞都是什么&#xff1f;還有什么公鑰加密、私鑰解密、私鑰加簽、公鑰驗簽。這些都什么鬼&#xff1f;或許在你日常工作沒有聽說過這些名詞&#xff0c;但是一旦你要設計一個對外訪問的接口&#xff…

聚焦中國—東盟大健康產業峰會 點靚廣西“長壽福地”品牌

12月8-10日2023中國—東盟大健康產業峰會暨大健康產業博覽會在南寧國際會展中心成功舉辦&#xff0c;本次峰會由國家中醫藥管理局、廣西壯族自治區人民政府聯合主辦&#xff0c;中國老年學和老年醫學學會、自治區黨委宣傳部、自治區民政廳、廣西壯族自治區外事辦公室、廣西壯族…

MySQL使用窗口函數ROW_NUMBER()、DENSE_RANK()查詢每組第一名或每組前幾名,窗口函數使用詳解

MySQL數據表結構 創建 tbl_class_info 表&#xff0c;表中有四個字段 id、username、score、group_name 使用 ROW_NUMBER()、DENSE_RANK() 查詢每組前三名 -- 查詢每組前3名 SELECT username, score, group_name FROM ( SELECT username, score, group_name, ROW_NUMBER()…

目標檢測——R-FCN算法解讀

論文&#xff1a;R-FCN: Object Detection via Region-based Fully Convolutional Networks 作者&#xff1a;Jifeng Dai, Yi Li, Kaiming He and Jian Sun 鏈接&#xff1a;https://arxiv.org/pdf/1605.06409v2.pdf 代碼&#xff1a;https://github.com/daijifeng001/r-fcn 文…

5.鴻蒙hap可以直接點擊包安裝嗎?

5.鴻蒙hap可以直接點擊包安裝嗎&#xff1f; hap與apk不同&#xff0c;獲取的hap不能直接安裝 安裝方法1&#xff1a; DevEco studio打開項目源文件&#xff0c;打開手機USB調試&#xff0c;DevEco識別到手機后&#xff0c;點擊播放按鈕安裝到手機 https://txwtech.blog.cs…

Rust 通用代碼生成器蓮花發布紅蓮嘗鮮版十八介紹視頻,初學者指南

Rust 通用代碼生成器蓮花發布紅蓮嘗鮮版十八介紹視頻&#xff0c;初學者指南 Rust 通用代碼生成器蓮花發布深度修復版紅蓮嘗鮮版十八介紹視頻&#xff0c;初學者指南&#xff0c;詳細介紹代碼生成器環境搭建&#xff0c;編譯&#xff0c;運行和使用代碼生成物&#xff0c;歡迎…

飛天使-linux操作的一些技巧與知識點6

文章目錄 在議playbook虛擬環境中安裝ansibleplaybook 結合變量的一些演示普通的vars_files 變量&#xff0c;在同級目錄創建目錄使用host_vars 定義變量group_vars定義變量根據不同系統操作不同版本傳遞多個外置變量舉例幾個不同的示例factswhenloophandlers 與 notifytags 任…

nginx中的正則表達式及location和rewrite

目錄 常用的Nginx 正則表達式 location和rewrite的區別 location location 大致可以分為三類 location 常用的匹配規則 location 優先級 location 示例說明 location優先級的總結 rewrite rewrite的功能 rewrite實現跳轉的條件 rewrite的執行順序 rewrite的語法格式…

ARM day3

題目&#xff1a;實現3盞燈的流水 代碼&#xff1a; .text .global _start _start: 設置RCC寄存器使能 LDR R0,0X50000A28 LDR R1,[R0] ORR R1,R1,#(0X1<<4) ORR R1,R1,#(0X1<<5) STR R1,[R0]設置PE10管腳為輸出模式 LDR R0,0X50006000 LDR R1,[R0] BIC R1,R1,…

文心ERNIE Bot SDK+LangChain:基于文檔、網頁的個性化問答系統

現在各行各業紛紛選擇接入大模型&#xff0c;其中最火且可行性最高的形式無異于智能文檔問答助手&#xff0c;而LangChain是其中主流技術實現工具&#xff0c;能夠輕松讓大語言模型與外部數據相結合&#xff0c;從而構建智能問答系統。ERNIE Bot SDK已接入文心大模型4.0能力&am…

如何使用Imagewheel本地搭建一個簡單的的私人圖床公網可訪問?

文章目錄 1.前言2. Imagewheel網站搭建2.1. Imagewheel下載和安裝2.2. Imagewheel網頁測試2.3.cpolar的安裝和注冊 3.本地網頁發布3.1.Cpolar臨時數據隧道3.2.Cpolar穩定隧道&#xff08;云端設置&#xff09;3.3.Cpolar穩定隧道&#xff08;本地設置&#xff09; 4.公網訪問測…

Java:字符流 文件輸出 與 讀入 方法

Java&#xff1a;字節流 文件輸出與讀入方法 并 實現文件拷貝 文章目錄 字符流FileReaderFileWrite 字符流 字符流底層就是字節流。 字符流 字節流 字符集 特點&#xff1a; 輸入流&#xff1a;一次讀入一個字節&#xff0c;遇到中文時&#xff0c;一次讀多個字節。 輸出流…

POJ-2777 Count Color

經典區間染色板子題 #include<iostream> #include<cstring> #include<algorithm> using namespace std; const int N 1e610; struct Segment{int l,r,id; }tr[N<<2]; int n,color,m;void pushdown(int u){if(tr[u].id){tr[u<<1].id tr[u<&l…

P5707 【深基2.例12】上學遲到題解

題目 學校和 yyy 的家之間的距離為s米&#xff0c;而 yyy 以v米每分鐘的速度勻速走向學校。 在上學的路上&#xff0c;yyy 還要額外花費10分鐘的時間進行垃圾分類。 學校要求必須在上午8:00到達&#xff0c;請計算在不遲到的前提下&#xff0c;yyy 最晚能什么時候出門。 由…