Go gRPC超時控制Deadlines用法詳解
Deadlines
今天將介紹 gRPC Deadlines 的用法,這一個必備技巧。內(nèi)容也比較簡單。
Deadlines 意指截止時間,在 gRPC 中強(qiáng)調(diào) TL;DR(Too long, Don't read)并建議始終設(shè)定截止日期,為什么呢?
為什么要設(shè)置
當(dāng)未設(shè)置 Deadlines 時,將采用默認(rèn)的 DEADLINE_EXCEEDED(這個時間非常大)
如果產(chǎn)生了阻塞等待,就會造成大量正在進(jìn)行的請求都會被保留,并且所有請求都有可能達(dá)到最大超時
這會使服務(wù)面臨資源耗盡的風(fēng)險,例如內(nèi)存,這會增加服務(wù)的延遲,或者在最壞的情況下可能導(dǎo)致整個進(jìn)程崩潰
gRPC
Client
func main() {
...
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Duration(5 * time.Second)))
defer cancel()
client := pb.NewSearchServiceClient(conn)
resp, err := client.Search(ctx, &pb.SearchRequest{
Request: "gRPC",
})
if err != nil {
statusErr, ok := status.FromError(err)
if ok {
if statusErr.Code() == codes.DeadlineExceeded {
log.Fatalln("client.Search err: deadline")
}
}
log.Fatalf("client.Search err: %v", err)
}
log.Printf("resp: %s", resp.GetResponse())
}
- context.WithDeadline:會返回最終上下文截止時間。第一個形參為父上下文,第二個形參為調(diào)整的截止時間。若父級時間早于子級時間,則以父級時間為準(zhǔn),否則以子級時間為最終截止時間
func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) {
if cur, ok := parent.Deadline(); ok && cur.Before(d) {
// The current deadline is already sooner than the new one.
return WithCancel(parent)
}
c := &timerCtx{
cancelCtx: newCancelCtx(parent),
deadline: d,
}
propagateCancel(parent, c)
dur := time.Until(d)
if dur <= 0 {
c.cancel(true, DeadlineExceeded) // deadline has already passed
return c, func() { c.cancel(true, Canceled) }
}
c.mu.Lock()
defer c.mu.Unlock()
if c.err == nil {
c.timer = time.AfterFunc(dur, func() {
c.cancel(true, DeadlineExceeded)
})
}
return c, func() { c.cancel(true, Canceled) }
}
context.WithTimeout:很常見的另外一個方法,是便捷操作。實際上是對于 WithDeadline 的封裝
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
return WithDeadline(parent, time.Now().Add(timeout))
}
status.FromError:返回 GRPCStatus 的具體錯誤碼,若為非法,則直接返回 codes.Unknown
Server
type SearchService struct{}
func (s *SearchService) Search(ctx context.Context, r *pb.SearchRequest) (*pb.SearchResponse, error) {
for i := 0; i < 5; i++ {
if ctx.Err() == context.Canceled {
return nil, status.Errorf(codes.Canceled, "SearchService.Search canceled")
}
time.Sleep(1 * time.Second)
}
return &pb.SearchResponse{Response: r.GetRequest() + " Server"}, nil
}
func main() {
...
}
而在 Server 端,由于 Client 已經(jīng)設(shè)置了截止時間。Server 勢必要去檢測它
否則如果 Client 已經(jīng)結(jié)束掉了,Server 還傻傻的在那執(zhí)行,這對資源是一種極大的浪費
因此在這里需要用 ctx.Err() == context.Canceled 進(jìn)行判斷,為了模擬場景我們加了循環(huán)和睡眠 ??
驗證
重新啟動 server.go 和 client.go,得到結(jié)果:
$ go run client.go 2018/10/06 17:45:55 client.Search err: deadline exit status 1
總結(jié)
本章節(jié)比較簡單,你需要知道以下知識點:
- 怎么設(shè)置 Deadlines
- 為什么要設(shè)置 Deadlines
你要清楚地明白到,gRPC Deadlines 是很重要的,否則這小小的功能點就會要了你生產(chǎn)的命。
示例源碼
https://github.com/eddycjy/go-grpc-example
以上就是Go gRPC超時控制Deadlines用法詳解的詳細(xì)內(nèi)容,更多關(guān)于Go gRPC超時控制Deadlines的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Golang中結(jié)構(gòu)體映射mapstructure庫深入詳解
mapstructure用于將通用的map[string]interface{}解碼到對應(yīng)的 Go 結(jié)構(gòu)體中,或者執(zhí)行相反的操作。很多時候,解析來自多種源頭的數(shù)據(jù)流時,我們一般事先并不知道他們對應(yīng)的具體類型。只有讀取到一些字段之后才能做出判斷2023-01-01
golang 在windows中設(shè)置環(huán)境變量的操作
這篇文章主要介紹了golang 在windows中設(shè)置環(huán)境變量的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04
Go語言中?Print?Printf和Println?的區(qū)別解析
這篇文章主要介紹了Go語言中?Print?Printf和Println?的區(qū)別,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
Golang中Kafka的重復(fù)消費和消息丟失問題的解決方案
在Kafka中無論是生產(chǎn)者發(fā)送消息到Kafka集群還是消費者從Kafka集群中拉取消息,都是容易出現(xiàn)問題的,比較典型的就是消費端的重復(fù)消費問題、生產(chǎn)端和消費端產(chǎn)生的消息丟失問題,下面將對這兩個問題出現(xiàn)的場景以及常見的解決方案進(jìn)行講解2023-08-08
Go語言學(xué)習(xí)教程之goroutine和通道的示例詳解
這篇文章主要通過A?Tour?of?Go中的例子進(jìn)行學(xué)習(xí),以此了解Go語言中的goroutine和通道,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-09-09

