博客
关于我
Golang gRPC学习(04): Deadlines超时限制
阅读量:434 次
发布时间:2019-03-06

本文共 5259 字,大约阅读时间需要 17 分钟。

为什么要使用Deadlines

在使用gRPC时,客户端和服务器之间的连接、序列化、反序列化以及超时执行是常见的操作。Deadlines(截止时间)允许客户端设置等待多长时间完成RPC操作的限制,直到出现错误DEADLINE_EXCEEDED。默认情况下,DEADLINE_EXCEEDED的值通常设置得很高。

如果没有设置Deadlines,所有请求可能会在最大请求时间过后才超时,这会导致服务器资源消耗增加,如内存膨胀等问题。因此,为客户端请求程序设置一个默认超时时间是必要的,当请求在此时间内未返回时,会超时报错。

Deadlines的使用步骤

1. 设置Deadlines

var deadlineMs = flag.Int("deadline_ms", 20*1000, "Default deadline in milliseconds.")clientDeadline := time.Now().Add(time.Duration(*deadlineMs) * time.Millisecond)ctx, cancel := context.WithDeadline(ctx, clientDeadline)

2. 检查Deadlines

if ctx.Err() == context.Canceled {    return status.New(codes.Canceled, "Client cancelled, abandoning.")}

2.2 具体使用

1. 建立连接时超时控制

Dial()函数位于google.golang.org/grpc/clientconn.go中,实际执行由DialContext()函数完成。客户端应传入设置超时的context,如下所示:

ctx, cancel := context.Timeout(context.Background(), time.Second*5)defer cancel()conn, err := grpc.DialContext(ctx, address, grpc.WithBlock(), grpc.WithInsecure())

注意事项:

  • grpc.WithInsecure():跳过对服务器证书的验证,适用于练习,但在生产环境中不建议使用。
  • grpc.WithBlock():阻塞等待握手成功,否则超时控制失效。

2. 调用时超时

ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5)defer cancel()result, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})

实例

1. 定义服务echo.proto

syntax = "proto3";package echo;message EchoRequest {    string message = 1;}message EchoResponse {    string message = 1;}service Echo {    rpc UnaryEcho(EchoRequest) returns (EchoRequest) {}    rpc ServerStreamingEcho(EchoRequest) returns (stream EchoResponse) {}    rpc ClientStreamingEcho(stream EchoRequest) returns (EchoResponse) {}    rpc BidirectionalStreamingEcho(stream EchoRequest) returns (stream EchoResponse) {}}

2. 客户端client/main.go

都不是stream的函数

// unaryCall 不是stream的请求func unaryCall(c pb.EchoClient, requestID int, message string, want codes.Code) {    ctx, cancel := context.WithTimeout(context.Background(), time.Second)    defer cancel()    req := &pb.EchoRequest{Message: message}    _, err := c.UnaryEcho(ctx, req)    got := status.Code(err)    fmt.Printf "[%v] wanted = %v, got = %v\n", requestID, want, got}

2端都是stream的函数

// streamingCall,2端都是streamfunc streamingCall(c pb.EchoClient, requestID int, message string, want codes.Code) {    ctx, cancel := context.WithTimeout(context.Background(), time.Second)    defer cancel()    stream, err := c.BidirectionalStreamingEcho(ctx)    if err != nil {        log.Printf("Send error : %v", err)        return    }    err = stream.Send(&pb.EchoRequest{Message: message})    if err != nil {        log.Printf("Send error : %v", err)        return    }    _, err = stream.Recv()    got := status.Code(err)    fmt.Printf "[%v] wanted = %v, got = %v\n", requestID, want, got}

main 执行函数

