Two strategies for implementing the heartbeat mechanism on the server

Source: Internet
Author: User

Most CS applications require a heartbeat mechanism. The heartbeat mechanism is generally implemented on both the server and client, and the two have the same implementation principle. The client does not care about performance.

If the application is TCP-based, you can simply achieve heartbeat through so_keepalive. When the keepalive timer is set, TCP sends a TCP segment check to the peer. If no ACK or RST is received, after several attempts, the peer is deemed to be nonexistent and the application is notified.Program. The disadvantage here is that the server actively sends detection packets, which has a slight impact on performance.

 

Application Implementation

The client starts a timer and keeps sending heartbeat;

The server returns a response after receiving the heartbeat;

The server starts a timer to determine whether the client exists. Two methods are listed here: Time Difference and simple sign.

 

1. Time Difference Policy

After receiving a heartbeat, record the current time (recorded as recvedtime ).

Determine the time when the timer arrives, and calculate the time when the heartbeat is not received (t) = Current Time-recvedtime (the time recorded above ). If t is greater than a specified value, the client times out.

 

2. Simple flag

After receiving a heartbeat, set the connection flag to true;

Determine the time when the timer arrives and view all the signs. If it is false, the peer is deemed to have timed out. If it is true, set it to false.

 

The above method is simpler than above, but the error of detecting whether a client is offline is a little large.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.