Go gRPC 系列九:gRPC超時控制(Deadlines)

前言

你們好,我是煎魚,在前面的章節中,已經介紹了 gRPC 的基本用法。那你想一想,讓它這麼裸跑真的沒問題嗎?git

那麼,確定是有問題了。今天將介紹 gRPC Deadlines 的用法,這一個必備技巧。內容也比較簡單。github

Deadlines

Deadlines 意指截止時間,在 gRPC 中強調 TL;DR(Too long, Don't read)並建議始終設定截止日期,爲何呢?bash

爲何要設置

當未設置 Deadlines 時,將採用默認的 DEADLINE_EXCEEDED(這個時間很是大)ui

若是產生了阻塞等待,就會形成大量正在進行的請求都會被保留,而且全部請求都有可能達到最大超時spa

這會使服務面臨資源耗盡的風險,例如內存,這會增長服務的延遲,或者在最壞的狀況下可能致使整個進程崩潰code

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:會返回最終上下文截止時間。第一個形參爲父上下文,第二個形參爲調整的截止時間。若父級時間早於子級時間,則以父級時間爲準,不然以子級時間爲最終截止時間
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 已經設置了截止時間。Server 勢必要去檢測它cdn

不然若是 Client 已經結束掉了,Server 還傻傻的在那執行,這對資源是一種極大的浪費server

所以在這裏須要用 ctx.Err() == context.Canceled 進行判斷,爲了模擬場景咱們加了循環和睡眠 🤔blog

驗證

從新啓動 server.go 和 client.go,獲得結果:進程

$ go run client.go
2018/10/06 17:45:55 client.Search err: deadline
exit status 1
複製代碼

總結

本章節比較簡單,你須要知道如下知識點:

  • 怎麼設置 Deadlines
  • 爲何要設置 Deadlines

你要清楚地明白到,gRPC Deadlines 是很重要的,不然這小小的功能點就會要了你生產的命。

若是有任何疑問或錯誤,歡迎在 issues 進行提問或給予修正意見,若是喜歡或對你有所幫助,歡迎 Star,對做者是一種鼓勵和推動。

個人博客

跟煎魚學 Go:github.com/eddycjy/blo…

個人公衆號

image

參考

本系列示例代碼

相關文章
相關標籤/搜索