Go?gRPC進(jìn)階教程服務(wù)超時(shí)設(shè)置
前言
gRPC默認(rèn)的請求的超時(shí)時(shí)間是很長的,當(dāng)你沒有設(shè)置請求超時(shí)時(shí)間時(shí),所有在運(yùn)行的請求都占用大量資源且可能運(yùn)行很長的時(shí)間,導(dǎo)致服務(wù)資源損耗過高,使得后來的請求響應(yīng)過慢,甚至?xí)鹫麄€(gè)進(jìn)程崩潰。
為了避免這種情況,我們的服務(wù)應(yīng)該設(shè)置超時(shí)時(shí)間。
前面的入門教程
Go gRPC教程實(shí)現(xiàn)Simple RPC
提到當(dāng)客戶端發(fā)起請求時(shí)候,需要傳入上下文context.Context,用于結(jié)束超時(shí)或取消的請求。
本篇以簡單RPC為例,介紹如何設(shè)置gRPC請求的超時(shí)時(shí)間。
客戶端請求設(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ò)誤檢測中添加超時(shí)檢測
// 傳入超時(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ù)端判斷請求是否超時(shí)
當(dā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ù)庫前進(jìn)行超時(shí)判斷 // if ctx.Err() == context.Canceled{ // ... // //如果已經(jīng)超時(shí),則退出 // } data <- &res } }
一般地,在寫庫前進(jìn)行超時(shí)檢測,發(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)聽地址 Address string = ":8000" // Network 網(wǎng)絡(luò)通信協(xié)議 Network string = "tcp" ) func main() { // 監(jiān)聽本地端口 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ù)器注冊我們的服務(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ù)庫前進(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í)間的長短需要根據(jù)自身服務(wù)而定,例如返回一個(gè)hello grpc,可能只需要幾十毫秒,然而處理大量數(shù)據(jù)的同步操作則可能要很長時(shí)間。需要考慮多方面因素來決定這個(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è)置的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Go微服務(wù)網(wǎng)關(guān)的實(shí)現(xiàn)
本文主要介紹了Go微服務(wù)網(wǎng)關(guān)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07golang進(jìn)程內(nèi)存控制避免docker內(nèi)oom
這篇文章主要為大家介紹了golang進(jìn)程內(nèi)存控制避免docker內(nèi)oom示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10golang基礎(chǔ)之Gocurrency并發(fā)
這篇文章主要介紹了golang基礎(chǔ)之Gocurrency并發(fā),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07