当前位置:网站首页>Grpc long connection keepalive
Grpc long connection keepalive
2022-04-23 05:05:00 【canger_】
background
Encountered in the project grpc The two-way flow and one-way flow appear the service end flow context Report errors :context cancel error, And the client is not aware of the disconnection , Still think it's still connected , Cause data transmission interruption .
In this case, think about the possibility of the problem from the following dimensions :
- Is the network stable , Can you reproduce
- Whether there is a connection between the client and the server context Conduct cancel
- Whether the client and server have to deal with context Of cancel The signal
Because the problem is not in the development environment , Therefore, you can only check the log at first , But only the server will receive in the log context Of cancel The signal , The client does not have any exceptions . So try to replicate it in the development environment , This exception is reproduced by unplugging the client network cable . The solution is simple , You can add heartbeat to the streaming interface , If you haven't received your heartbeat for a long time, you'll be disconnected and reconnected . But is there a more concise and elegant way ? yes , we have , The answer is grpc Of keepalive Parameters .
KeepAlive
keepalive ping It is a kind of transmission HTTP2 ping To check whether the channel is currently working . It is sent periodically , If ping No confirmation from the peer within a certain timeout , Then the transmission is disconnected .
Client's grpc keepalive Parameters
// 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.
}
keepalive.ClientParameters The meaning of the parameter is as follows :
- Time: without activity, Then send one... Every this value ping package
- Timeout: If ping ack If it is not returned within this value, the connection is considered disconnected
- PermitWithoutStream: without active Of stream, Is it allowed to send ping
Server side grpc keepalive Parameters
// 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.
}
// 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.
}
keepalive.EnforcementPolicy:
- MinTime: If the client twice ping The interval between is less than this value , Close the connection
- PermitWithoutStream: Even without active stream, It is also allowed to ping
keepalive.ServerParameters:
- MaxConnectionIdle: If one client Idle exceeds this value , Send a GOAWAY, To prevent sending a large number of at the same time GOAWAY, Will float up and down this time interval 10%, For example, set to 15s, namely 15+1.5 perhaps 15-1.5
- MaxConnectionAge: If the lifetime of any connection exceeds this value , Send a GOAWAY
- MaxConnectionAgeGrace: Between forcibly closing the connection , Allow time with this value to complete pending Of rpc request
- Time: If one client Idle exceeds this value , Send a ping request
- Timeout: If ping Request no reply received within this time period , The connection is considered disconnected
Official sample code
server
https://github.com/grpc/grpc-go/blob/master/examples/features/keepalive/server/main.go
client
https://github.com/grpc/grpc-go/blob/master/examples/features/keepalive/client/main.go
版权声明
本文为[canger_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220552063257.html
边栏推荐
- Sword finger offer: symmetric binary tree (recursive iteration leetcode 101)
- DIY 一个 Excel 版的子网计算器
- Leetcode -- heuristic search
- [winui3] Écrivez une copie du gestionnaire de fichiers Explorer
- Innovation training (V) configuration information
- Deep learning notes - fine tuning
- PHP 统计指定文件夹下文件的数量
- Chapter II project scope management of information system project manager summary
- Learning Android from scratch -- Introduction
- 深度学习笔记 —— 微调
猜你喜欢
Innovation training (V) configuration information
MySQL - index
Excel protects worksheets and workbooks from damage
深度学习笔记 —— 语义分割和数据集
MySQL circularly adds sequence numbers according to the values of a column
Pixel 5 5g unlocking tutorial (including unlocking BL, installing edxposed and root)
COM in wine (2) -- basic code analysis
redis数据类型有哪些
Use the built-in function of win to transfer files between two computers in the same LAN (the speed is the same as that between local disks)
[2022 ICLR] Pyraformer: Low-Complexity Pyramidal Attention for Long-Range 时空序列建模和预测
随机推荐
直播带货表格模板-自动显示图片-自动关联系列商品
Analysis of POM files
Restful toolkit of idea plug-in
Set Chrome browser background to eye protection (eye escort / darkreader plug-in)
独立站运营 | FaceBook营销神器——聊天机器人ManyChat
redis数据类型有哪些
[2022 ICLR] Pyraformer: Low-Complexity Pyramidal Attention for Long-Range 时空序列建模和预测
信息学奥赛一本通 1955:【11NOIP普及组】瑞士轮 | OpenJudge 4.1 4363:瑞士轮 | 洛谷 P1309 [NOIP2011 普及组] 瑞士轮
PHP counts the number of files in the specified folder
DIY is an excel version of subnet calculator
The 2021 more reading report was released, and the book consumption potential of post-95 and Post-00 rose
Deep learning notes - semantic segmentation and data sets
Learning Android II from scratch - activity
This call when the transaction does not take effect
vscode ipynb文件没有代码高亮和代码补全解决方法
C. Tree infection (simulation + greed)
深度学习笔记 —— 语义分割和数据集
JS determines whether the numeric string contains characters
PHP+MySQL 制作留言板
MySQL realizes row to column SQL