为了账号安全,请及时绑定邮箱和手机立即绑定

在 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 关注
  • 199 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号