美文网首页
golang grpc keepalive

golang grpc keepalive

作者: hatlonely | 来源:发表于2019-11-27 20:22 被阅读0次

最近遇到 grpc 客户端报错 rpc error: code = Unavailable desc = transport is closing,原因是连接长时间没有使用,被服务端断开,这种情况通过简单粗暴的重试策略可以解决,更加优雅的解决方案是增加保持连接策略

服务端

var kaep = keepalive.EnforcementPolicy{
    MinTime:             5 * time.Second, // If a client pings more than once every 5 seconds, terminate the connection
    PermitWithoutStream: true,            // Allow pings even when there are no active streams
}

var kasp = keepalive.ServerParameters{
    MaxConnectionIdle:     15 * time.Second, // If a client is idle for 15 seconds, send a GOAWAY
    MaxConnectionAge:      30 * time.Second, // If any connection is alive for more than 30 seconds, send a GOAWAY
    MaxConnectionAgeGrace: 5 * time.Second,  // Allow 5 seconds for pending RPCs to complete before forcibly closing connections
    Time:                  5 * time.Second,  // Ping the client if it is idle for 5 seconds to ensure the connection is still active
    Timeout:               1 * time.Second,  // Wait 1 second for the ping ack before assuming the connection is dead
}

server := grpc.NewServer(grpc.KeepaliveEnforcementPolicy(kaep), grpc.KeepaliveParams(kasp))

客户端

var kacp = keepalive.ClientParameters{
    Time:                10 * time.Second, // send pings every 10 seconds if there is no activity
    Timeout:             time.Second,      // wait 1 second for ping ack before considering the connection dead
    PermitWithoutStream: true,             // send pings even without active streams
}

conn, err := grpc.Dial(*addr, grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp))

链接

转载请注明出处
本文链接:https://tech.hatlonely.com/article/52

相关文章

  • golang grpc keepalive

    最近遇到 grpc 客户端报错 rpc error: code = Unavailable desc = tran...

  • grpc-源码-网络模型

    golang 的grpc库是 https://github.com/grpc/grpc-go grpc serve...

  • golang安装gRpc

    golang安装gRpc安装官方安装命令: go get google.golang.org/grpc 是安装不起...

  • gRPC keepalive参数说明

    gRPC keepalive 的作用:gRPC 做服务间的通讯时,由于配置问题容易出现各类非预期的异常情况,能够很...

  • grpc 简单使用

    参考:golang grpc 快速开始[https://grpc.io/docs/languages/go/qu...

  • golang grpc

    golang grpc rpc种类 grpc含义 gRPC是Google的RPC框架,开源、高性能、跨语言,基于H...

  • Windows下grpc安装

    Golang 官方说明使用 go get google.golang.org/grpc 进行安装,但是由于代码已经...

  • 使用TypeScript与Golang进行gRPC通信

    title: 使用TypeScript与Golang进行gRPC通信tags: gRPCcategories: 后...

  • Golang gRPC

    gRPC介绍 gRPC 是一个高性能、开源、通用的RPC框架,由Google推出,基于HTTP/2协议标准设计开发...

  • grpc for golang

    作者: 一字马胡 转载标志 【2017-11-27】 更新日志 本文将简单介绍如何使用grpc,grpc支持多种...

网友评论

      本文标题:golang grpc keepalive

      本文链接:https://www.haomeiwen.com/subject/zoemwctx.html