ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Go gRPC-超时设置

2022-05-15 18:31:30  阅读:213  来源: 互联网

标签:err gRPC Route ctx go Go 超时 grpc


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/

标签:err,gRPC,Route,ctx,go,Go,超时,grpc
来源: https://www.cnblogs.com/randysun/p/16273941.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有