亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 GRPC 中處理 IO 等待狀態的優雅方式

在 GRPC 中處理 IO 等待狀態的優雅方式

Go
撒科打諢 2022-09-05 17:33:14
我們的服務器(grpc-gateway + grpc)在K8S上運行,使用go 1.13,并終止堆棧信息    Last State:    Terminated      Reason:      Error      Message:     o.(*Reader).fill(0xc002ec78c0)                   /Users/local/go/src/bufio/bufio.go:100 +0x103bufio.(*Reader).Peek(0xc002ec78c0, 0x4, 0x0, 0x0, 0x0, 0x0, 0xc002e79ad0)  /Users/local/go/src/bufio/bufio.go:138 +0x4fnet/http.(*conn).readRequest(0xc00245a000, 0x1ef4800, 0xc000be7780, 0x0, 0x0, 0x0)  /Users/local/go/src/net/http/server.go:962 +0xb3bnet/http.(*conn).serve(0xc00245a000, 0x1ef4800, 0xc000be7780)  /Users/local/go/src/net/http/server.go:1817 +0x6d4created by net/http.(*Server).Serve  /Users/local/go/src/net/http/server.go:2928 +0x384goroutine 8724981 [IO wait]:internal/poll.runtime_pollWait(0x7f5d3a8f84e8, 0x72, 0xffffffffffffffff)  /Users/local/go/src/runtime/netpoll.go:184 +0x55internal/poll.(*pollDesc).wait(0xc000155d98, 0x72, 0x1000, 0x1000, 0xffffffffffffffff)  /Users/local/go/src/internal/poll/fd_poll_runtime.go:87 +0x45internal/poll.(*pollDesc).waitRead(...)  /Users/local/go/src/internal/poll/fd_poll_runtime.go:92internal/poll.(*FD).Read(0xc000155d80, 0xc00138b000, 0x1000, 0x1000, 0x0, 0x0, 0x0)  /Users/local/go/src/internal/poll/fd_unix.go:169 +0x1cfnet.(*netFD).Read(0xc000155d80, 0xc00138b000, 0x1000, 0x1000, 0xc00142b9e8, 0x4ce13d, 0xc000155d80)  /Users/local/go/src/net/fd_unix.go:202 +0x4fnet.(*conn).Read(0xc0000dd690, 0xc00138b000, 0x1000, 0x1000, 0x0, 0x0, 0x0)  /Users/local/go/src/net/net.go:184 +0x68net/http.(*connReader).Read(0xc001e9b050, 0xc00138b000, 0x1000, 0x1000, 0x0, 0x0, 0x0)  /Users/local/go/src/net/http/server.go:785 +0xf4bufio.(*Reader).fill(0xc0021fe060)      Exit Code:    2根據這個問題,一個可能的解決方案是>  s := new(http.Server)>  // ...>  s.ReadTimeout = 5 * time.Second>  s.WriteTimeout = 5 * time.Second>  // ...但是,我們未能找到來自,我們錯過了什么嗎?或者如何在GRPC中更優雅地處理IO等待狀態下的goroutine?ReadTimeoutgrpc.NewServergrpc 版本是v1.21.1
查看完整描述

2 回答

?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

網易娛樂.NewServer 允許在創建期間使用零個或多個:ServerOption


func NewServer(opt ...ServerOption) *Server

雖然似乎沒有一個或等價物,你可以試試:ReadTimeoutWriteTimeouthttp.Serverkeepalive.ServerParameters


type ServerParameters struct {

    MaxConnectionIdle     time.Duration // The current default value is infinity.

    MaxConnectionAge      time.Duration // The current default value is infinity.

    MaxConnectionAgeGrace time.Duration // The current default value is infinity.

    Time                  time.Duration // The current default value is 2 hours.

    Timeout               time.Duration // The current default value is 20 seconds.

}

(完整的文檔保持。服務器參數)


并調整為低于 2 小時的內容:keepalive.ServerParameters.Time


srv := grpc.NewServer(

    keepalive.ServerParameters{Time:5*time.Minute},

)

這將降低連接重用率 - 但也會釋放早已失效的客戶端連接。


查看完整回答
反對 回復 2022-09-05
?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

使用具有超時的上下文。

https://golang.org/pkg/context/


查看完整回答
反對 回復 2022-09-05
  • 2 回答
  • 0 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號