当前位置:网站首页>Teach you how to realize an efficient im long connection adaptive heartbeat keeping mechanism
Teach you how to realize an efficient im long connection adaptive heartbeat keeping mechanism
2022-07-21 22:53:00 【hellojackjiang2011】
The author of this article “Carson”, Now working in Tencent , The original title is “ High efficiency and long connection : Hand in hand to teach you to realize the adaptive heartbeat preservation mechanism ”, There are many revisions and changes .
1、 introduction
When you want to achieve IM Instant messaging chat 、 When high real-time requirements such as message push , We usually choose the communication mode of long connection .
And when the long connection mode is really realized , There will be a lot of technical problems , For example, the most common problem of keeping long connections alive .
today , I will pass this article , Hand in hand to teach you to realize a set of adaptive heartbeat preservation mechanism , Thus, it can efficiently and stably maintain such as IM Long connections for such needs as chat .
Exchange of learning :
- Mobile IM Introduction to development :《 Beginner level one is enough : Develop mobile from scratch IM》
- Open source IM The framework source code :https://github.com/JackJiang2011/MobileIMSDK
( This article is published synchronously in :http://www.52im.net/thread-3908-1-1.html)
2、 Related articles
- 《 Why based on TCP The mobile end of the protocol IM We still need a heartbeat mechanism ?》
- 《 Read and understand the network heartbeat packet mechanism in instant messaging applications : effect 、 principle 、 Implementation ideas, etc 》
- 《 A kind of Android End IM The design and implementation of intelligent heartbeat algorithm ( With sample code )》
- 《 Develop by yourself IM Is it that hard ? I'll teach you to roll one by yourself Andriod Version easy IM ( Source code )》
- 《 Learn from the source code IM( One ): I'll teach you how to use Netty Implement heartbeat mechanism 、 Disconnection and reconnection mechanism 》
- 《 Learn from the source code IM( 5、 ... and ): Correct understanding IM A long connection 、 Heartbeat and reconnection mechanism , And do it 》
3、 What is long connection
Know the long connection :
The main function of long-term connection is to keep both sides connected for a long time , thus :
- 1) Speed up communication ;
- 2) Ensure real-time ;
- 3) Avoid the waste of channel resources and network resources caused by repeated connections in a short time .
The difference between long connection and short connection :
PS: about IM For developers like this , Usually people put HTTP The agreement says “ Short connection ”、 Based directly on TCP、UDP or WebSocket Of socket be called “ A long connection ”.
4、 Cause of long connection disconnection
4.1 Basic concepts
From the last section we can see that , With long connections , All communications between the parties are based on 1 The length of the bar is connected to ( such as 1 Time TCP Connect ). therefore , A long connection requires a continuous connection between both parties to enable both parties to communicate continuously .
However , The reality is that , The connection will be broken for a long time .
The main reasons for these disconnections are :
- 1) The process of the long connection is killed ( This mainly refers to the mobile terminal );
- 2)NAT Overtime ;
- 3) Network state changes ;
- 4) Other irresistible factors ( The state of the network is poor 、DHCP And so on ).
below , I will analyze each reason .
4.2 make a concrete analysis
1) reason 1: Process killed
When the process is killed , The long connection will also be broken . The process was killed in Andriod End is the most common problem , Limited to space, I will not start this topic here , If you are interested, you can read this :《Android P The official edition is coming : Backstage application to keep alive 、 The real nightmare of news push 》.
2) reason 2:NAT Overtime ( Focus on )
NAT The overtime phenomenon is as follows :
Of each operator and region NAT The timeout is as follows :
PS: The above data comes from the wechat team 《 Mobile IM practice : Realization Android Version of wechat's intelligent heartbeat mechanism 》 One article , With 4G、5G The popularity of , These data may have changed , Please refer to the actual test results .
Particular attention : Exclude other external causes ( Network switching 、NAT Overtime 、 For human reasons ),TCP A long connection is in a situation where both sides are constantly disconnected , It doesn't automatically interrupt ( That is, you don't need a heartbeat package to maintain , You can verify that : Give Way 2 A computer is connected to 1 individual Wifi, among 1 Taiwan as a server , another 1 Taiwan does client connection server ( No settings KeepAlive). As long as the computer 、 The router is constantly losing power , that ,2 The long connection of a computer is not automatically interrupted ).
Jack Jiang notes : The above statement may not be accurate , Readers with new interests can read in detail 《 Unplug the network cable and plug it in ,TCP Is the connection still there ? You can understand... In a word !》.
3) reason 3: Network state changes
When the mobile client network state changes ( Like the mobile network & Wifi Switch 、 To break off 、 Reconnection ), It also breaks long connections .
4) reason 4: Other irresistible factors
For example, the network status is poor 、DHCP And so on , Will make the long connection happen Accidental disconnection .DHCP The lease term of : about Android System , DHCP When it comes to the lease term, it will not renew the contract voluntarily ( Continue to use expired IP), The connection is long, resulting in disconnection .
5、 Efficient solution to maintain long connections
5.1 Basic introduction
After understanding the reason why the long connection is disconnected , For these reasons , Here is my solution to efficiently maintain long connections ( As shown in the figure below ).
So , To effectively maintain long connections , It needs to be done :
Put it simply , The key to maintaining a long connection efficiently is :
- 1) Keep alive : Try not to disconnect when you are connected ;
- 2) Reconnection : After the connection is broken, you should be able to continue to reconnect .
5.2 Specific measures
1) measures 1: Process keeping alive
The overall summary is as follows :
PS: About Android Keep the process alive , This topic is very popular , If you are interested, you can read the following article in detail :
- 《 Application of the ultimate summary of life preservation ( One ):Android6.0 The following two process daemons live 》
- 《 Application of the ultimate summary of life preservation ( Two ):Android6.0 And the above living practice ( Progress prevention )》
- 《 Application of the ultimate summary of life preservation ( 3、 ... and ):Android6.0 And the above living practice ( Killed and resurrected )》
- 《Android Detailed explanation of process assurance : One article solves all your questions 》
- 《 Wechat team original sharing :Android Version of wechat backstage to keep the real battle sharing ( Keep the process alive )》
- 《Android P The official edition is coming : Backstage application to keep alive 、 The real nightmare of news push 》
- 《 Take a comprehensive inventory of the current Android The real operation effect of the backstage scheme ( end 2019 Years ago )》
- 《2020 Years. ,Android Is there any play in the backstage ? See how I can achieve it gracefully !》
- 《 The strongest in history Android Keep alive : In depth analysis of Tencent TIM The process of immortality 》
- 《Android Process immortality technology finally revealed : The underlying principle of process killing 、APP Coping skills 》
- 《Android Keep alive from the beginning to the end : Guide users to add white list ( attach 7 Example of adding white for large models )》
2) measures 2: Heart beating mechanism
This is the focus of this article , The next section begins with a detailed analysis of
3) measures 3: Disconnection and reconnection mechanism
The principle is : Detect the change of network state and judge the effectiveness of connection in time .
Concrete realization : This is actually a complete set of logic with the heartbeat preservation mechanism , So the following will be explained in the heart beating mechanism .
6、 Brief introduction of heart beating mechanism
The overall introduction of heart beating mechanism is as follows :
however , Many people easily confuse the heartbeat mechanism with the traditional HTTP The polling mechanism is confused .
Here is the difference between the two :
7、 Main stream IM Analysis and comparison of heartbeat mechanism
To the country 、 The movement of internal and external mainstream IM product (WhatsApp、Line、 WeChat ) Simple analysis and comparison of heartbeat mechanism .
Please see the following picture for details :
PS: The above data comes from the data shared by wechat team 《 Mobile IM practice :WhatsApp、Line、 Heartbeat strategy analysis of wechat 》 One article .
8、 Overall design of heartbeat preservation mechanism scheme
below , I will follow the mainstream heartbeat mechanism on the market , A scheme of heartbeat mechanism is designed .
The basic flow of heartbeat mechanism scheme :
The main consideration for the scheme design of heartbeat mechanism is :
- 1) To ensure the real-time performance of messages ;
- 2) Consider the resources that consume the equipment ( The network traffic 、 Electric quantity 、CPU wait ).
As can be seen from the above figure , The key to the design of heartbeat mechanism is :
- 1) The specification of the heartbeat bag ( Content & size );
- 2) The interval between heartbeat sending ;
- 3) Disconnection and reconnection mechanism ( The core = how Judging the validity of long connections ).
In the following scheme design , Will aim at this 3 Give a detailed solution to the problem .
9、 Detailed design of heartbeat mechanism scheme
9.1 The specification of the heartbeat bag
In order to reduce traffic and improve transmission efficiency , Need to streamline the design of the heartbeat pack .
It mainly starts with the content and size of the heartbeat package , The design principles are as follows :
design scheme :
Heartbeat bag = 1 Carry a small amount of information & Size in 10 Packets in bytes
9.2 The interval between heartbeat sending
in order to prevent NAT Timeout and reduce the consumption of device resources ( The network traffic 、 Electric quantity 、CPU wait ), The interval of heartbeat transmission is the focus of the whole heartbeat mechanism scheme design .
The design principle of heartbeat interval is as follows :
9.3 The most commonly used heartbeat interval scheme
commonly , The most direct and commonly used heartbeat transmission interval setting scheme mostly adopts :“ Every estimate x Minutes to send heartbeat packets 1 Time ”. among ,x <5 minutes ( Integrated mainstream mobile IM product , It is suggested that x= 4 minute ).
however , There are some problems with this scheme :
PS: The specific implementation of the scheme of fixed heartbeat interval , You can refer to :
- 《 Learn from the source code IM( One ): I'll teach you how to use Netty Implement heartbeat mechanism 、 Disconnection and reconnection mechanism 》;
- 《 Learn from the source code IM( 5、 ... and ): Correct understanding IM A long connection 、 Heartbeat and reconnection mechanism , And do it 》;
- 《 Develop by yourself IM Is it that hard ? I'll teach you to roll one by yourself Andriod Version easy IM ( Source code )》.
9.4 Adaptive heartbeat interval scheme
below , I will explain in detail the design scheme of adaptive heartbeat interval .
Basic logic :
The solution needs to be solved 2 A core question .
1) How to adaptively calculate the heartbeat interval So that the heartbeat interval near At present NAT Timeout time ?
answer : Increasing heart rate interval for heart rate response test , Until the heartbeat fails 5 Next time , You can find the closest to At present NAT Time out of the heartbeat interval .
Please see the following picture for details :
notes : Only when the heartbeat interval near NAT Timeout time when , To maximize the balance Long connection is not interrupted & The lowest consumption of equipment resources .
2) How to detect In the current network environment NAT Timeout time There is a change ?
answer : Sending heartbeat packet successfully The maximum interval time of ( That is, the closest to NAT The heartbeat interval of the timeout period ) fail in send 5 Next time , Then judge the current network environment NAT Timeout time There is a change .
Please see the following picture for details :
notes : In the detection of NAT Timeout time After the change , Recalculate the heartbeat interval adaptively So that the heartbeat interval near NAT Timeout time
To sum up : Overall planning above 2 A core question , The design scheme of self adaptive heartbeat interval is summarized in the figure below :
PS: Design and implementation of adaptive heartbeat mechanism , You can refer to :
- 《 Mobile IM practice : Realization Android Version of wechat's intelligent heartbeat mechanism 》;
- 《 A kind of Android End IM The design and implementation of intelligent heartbeat algorithm ( With sample code )》.
10、 Implementation of disconnection and reconnection mechanism
Technically : Keeping the heartbeat alive for long connections depends on the heartbeat mechanism , When the heartbeat mechanism works , Timely start the disconnection and reconnection mechanism , Only under the joint action of heartbeat mechanism and disconnection reconnection mechanism can the real heartbeat be kept alive . But to make the logic clearer , I will explain the disconnection reconnection mechanism and the heartbeat mechanism in a separate section . This section is about the fragment line reconnection mechanism .
The core of the mechanism is : How to judge the validity of long connections . namely : Under what circumstances is a long connection considered broken ?
1) Design principles :
The basic logic is : Criteria for judging whether a long connection is valid = Whether the server returns a heartbeat response .
Here, we need to distinguish the long connection “ Survive & It works “ The difference between States :
2) The specific plan :
Realize the idea : By counting , If continuous 5 After sending heartbeat times , No heartbeat response from servers , The long connection is considered invalid .
Judgment process :
3) The scheme circulated on the Internet :
There are some schemes circulating on the Internet to judge whether the long connection is effective , The details are as follows :
thus , The mechanism of heart beating is explained .
11、 Program summary
It is necessary to summarize the heartbeat mechanism and disconnection reconnection mechanism I shared in the last two sections , These two mechanisms constitute the long connection heartbeat keep alive complete logic in this paper .
design scheme :
Process design :
notes : identification “ gray ” Please refer to the above description for the judgment process of .
12、 Further optimize and improve the heartbeat preservation scheme
12.1 Basic information
The solutions in the previous two sections will still have technical defects , The connection is long, resulting in disconnection ( such as : The long connection itself is not available ( It doesn't matter how many times you have to do it again )).
The following will optimize and improve the above scheme , To ensure that The client and the server are still communicating .
The main optimization point is :
- 1) Ensure the effectiveness and stability of the current network before starting a long connection ;
- 2) Timing of adaptive calculation of heartbeat packet interval time .
12.2 Ensure the effectiveness and stability of the network before starting a long connection
Problem description :
Solution :
Add to the main process of the original heartbeat preservation mechanism :
12.3 Timing of adaptive calculation of heartbeat packet interval time
Problem description :
The project design :
Add to the main process of the original heartbeat preservation mechanism :
12.4 To sum up
13、 Extra thinking :TCP Provided by the agreement KeepAlive Whether the mechanism can replace the heartbeat mechanism ?
Many people think that ,TCP The agreement itself has KeepAlive Mechanism , Why based on its communication links , Additional heartbeat keeping mechanism still needs to be implemented in the application layer ?
- The conclusion is that : There is no substitute for ;
- as a result of :TCP KeepAlive The function of the mechanism is to detect whether there is a connection ( Life and death ), But we can't detect if the connection is valid .
notes :“ The connection works ” The definition of = Both parties have the ability to send & The ability to receive messages .
First look at it. KeepAlive What is the mechanism :
KeepAlive The specific reasons why the mechanism cannot replace the heartbeat mechanism are as follows :
Particular attention :
- 1)KeepAlive Mechanism is just a passive mechanism at the bottom of the operating system , Should not be used by the upper application layer ;
- 2) When the system is shut down, a KeepAlive When the mechanism checks for dead connections , It won't actively notify the upper application , Only by calling the corresponding IO Found in the return value of the operation .
To sum up, it is :KeepAlive The mechanism can't replace the heartbeat mechanism , It needs to be in the application layer Implement heartbeat mechanism to detect the validity of long connection , Thus maintaining long connections efficiently .
Jack Jiang notes : About TCP Of itself KeepAlive Mechanism , May read in detail :
- 《 Why based on TCP The mobile end of the protocol IM We still need a heartbeat mechanism ?》
- 《 Understand thoroughly TCP Protocol layer KeepAlive The survival mechanism 》
14、 This paper summarizes
After reading this article , We believe that the long-term connection is efficient , You can solve it perfectly !
The main design of this scheme is :
The content of the optimization and improvement of the scheme is :
15、 Reference material
[1] TCP/IP Detailed explanation volume 1: agreement
[2] Why based on TCP The mobile end of the protocol IM We still need a heartbeat mechanism ?
[3] Understand thoroughly TCP Protocol layer KeepAlive The survival mechanism
[5] Mobile IM practice : Realization Android Version of wechat's intelligent heartbeat mechanism
[6] Mobile IM practice :WhatsApp、Line、 Heartbeat strategy analysis of wechat
[10] 2020 Years. ,Android Is there any play in the backstage ? See how I can achieve it gracefully !
( This article is published synchronously in :http://www.52im.net/thread-3908-1-1.html)
边栏推荐
- Cocos creator 3.2 realizes the complete effect of 2D map 3D character 45 degree RPG game
- Cocos Creator 3.2 中实现2D地图3D人物45度角RPG游戏效果笔记(摄像机设置方案)
- Mapping between sets and potential of sets
- 第四周ACM训练报告
- 【oops-framework】配套热更新插件
- Watermelon book chapter 2 Notes - evaluation method
- D - AND and SUM (AtCoder Beginner Contest 238)
- 19. Delete the penultimate node of the linked list
- ACM training problem solution set record
- 二叉堆的c#实现
猜你喜欢
随机推荐
437. Path sum III
994. Rotten oranges
二叉堆的c#实现
力扣记录:动态规划5子序列问题(3)回文——647 回文子串,516 最长回文子序列
如何在 Unity Editor 中录制游戏界面
Winter vacation learning summary
2022ACM夏季集训周报(一)
【oops-framework】配套游戏配置数据生成 + 数据对象代码生成器插件
【oops-framework】音频管理
本地计算机上的MySQL57服务启动后停止
数据库初学笔记
110. Balanced binary tree
tmux学习记录
Watermelon book chapter 2 Notes - evaluation method
10th week ACM training report
MySQL安装
【oops-framework】配套热更新插件
MySql the first
力扣记录:动态规划2背包问题(2)完全背包——518 零钱兑换II,377 组合总和IV,70 爬楼梯,322零钱兑换,279 完全平方数,139 单词拆分
Force deduction record: dynamic programming 5 Subsequence Problems (2) editing distance - 392 judging subsequences, 115 different subsequences, 583 deleting operations of two strings, 72 editing dista