func main() {    flag.Parse()    conn, err := grpc.Dial(*addr, grpc.WithInsecure(), grpc.WithBlock())    if err != nil {        log.Fatalf("did not connect : %v ", err)    }    defer conn.Close()    c := pb.NewEchoClient(conn)    // 成功请求    unaryCall(c, 1, "word", codes.OK)    // 超时 deadline    unaryCall(c, 2, "delay", codes.DeadlineExceeded)    // A successful request with propagated deadline    unaryCall(c, 3, "[propagate me]world", codes.OK)    // Exceeds propagated deadline    unaryCall(c, 4, "[propagate me][propagate me]world", codes.DeadlineExceeded)    // Receives a response from the stream successfully.    streamingCall(c, 5, "[propagate me]world", codes.OK)    // Exceeds propagated deadline before receiving a response    streamingCall(c, 6, "[propagate me][propagate me]world", codes.DeadlineExceeded)}

3. 服务端server/main.go

定义一个struct

type server struct {    pb.UnimplementedEchoServer    client pb.EchoClient    cc *grpc.ClientConn}

2端不是stream的函数

func (s *server) UnaryEcho(ctx context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) {    message := req.Message    if strings.HasPrefix(message, "[propagate me]") {        time.Sleep(800 * time.Millisecond)        message := strings.TrimPrefix(message, "[propagate me]")        return s.client.UnaryEcho(ctx, &pb.EchoRequest{Message: message})    }    if message == "delay" {        time.Sleep(1500 * time.Millisecond)    }    return &pb.EchoResponse{Message: message}, nil}

2端设置stream的函数

func (s *server) BidirectionalStreamingEcho(stream pb.Echo_BidirectionalStreamingEchoServer) error {    for {        req, err := stream.Recv()        if err == io.EOF {            return status.Error(codes.InvalidArgument, "request message not received")        }        if err != nil {            return err        }        message := req.Message        if strings.HasPrefix(message, "[propagate me]") {            time.Sleep(800 * time.Millisecond)            message := strings.TrimPrefix(message, "[propagate me]")            res, err := s.client.UnaryEcho(stream.Context(), &pb.EchoRequest{Message: message})            if err != nil {                return err            }            stream.Send(res)        }        if message == "delay" {            time.Sleep(1500 * time.Millisecond)        }        stream.Send(&pb.EchoResponse{Message: message})    }}

执行

运行server/main.go和client/main.go,执行结果如下:

go run main.go[1] wanted = OK, got = OK[2] wanted = DeadlineExceeded, got = DeadlineExceeded[3] wanted = OK, got = Unavailable[4] wanted = DeadlineExceeded, got = Unavailable[5] wanted = OK, got = Unavailable[6] wanted = DeadlineExceeded, got = Unavailable

转载地址:http://hzfyz.baihongyu.com/

你可能感兴趣的文章
python:如何检测串行 COM 上的设备名称/ID
查看>>
Python:如何模拟 datetime.utcnow()?
查看>>
Python:如果DataFrames之间的其他值匹配,则对DataFrame中的值求和
查看>>
Python:字典思维
查看>>
Python:将 HEX 字符串转换为字节
查看>>
Python:将列表与范围列表合并
查看>>
Python:将列表列表初始化为一定大小,
查看>>
Python:带有生成器的给定集合的幂集
查看>>
Python:按字符的位置拆分字符串
查看>>
Python:替换嵌套字典中的值
查看>>
Python:爬取数据出现response.status_code为403解决方法
查看>>
Python:爬取数据出现response.status_code为418解决方法
查看>>
Python:生成“元组集“;来自“元组列表“;不考虑顺序
查看>>
Python:生成具有趋势的随机时间序列数据(例如周期性、指数衰减等)
查看>>
Python:用渐进式数字重命名列表中的重复项而不对列表进行排序
查看>>
Python:矩形参数无效
查看>>
Python:语法进阶
查看>>
Python:调用复制函数时调用 Python 对象时超出了最大递归深度
查看>>
Python:这两个 import 语句有什么区别?
查看>>
Python:通过串口从 Velleman k8090 读取数据
查看>>