你们好,我是煎鱼,在前面的章节中,已经介绍了 gRPC 的基本用法。那你想一想,让它这么裸跑真的没问题吗?git
那么,确定是有问题了。今天将介绍 gRPC Deadlines 的用法,这一个必备技巧。内容也比较简单。github
Deadlines 意指截止时间,在 gRPC 中强调 TL;DR(Too long, Don't read)并建议始终设定截止日期,为何呢?bash
当未设置 Deadlines 时,将采用默认的 DEADLINE_EXCEEDED(这个时间很是大)ui
若是产生了阻塞等待,就会形成大量正在进行的请求都会被保留,而且全部请求都有可能达到最大超时spa
这会使服务面临资源耗尽的风险,例如内存,这会增长服务的延迟,或者在最坏的状况下可能致使整个进程崩溃code
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())
}
复制代码
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) }
}
复制代码
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
return WithDeadline(parent, time.Now().Add(timeout))
}
复制代码
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 势必要去检测它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
复制代码
本章节比较简单,你须要知道如下知识点:
你要清楚地明白到,gRPC Deadlines 是很重要的,不然这小小的功能点就会要了你生产的命。
若是有任何疑问或错误,欢迎在 issues 进行提问或给予修正意见,若是喜欢或对你有所帮助,欢迎 Star,对做者是一种鼓励和推动。
跟煎鱼学 Go:github.com/eddycjy/blo…