網站首頁 編程語言 正文
Deadlines
今天將介紹 gRPC Deadlines 的用法,這一個必備技巧。內容也比較簡單。
Deadlines 意指截止時間,在 gRPC 中強調 TL;DR(Too long, Don't read)并建議始終設定截止日期,為什么呢?
為什么要設置
當未設置 Deadlines 時,將采用默認的 DEADLINE_EXCEEDED(這個時間非常大)
如果產生了阻塞等待,就會造成大量正在進行的請求都會被保留,并且所有請求都有可能達到最大超時
這會使服務面臨資源耗盡的風險,例如內存,這會增加服務的延遲,或者在最壞的情況下可能導致整個進程崩潰
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 勢必要去檢測它
否則如果 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
總結
本章節比較簡單,你需要知道以下知識點:
- 怎么設置 Deadlines
- 為什么要設置 Deadlines
你要清楚地明白到,gRPC Deadlines 是很重要的,否則這小小的功能點就會要了你生產的命。
示例源碼
https://github.com/eddycjy/go-grpc-example
原文鏈接:https://juejin.cn/post/6844904017017962504
相關推薦
- 2022-12-06 Python基礎之hashlib模塊subprocess模塊logging模塊_python
- 2023-01-26 C#實現Word轉換TXT的方法詳解_C#教程
- 2022-10-15 python內建類型與標準類型_python
- 2022-10-16 部署k8s集群的超詳細實踐步驟_云和虛擬化
- 2022-10-25 python繪圖之坐標軸的超詳細講解_python
- 2023-04-14 拯救強迫癥Android?Builder模式_Android
- 2023-09-12 springboot將jar改成war
- 2022-08-15 Springboot整合Elasticsearch及相關API
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支