我最近在通過以下方式開發(fā)高并發(fā) http 客戶端時(shí)遇到了一個(gè)問題valyala/fasthttp:客戶端在前 15K~ 請(qǐng)求中工作正常,但之后越來越多dial tcp4 127.0.0.1:80: i/o timeout并dialing to the given TCP address timed out發(fā)生錯(cuò)誤。示例代碼var Finished = 0var Failed = 0var Success = 0func main() { for i := 0; i < 1000; i++ { go get() } start := time.Now().Unix() for { fmt.Printf("Rate: %.2f/s Success: %d, Failed: %d\n", float64(Success)/float64(time.Now().Unix()-start), Success, Failed) time.Sleep(100 * time.Millisecond) }}func get() { ticker := time.NewTicker(time.Duration(100+rand.Intn(2900)) * time.Millisecond) defer ticker.Stop() client := &fasthttp.Client{ MaxConnsPerHost: 10000, } for { req := &fasthttp.Request{} req.SetRequestURI("http://127.0.0.1:80/require?number=10") req.Header.SetMethod(fasthttp.MethodGet) req.Header.SetConnectionClose() res := &fasthttp.Response{} err := client.DoTimeout(req, res, 5*time.Second) if err != nil { fmt.Println(err.Error()) Failed++ } else { Success++ } Finished++ client.CloseIdleConnections() <-ticker.C }}細(xì)節(jié)服務(wù)器建立在labstack/echo/v4客戶端出現(xiàn)超時(shí)錯(cuò)誤時(shí),服務(wù)器沒有任何錯(cuò)誤,通過Postman或類似瀏覽器的方式手動(dòng)執(zhí)行請(qǐng)求Chrome都可以正常工作。客戶端在第一個(gè) 15K~ 請(qǐng)求中運(yùn)行良好,但之后,出現(xiàn)越來越多的超時(shí)錯(cuò)誤并且輸出越來越少Rate。我搜索了google和github,發(fā)現(xiàn)這個(gè)問題可能是最合適的,但沒有找到解決方案。另一個(gè)小問題...正如您所看到的,當(dāng)客戶端啟動(dòng)時(shí),它首先會(huì)產(chǎn)生一些the server closed connection before returning the first response byte. Make sure the server returns 'Connection: close' response header before closing the connection錯(cuò)誤,然后正常工作直到大約 15K 問題,然后開始產(chǎn)生越來越多的超時(shí)錯(cuò)誤。為什么它會(huì)在開始時(shí)產(chǎn)生 Connection closed 錯(cuò)誤?機(jī)器信息配備 16GB Ram 并運(yùn)行 macOS Monterey 12.4 的 Macbook Pro 14 2021 (Apple M1 Pro)
撥號(hào)TCP報(bào)錯(cuò):高并發(fā)請(qǐng)求一段時(shí)間后超時(shí)或i/o超時(shí)
慕運(yùn)維8079593
2022-12-26 10:53:24