Go gRPC-超時設定
阿新 • • 發佈:2022-05-15
Go gRPC-超時設定
一、前言
gRPC預設的請求的超時時間是很長的,當你沒有設定請求超時時間時,所有在執行的請求都佔用大量資源且可能執行很長的時間,導致服務資源損耗過高,使得後來的請求響應過慢,甚至會引起整個程序崩潰。
為了避免這種情況,我們的服務應該設定超時時間。前面的提到,當客戶端發起請求時候,需要傳入上下文context.Context
,用於結束超時
或取消
的請求。
如何設定gRPC請求的超時時間。
二、新建proto檔案
syntax = "proto3";// 協議為proto3 //path 表示生成的go檔案的存放地址,會自動生成目錄的。 //name 表示生成的go檔案所屬的包名 //option go_package = "path;name"; option go_package = "./;proto"; package proto; // 生成pb.go命令: protoc -I ./ --go_out=plugins=grpc:.\06deadlines\proto\ .\06deadlines\proto\simple.proto // 定義我們的服務(可定義多個服務,每個服務可定義多個介面) service Simple{ rpc Route (SimpleRequest) returns (SimpleResponse){}; } // 定義傳送請求資訊 message SimpleRequest{ // 定義傳送的引數,採用駝峰命名方式,小寫加下劃線,如:student_name // 引數型別 引數名 標識號(不可重複) string data = 1; } // 定義響應資訊 message SimpleResponse{ // 定義接收的引數 // 引數型別 引數名 標識號(不可重複) int32 code = 1; string value = 2; }
// 指令編譯方法,進入專案所在目錄,執行
go-grpc-example> protoc -I ./ --go_out=plugins=grpc:.\06deadlines\proto\ .\06deadlines\proto\simple.proto
二、客戶端請求設定超時時間
1.把超時時間設定為當前時間+3秒
clientDeadline := time.Now().Add(time.Duration(3 * time.Second))
ctx, cancel := context.WithDeadline(ctx, clientDeadline)
defer cancel()
2.響應錯誤檢測中新增超時檢測
// 傳入超時時間為3秒的ctx res, err := grpcClient.Route(ctx, &req) if err != nil { //獲取錯誤狀態 statu, ok := status.FromError(err) if ok { //判斷是否為呼叫超時 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"
pb "go-grpc-example/06deadlines/proto"
"log"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
)
/*
@author RandySun
@create 2022-05-08-15:53
*/
const (
// Address 監聽地址
Address string = ":8001"
)
var grpcClient pb.SimpleClient
//
// route
// @Description:設定超時時間
// @param ctx
// @param deadlines
//
func route(ctx context.Context, deadlines time.Duration) {
// 設定超時時間
clientDeadline := time.Now().Add(time.Duration(deadlines * time.Second))
ctx, cancel := context.WithDeadline(ctx, clientDeadline)
defer cancel()
// 建立傳送結構體
req := pb.SimpleRequest{
Data: "grpc",
}
// 呼叫 Route 方法 同時傳入context.Context, 在有需要時可以讓我們改變RPC的行為,比如超時/取消一個正在執行的RPC
res, err := grpcClient.Route(ctx, &req)
// 列印返回直
log.Println("服務的返回響應data:", res)
if err != nil {
// 獲取錯誤狀態
statu, ok := status.FromError(err)
if ok {
// 判斷是否為呼叫超時
if statu.Code() == codes.DeadlineExceeded {
log.Fatalln("Route timeout!")
}
}
log.Fatalln("Call Route err:", err)
}
}
func main() {
// 連線伺服器
conn, err := grpc.Dial(Address, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("net.Connect connect: %v", err)
}
defer conn.Close()
// 建立gRpc連線
ctx := context.Background()
grpcClient = pb.NewSimpleClient(conn)
// 修改超時
route(ctx, 2)
}
三、服務端判斷請求是否超時
當請求超時後,服務端應該停止正在進行的操作,避免資源浪費。
// Route 實現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() //超時後退出該Go協程
case <-time.After(4 * time.Second): // 模擬耗時操作
res := pb.SimpleResponse{
Code: 200,
Value: "hello " + req.Data,
}
// //修改資料庫前進行超時判斷
// if ctx.Err() == context.Canceled{
// ...
// //如果已經超時,則退出
// }
data <- &res
}
}
一般地,在寫庫前進行超時檢測,發現超時就停止工作。
完整server.go程式碼
package main
import (
"context"
pb "grpc/06deadlines/proto"
"log"
"net"
"runtime"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/grpc"
)
/*
@author RandySun
@create 2022-03-27-20:03
*/
// SimpleService 定義我們的服務
type SimpleService struct {
}
// Route 實現Route方法
func (s *SimpleService) Route(ctx context.Context, req *pb.SimpleRequest) (*pb.SimpleResponse, error) {
data := make(chan *pb.SimpleResponse)
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() // 超時後退出該Go協程
case <-time.After(4 * time.Second): // 模擬耗時操作
res := pb.SimpleResponse{
Code: 200,
Value: "hello " + req.Data,
}
// //修改資料庫前進行超時判斷
// if ctx.Err() == context.Canceled{
// ...
// //如果已經超時,則退出
// }
data <- &res
}
}
const (
// Address 監聽地址
Address string = ":8001"
// NetWork 網路通訊協議
NetWork string = "tcp"
)
func main() {
// 監聽本地埠
listener, err := net.Listen(NetWork, Address)
if err != nil {
log.Fatalf("net.Listen err: %V", err)
}
log.Println(Address, "net.Listing...")
// 建立grpc服務例項
grpcServer := grpc.NewServer()
// 在grpc伺服器註冊我們的服務
pb.RegisterSimpleServer(grpcServer, &SimpleService{})
err = grpcServer.Serve(listener)
if err != nil {
log.Fatalf("grpcService.Serve err:%v", err)
}
log.Println("grpcService.Serve run succ")
}
四、執行結果
服務端:
go-grpc-example> cd .\06deadlines\server\
go-grpc-example\06deadlines\server> go build .\service.go
go-grpc-example\06deadlines\server> .\server.exe
2022/05/08 16:40:11 :8001 net.Listing...
2022/05/08 16:40:20 context deadline exceeded
2022/05/08 16:40:30 context deadline exceeded
客戶端:
go-grpc-example\06deadlines\client> go build .\client.go
go-grpc-example\06deadlines\client> .\client.exe
2022/05/08 16:40:30 服務的返回響應data: <nil>
2022/05/08 16:40:30 Route timeout!
五、總結
超時時間的長短需要根據自身服務而定,例如返回一個hello grpc
,可能只需要幾十毫秒,然而處理大量資料的同步操作則可能要很長時間。需要考慮多方面因素來決定這個超時時間,例如系統間端到端的延時,哪些RPC是序列的,哪些是可以並行的等等。
參考:https://grpc.io/blog/deadlines/