Go?gRPC進(jìn)階教程服務(wù)超時(shí)設(shè)置
前言
gRPC默認(rèn)的請(qǐng)求的超時(shí)時(shí)間是很長(zhǎng)的,當(dāng)你沒(méi)有設(shè)置請(qǐng)求超時(shí)時(shí)間時(shí),所有在運(yùn)行的請(qǐng)求都占用大量資源且可能運(yùn)行很長(zhǎng)的時(shí)間,導(dǎo)致服務(wù)資源損耗過(guò)高,使得后來(lái)的請(qǐng)求響應(yīng)過(guò)慢,甚至?xí)鹫麄€(gè)進(jìn)程崩潰。
為了避免這種情況,我們的服務(wù)應(yīng)該設(shè)置超時(shí)時(shí)間。
前面的入門教程
Go gRPC教程實(shí)現(xiàn)Simple RPC
提到當(dāng)客戶端發(fā)起請(qǐng)求時(shí)候,需要傳入上下文context.Context,用于結(jié)束超時(shí)或取消的請(qǐng)求。
本篇以簡(jiǎn)單RPC為例,介紹如何設(shè)置gRPC請(qǐng)求的超時(shí)時(shí)間。
客戶端請(qǐng)求設(shè)置超時(shí)時(shí)間
修改調(diào)用服務(wù)端方法
1.把超時(shí)時(shí)間設(shè)置為當(dāng)前時(shí)間+3秒
clientDeadline := time.Now().Add(time.Duration(3 * time.Second)) ctx, cancel := context.WithDeadline(ctx, clientDeadline) defer cancel()
2.響應(yīng)錯(cuò)誤檢測(cè)中添加超時(shí)檢測(cè)
// 傳入超時(shí)時(shí)間為3秒的ctx res, err := grpcClient.Route(ctx, &req) if err != nil { //獲取錯(cuò)誤狀態(tài) statu, ok := status.FromError(err) if ok { //判斷是否為調(diào)用超時(shí) if statu.Code() == codes.DeadlineExceeded { log.Fatalln("Route timeout!") } } log.Fatalf("Call Route err: %v", err) } // 打印返回值 log.Println(res.Value)
完整的client.go代碼
package main import ( "context" "log" "time" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" pb "go-grpc-example/6-grpc_deadlines/proto" ) // Address 連接地址 const Address string = ":8000" var grpcClient pb.SimpleClient func main() { // 連接服務(wù)器 conn, err := grpc.Dial(Address, grpc.WithInsecure()) if err != nil { log.Fatalf("net.Connect err: %v", err) } defer conn.Close() ctx := context.Background() // 建立gRPC連接 grpcClient = pb.NewSimpleClient(conn) route(ctx, 2) } // route 調(diào)用服務(wù)端Route方法 func route(ctx context.Context, deadlines time.Duration) { //設(shè)置3秒超時(shí)時(shí)間 clientDeadline := time.Now().Add(time.Duration(deadlines * time.Second)) ctx, cancel := context.WithDeadline(ctx, clientDeadline) defer cancel() // 創(chuàng)建發(fā)送結(jié)構(gòu)體 req := pb.SimpleRequest{ Data: "grpc", } // 調(diào)用我們的服務(wù)(Route方法) // 傳入超時(shí)時(shí)間為3秒的ctx res, err := grpcClient.Route(ctx, &req) if err != nil { //獲取錯(cuò)誤狀態(tài) statu, ok := status.FromError(err) if ok { //判斷是否為調(diào)用超時(shí) if statu.Code() == codes.DeadlineExceeded { log.Fatalln("Route timeout!") } } log.Fatalf("Call Route err: %v", err) } // 打印返回值 log.Println(res.Value) }
服務(wù)端判斷請(qǐng)求是否超時(shí)
當(dāng)請(qǐng)求超時(shí)后,服務(wù)端應(yīng)該停止正在進(jìn)行的操作,避免資源浪費(fèi)。
// Route 實(shí)現(xiàn)Route方法 func (s *SimpleService) Route(ctx context.Context, req *pb.SimpleRequest) (*pb.SimpleResponse, error) { data := make(chan *pb.SimpleResponse, 1) go handle(ctx, req, data) select { case res := <-data: return res, nil case <-ctx.Done(): return nil, status.Errorf(codes.Canceled, "Client cancelled, abandoning.") } } func handle(ctx context.Context, req *pb.SimpleRequest, data chan<- *pb.SimpleResponse) { select { case <-ctx.Done(): log.Println(ctx.Err()) runtime.Goexit() //超時(shí)后退出該Go協(xié)程 case <-time.After(4 * time.Second): // 模擬耗時(shí)操作 res := pb.SimpleResponse{ Code: 200, Value: "hello " + req.Data, } // //修改數(shù)據(jù)庫(kù)前進(jìn)行超時(shí)判斷 // if ctx.Err() == context.Canceled{ // ... // //如果已經(jīng)超時(shí),則退出 // } data <- &res } }
一般地,在寫(xiě)庫(kù)前進(jìn)行超時(shí)檢測(cè),發(fā)現(xiàn)超時(shí)就停止工作。
完整server.go代碼
package main import ( "context" "log" "net" "runtime" "time" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" pb "go-grpc-example/6-grpc_deadlines/proto" ) // SimpleService 定義我們的服務(wù) type SimpleService struct{} const ( // Address 監(jiān)聽(tīng)地址 Address string = ":8000" // Network 網(wǎng)絡(luò)通信協(xié)議 Network string = "tcp" ) func main() { // 監(jiān)聽(tīng)本地端口 listener, err := net.Listen(Network, Address) if err != nil { log.Fatalf("net.Listen err: %v", err) } log.Println(Address + " net.Listing...") // 新建gRPC服務(wù)器實(shí)例 grpcServer := grpc.NewServer() // 在gRPC服務(wù)器注冊(cè)我們的服務(wù) pb.RegisterSimpleServer(grpcServer, &SimpleService{}) //用服務(wù)器 Serve() 方法以及我們的端口信息區(qū)實(shí)現(xiàn)阻塞等待,直到進(jìn)程被殺死或者 Stop() 被調(diào)用 err = grpcServer.Serve(listener) if err != nil { log.Fatalf("grpcServer.Serve err: %v", err) } } // Route 實(shí)現(xiàn)Route方法 func (s *SimpleService) Route(ctx context.Context, req *pb.SimpleRequest) (*pb.SimpleResponse, error) { data := make(chan *pb.SimpleResponse, 1) go handle(ctx, req, data) select { case res := <-data: return res, nil case <-ctx.Done(): return nil, status.Errorf(codes.Canceled, "Client cancelled, abandoning.") } } func handle(ctx context.Context, req *pb.SimpleRequest, data chan<- *pb.SimpleResponse) { select { case <-ctx.Done(): log.Println(ctx.Err()) runtime.Goexit() //超時(shí)后退出該Go協(xié)程 case <-time.After(4 * time.Second): // 模擬耗時(shí)操作 res := pb.SimpleResponse{ Code: 200, Value: "hello " + req.Data, } // //修改數(shù)據(jù)庫(kù)前進(jìn)行超時(shí)判斷 // if ctx.Err() == context.Canceled{ // ... // //如果已經(jīng)超時(shí),則退出 // } data <- &res } }
運(yùn)行結(jié)果
服務(wù)端:
:8000 net.Listing...
goroutine still running
客戶端:
Route timeout!
總結(jié)
超時(shí)時(shí)間的長(zhǎng)短需要根據(jù)自身服務(wù)而定,例如返回一個(gè)hello grpc,可能只需要幾十毫秒,然而處理大量數(shù)據(jù)的同步操作則可能要很長(zhǎng)時(shí)間。需要考慮多方面因素來(lái)決定這個(gè)超時(shí)時(shí)間,例如系統(tǒng)間端到端的延時(shí),哪些RPC是串行的,哪些是可以并行的等等。
教程源碼地址:https://github.com/Bingjian-Zhu/go-grpc-example
參考:https://grpc.io/blog/deadlines/
以上就是Go gRPC進(jìn)階服務(wù)超時(shí)設(shè)置的詳細(xì)內(nèi)容,更多關(guān)于Go gRPC超時(shí)設(shè)置的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Golang中時(shí)間戳與時(shí)區(qū)轉(zhuǎn)換的方法詳解
時(shí)間是我們生活的基石,而在計(jì)算機(jī)科學(xué)中,時(shí)間處理顯得尤為重要,尤其是當(dāng)你在處理分布式系統(tǒng)、跨時(shí)區(qū)應(yīng)用和全球服務(wù)時(shí),時(shí)間和時(shí)區(qū)的管理變得不可或缺,在這篇文章中,我們將深入探討Golang中的時(shí)間戳與時(shí)區(qū)轉(zhuǎn)換,需要的朋友可以參考下2024-06-06go語(yǔ)言用八百行代碼實(shí)現(xiàn)一個(gè)JSON解析器
這篇文章主要為大家介紹了go語(yǔ)言用八百行代碼實(shí)現(xiàn)一個(gè)JSON解析器實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07Go中的函數(shù)選項(xiàng)模式(Functional Options Pattern)詳解
在 Go 語(yǔ)言中,函數(shù)選項(xiàng)模式是一種優(yōu)雅的設(shè)計(jì)模式,用于處理函數(shù)的可選參數(shù),本文將對(duì)其進(jìn)行講解,準(zhǔn)備好了嗎,快跟隨著本文一探究竟吧2023-06-06深入解析Go語(yǔ)言中HTTP請(qǐng)求處理的底層實(shí)現(xiàn)
本文將詳細(xì)介紹?Go?語(yǔ)言中?HTTP?請(qǐng)求處理的底層機(jī)制,包括工作流程、創(chuàng)建?Listen?Socket?監(jiān)聽(tīng)端口、接收客戶端請(qǐng)求并建立連接以及處理客戶端請(qǐng)求并返回響應(yīng)等,需要的朋友可以參考下2023-05-05golang使用redis實(shí)現(xiàn)全文搜索功能詳解
這篇文章主要為大家詳細(xì)介紹了golang如何使用redis實(shí)現(xiàn)全文搜索功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02