這篇文章主要介紹了Go gRPC超時控制Deadlines怎么使用的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Go gRPC超時控制Deadlines怎么使用文章都會有所收獲,下面我們一起來看看吧。
當未設置 Deadlines 時,將采用默認的 DEADLINE_EXCEEDED(這個時間非常大)
如果產生了阻塞等待,就會造成大量正在進行的請求都會被保留,并且所有請求都有可能達到最大超時
這會使服務面臨資源耗盡的風險,例如內存,這會增加服務的延遲,或者在最壞的情況下可能導致整個進程崩潰
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:會返回最終上下文截止時間。第一個形參為父上下文,第二個形參為調整的截止時間。若父級時間早于子級時間,則以父級時間為準,否則以子級時間為最終截止時間
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
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 已經設置了截止時間。Server 勢必要去檢測它
否則如果 Client 已經結束掉了,Server 還傻傻的在那執行,這對資源是一種極大的浪費
因此在這里需要用 ctx.Err() == context.Canceled 進行判斷,為了模擬場景我們加了循環和睡眠 ????
重新啟動 server.go 和 client.go,得到結果:
$ go run client.go 2018/10/06 17:45:55 client.Search err: deadline exit status 1
關于“Go gRPC超時控制Deadlines怎么使用”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Go gRPC超時控制Deadlines怎么使用”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。