关于grpc 的keepalive 的一些知识
1. GRPC keepalive 的相关知识
参照这个连接
// ClientParameters is used to set keepalive parameters on the client-side.
// These configure how the client will actively probe to notice when a
// connection is broken and send pings so intermediaries will be aware of the
// liveness of the connection. Make sure these parameters are set in
// coordination with the keepalive policy on the server, as incompatible
// settings can result in closing of connection.
type ClientParameters struct {// After a duration of this time if the client doesn't see any activity it// pings the server to see if the transport is still alive.// If set below 10s, a minimum value of 10s will be used instead.Time time.Duration // The current default value is infinity.// After having pinged for keepalive check, the client waits for a duration// of Timeout and if no activity is seen even after that the connection is// closed.Timeout time.Duration // The current default value is 20 seconds.// If true, client sends keepalive pings even with no active RPCs. If false,// when there are no active RPCs, Time and Timeout will be ignored and no// keepalive pings will be sent.PermitWithoutStream bool // false by default.
}// ServerParameters is used to set keepalive and max-age parameters on the
// server-side.
type ServerParameters struct {// MaxConnectionIdle is a duration for the amount of time after which an// idle connection would be closed by sending a GoAway. Idleness duration is// defined since the most recent time the number of outstanding RPCs became// zero or the connection establishment.MaxConnectionIdle time.Duration // The current default value is infinity.// MaxConnectionAge is a duration for the maximum amount of time a// connection may exist before it will be closed by sending a GoAway. A// random jitter of +/-10% will be added to MaxConnectionAge to spread out// connection storms.MaxConnectionAge time.Duration // The current default value is infinity.// MaxConnectionAgeGrace is an additive period after MaxConnectionAge after// which the connection will be forcibly closed.MaxConnectionAgeGrace time.Duration // The current default value is infinity.// After a duration of this time if the server doesn't see any activity it// pings the client to see if the transport is still alive.// If set below 1s, a minimum value of 1s will be used instead.Time time.Duration // The current default value is 2 hours.// After having pinged for keepalive check, the server waits for a duration// of Timeout and if no activity is seen even after that the connection is// closed.Timeout time.Duration // The current default value is 20 seconds.
}
grpc client 的keepalive 用来检测 client 创建的grpc channel 连接是不是可用的,如果超时,就会关掉这个channel 的连接
grpc server 的keepalive 用来检测 server 创建的grpc channel 连接是不是可用的,如果超时,就会关掉这个channel 的连接
在这里还需要特别注意一个问题, grpc client 的keepalive 的 时间设定 需要在server 允许范围内,否则,server 会认为你是捣蛋的,给你发送一个GOAWAY 消息,把和client 的连接强制关掉
这部分,具体我们来看grpc-go的相关定义:
// EnforcementPolicy is used to set keepalive enforcement policy on the
// server-side. Server will close connection with a client that violates this
// policy.
type EnforcementPolicy struct {// MinTime is the minimum amount of time a client should wait before sending// a keepalive ping.MinTime time.Duration // The current default value is 5 minutes.// If true, server allows keepalive pings even when there are no active// streams(RPCs). If false, and client sends ping when there are no active// streams, server will send GOAWAY and close the connection.PermitWithoutStream bool // false by default.
}
够清晰吧,所以在grpc server 端需要设置这个参数,来和client 端作匹配!
如果两者不匹配就会出现下面的错误
closing transport due to: connection error: desc = “error reading from server: EOF”, received prior goaway: code: ENHANCE_YOUR_CALM, debug data: “too_many_pings”
意思就是server 端说:”兄弟,你ping 的手速太快了,哥撑不住了,不带你玩了,滚吧!“。 说完就把连接诶给断了。
2. Java grpc server 的一些注意事项
如果要用java 写一个grpc server, 此时假定需要设定上面所述的匹配,应该要怎么做?
刚开始我们用的和官方demo一样的方式(io.grpc.ServerBuilder)创建server,但发现这个类io.grpc.ServerBuilder压根没有设置这个keepalive 参数的接口,后来在github 上看到这个issue:
https://github.com/grpc/grpc-java/issues/8991
NettyServerBuilder has the appropriate API. You’d need to use the APIs io.grpc.netty.NettyServerBuilder (and swap to grpc-netty) or the questionable io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder (to continue with grpc-netty). Neither option is great as grpc-netty-shaded is much preferred over grpc-netty but nominally it doesn’t really expose a public API.
As a short-term “get it working.” Using io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder is probably fair. But we need to add the keepalive methods to io.grpc.ServerBuilder.
确实NettyServerBuilder 有更丰富的接口,包含了许多设置keepalive 的api 接口。
其中就有限制client 的keepalive 的API, 让我们看下源码:
/*** Specify the most aggressive keep-alive time clients are permitted to configure. The server will* try to detect clients exceeding this rate and when detected will forcefully close the* connection. The default is 5 minutes.** <p>Even though a default is defined that allows some keep-alives, clients must not use* keep-alive without approval from the service owner. Otherwise, they may experience failures in* the future if the service becomes more restrictive. When unthrottled, keep-alives can cause a* significant amount of traffic and CPU usage, so clients and servers should be conservative in* what they use and accept.** @see #permitKeepAliveWithoutCalls(boolean)* @since 1.3.0*/public NettyServerBuilder permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit) {checkArgument(keepAliveTime >= 0, "permit keepalive time must be non-negative: %s",keepAliveTime);permitKeepAliveTimeInNanos = timeUnit.toNanos(keepAliveTime);return this;}
Okay, 祝您好运!
关于grpc 的keepalive 的一些知识相关推荐
- gRPC学习记录(一)--概念性知识
前几天刚发了一个如何学习一门新技术,现在正好遇到了要学习的东西,因为重新找了工作,所以新公司使用的技术需要自己快速上手,那么快速学习就是必须掌握的一门技能了.下面根据之前的博文展示如何快速入门一门新技 ...
- gRPC学习记录(三)--proto3知识
在上一篇中,得知proto文件是定义服务端和客户端通讯接口的标准,说白了就是客户端该传什么样的参数,服务端该返回什么样子的参数,客户端该怎么调用,是阻塞还是非阻塞,是同步还是异步,那么就需要对这个东西 ...
- gRPC的那些事 - streaming
gRPC是一个高性能.通用的开源RPC框架,其由Google主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf(Protocol Buffers)序列化协议开发,且支持众多开发 ...
- 聊聊 Docker Swarm 部署 gRPC 服务的坑
gRPC 是一个高性能.开源和通用的 RPC 框架,面向移动和 HTTP/2 设计,也是目前流行的微服务架构中比较突出的跨语言 RPC 框架. 一直以来,我们的微服务都是基于 gRPC 来开发,使用的 ...
- 详解 gRPC 客户端长连接机制实现
本文作者: 熊喵君,原文链接:https://pandaychen.github.io/2020/09/01/GRPC-CLIENT-CONN-LASTING/ 转载 Go语言中文网 公众号 Gola ...
- 不可不知的KeepAlive科普
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.什么是keepAlive? 二.TCP之KeepAlive详解 2.1 为什么需要KeepAlive? 2.2 如 ...
- Http Keep-Alive和Tcp keepalive介绍
Http Keep-Alive和Tcp keepalive介绍 1.TCP连接介绍 1.1 建立连接-三次握手 1.2 释放连接-四次挥手 2. KeepAlive与Keep-Alive介绍 2.1 ...
- gRPC教程 — TLS单向认证、双向认证、Token认证、拦截器
gRPC教程 - 使用TLS时相关问体积解决办法 本文代码 问题重现 解决办法 一.生成CA根证书 1.1 在证书存放文件夹下 新建 `ca.conf`,写入内容如下: 1.2 生成ca秘钥,得到ca ...
- 知识分享之Golang——使用gorm时进行执行自定义SQL的几种方式
知识分享之Golang--使用gorm时进行执行自定义SQL的几种方式 背景 知识分享之Golang篇是我在日常使用Golang时学习到的各种各样的知识的记录,将其整理出来以文章的形式分享给大家,来进 ...
- TIS教程04-客户端
简介 在之前的文章中,我们主要关注服务端的配置和部署,这无可厚非,因为Triton Inference Server本就是服务端框架.但是,作为一个完善的生态,Triton也对客户端请求做了诸多封装以 ...
最新文章
- 面向自动驾驶车辆的高效激光里程计(ICRA2021)
- telegraf input的配置
- opengl中的Floatbuffer和IntBuffer与java中数据的存储方式不同的解决方法,编辑一个自己的BufferUtil工具类
- Python小技巧:用 print() 函数实现的三个特效
- 浅谈PHP的Public、Protected、Private三种方法的区别
- 你光明,这世界便不黑暗!---2016年.3.16演讲稿
- php 使用postfix发邮件,PHP处理postfix邮件内容的方法
- 哈希表及哈希表查找相关概念(转)
- UIMenuController在label中的使用
- Python MySqlDB 增删改数据库(转载)
- java小区管理的项目描述,基于jsp的小区信息管理-JavaEE实现小区信息管理 - java项目源码...
- Coco2dx-3.0中怎样调用LUA
- 嵌入式linux工程师 考试,嵌入式Linux工程师常见笔试题.doc
- Cloudera Certified Associate Administrator案例之Install篇
- C# WMP 视频播放
- seo之html优化,SEO优化技巧之HTML优化
- django2.2-视图层详解
- 什么是复制和交换成语?
- 指纹特征提取及描述(附Python代码)
- 分销系统具有哪些大优势?