業務方反應調用接口超時,但是在服務端監控并沒有看到5xx異常, 于是我們模擬一下請求超時時發生了什么?
1.openresty模擬長耗時服務端
延遲5s響應
| error_log logs/error.log; |
|
|
| http { |
| server { |
| listen 80; |
| charset utf-8; |
|
|
| location /reqtimeout { |
| default_type text/html; |
| content_by_lua ' |
| local start = os.clock() |
| while os.clock() - start < 5 do end |
| ngx.say("delay success!!") |
| '; |
| } |
| } |
| } |
2.golang和.net默認的httpclient對外都只有一個timeout設置
用于控制請求、響應的整體時間
.net httpclient 默認timeout= 100s;
golang net/http 無默認值設置,強烈推薦設置timeout,以避免服務端慢響應拖垮客戶端。
| static void Main(string[] args) |
| { |
| Console.WriteLine("Hello, World!"); |
| var a = HttpReqTimeout(); |
| Console.WriteLine(a.Result); |
| } |
|
|
| static async Task<string> HttpReqTimeout() |
| { |
| var handler = new SocketsHttpHandler |
| { |
| PooledConnectionLifetime = TimeSpan.FromMinutes(1) |
| }; |
| using (var hc = new HttpClient(handler)) |
| { |
| hc.Timeout = TimeSpan.FromSeconds(3); |
| return await hc.GetStringAsync("http://localhost/reqtimeout"); |
| } |
| } |
dotnet run ./ 顯示客戶端請求3s超時,爆出異常
| Hello, World! |
| Unhandled exception. System.AggregateException: One or more errors occurred. (A task was canceled.) |
| |
| at System.Threading.Tasks.Task.GetExceptions(Boolean includeTaskCanceledExceptions) |
| at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) |
| at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) |
| at ConsoleApp1.Program.Main(String[] args) in /Users/admin/RiderProjects/TestHttpClientFactory/ConsoleApp1/Program.cs:line 9 |
| |
|
|
| |
| at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) |
| at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) |
| at ConsoleApp1.Program.Main(String[] args) in /Users/admin/RiderProjects/TestHttpClientFactory/ConsoleApp1/Program.cs:line 9 |
openresty服務端日志,顯示執行完成,返回200ok:
| 127.0.0.1 - - [04/Dec/2024:15:17:50 +0800] "GET /reqtimeout HTTP/1.1" 200 28 "-" "-" |
這也正是對應上了業務方的反饋和服務端的監控現象(無5xx報錯)。
3.wireshark抓包看實質
tcp.port == 80 && ip.addr ==127.0.0.1 && ip.dst ==127.0.0.1
從tcp抓包過程看,分為三階段:
1>. httpclient請求, 正常tcp三次握手+ 請求確認;
2>. 客戶端3s之后超時, 客戶端發送FIN+ACK 數據包(客戶端標記連接已經被關閉), 服務端確認收到客戶端的FIN包;
3>. 服務端5s嘗試響應給客戶端,最終會檢測到客戶端已經關閉而釋放資源。
也就是說客戶端請求超時,只會影響客戶端, 服務端還會繼續處理并響應, 這也是我們在服務端監控上看不到5xx報錯的原因,可以通過在服務端設置: request_time between (-xx, 3s) 監測請求耗時占比。
正常的請求/響應讀者可以參考下圖:
4. 服務端能感知到客戶端請求超時嗎 ?
客戶端請求超時, 默認情況下服務端都是繼續執行之后響應;
服務器是具備感知客戶端請求取消的能力的。
C# 是通過CancellationToken
,感知客戶端取消,之后服務端可以做一些邏輯,比如記錄客戶端請求超時(常規實踐是記錄408響應碼)
| |
| var cancellationToken = httpContext.RequestAborted; |
| await LongLoop(cancellationToken); |
|
|
|
|
| public Task LongLoop(CancellationToken token) |
| { |
| while(true) |
| { |
| if (token.IsCancellationRequested == true) |
| { |
| break; |
| } |
| |
| } |
|
|
| return Task.CompletedTask; |
| } |
golang 是通過request.Context獲取客戶端取消信號,內核類似于C#
| func getHello(w http.ResponseWriter, r *http.Request) { |
| ctx := r.Context() |
| select { |
| case <-ctx.Done(): |
| |
| err := ctx.Err() |
| fmt.Println("Request cancelled:", err) |
| return |
| case <-time.After(5 * time.Second): |
| io.WriteString(w, "Hello, HTTP!\n") |
| return |
| } |
| } |
本文記錄了httpclient客戶端超時在雙端的現象, 服務端會繼續響應,在服務端可能檢測不到客戶端認定的報錯, 經驗無他,唯手熟爾。
轉自https://www.cnblogs.com/JulianHuang/p/18586745
該文章在 2024/12/5 10:13:27 編輯過