Go gRPC进阶教程服务超时设置

2022-07-14,,,,

前言

grpc默认的请求的超时时间是很长的,当你没有设置请求超时时间时,所有在运行的请求都占用大量资源且可能运行很长的时间,导致服务资源损耗过高,使得后来的请求响应过慢,甚至会引起整个进程崩溃。

为了避免这种情况,我们的服务应该设置超时时间。

前面的入门教程

go grpc环境安装教程示例详解

go grpc教程实现simple rpc

go grpc服务端流式rpc教程示例

go grpc服务客户端流式rpc教程

go grpc服务双向流式rpc教程

提到当客户端发起请求时候,需要传入上下文context.context,用于结束超时或取消的请求。

本篇以简单rpc为例,介绍如何设置grpc请求的超时时间。

客户端请求设置超时时间

修改调用服务端方法

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"
	"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() {
	// 连接服务器
	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 调用服务端route方法
func route(ctx context.context, deadlines time.duration) {
	//设置3秒超时时间
	clientdeadline := time.now().add(time.duration(deadlines * time.second))
	ctx, cancel := context.withdeadline(ctx, clientdeadline)
	defer cancel()
	// 创建发送结构体
	req := pb.simplerequest{
		data: "grpc",
	}
	// 调用我们的服务(route方法)
	// 传入超时时间为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)
}

服务端判断请求是否超时

当请求超时后,服务端应该停止正在进行的操作,避免资源浪费。

// 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"
	"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 定义我们的服务
type simpleservice struct{}
const (
	// address 监听地址
	address string = ":8000"
	// 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{})
	//用服务器 serve() 方法以及我们的端口信息区实现阻塞等待,直到进程被杀死或者 stop() 被调用
	err = grpcserver.serve(listener)
	if err != nil {
		log.fatalf("grpcserver.serve err: %v", err)
	}
}
// 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
	}
}

运行结果

服务端:

:8000 net.listing...
goroutine still running

客户端:

route timeout! 

总结

超时时间的长短需要根据自身服务而定,例如返回一个hello grpc,可能只需要几十毫秒,然而处理大量数据的同步操作则可能要很长时间。需要考虑多方面因素来决定这个超时时间,例如系统间端到端的延时,哪些rpc是串行的,哪些是可以并行的等等。

教程源码地址:https://github.com/bingjian-zhu/go-grpc-example

参考:https://grpc.io/blog/deadlines/

以上就是go grpc进阶服务超时设置的详细内容,更多关于go grpc超时设置的资料请关注其它相关文章!

《Go gRPC进阶教程服务超时设置.doc》

下载本文的Word格式文档,以方便收藏与打印。