Use a two-way linked list and hashtable to accelerate heartbeat timeout Detection

Source: Internet
Author: User

"Heartbeat" timeout detection is almost allC/SStructure of the application to handle a problem. The server needs to use it to promptly clean up unexpected client interruptions.This article hopes to change the detection time fromO (Number of all connected clients)DownO (Number of clients that have timed out).

In general, this operation needs to traverse all clients in the connection state to find out which clients have timed out,AlgorithmComplexity:O (N). In a multi-threaded server, you need to lock the list of all clients recorded during the detection. If the algorithm execution time is long, other threads in the system that need to access the list of clients will be blocked.

The following describes the specific measures for changing the space time:

Data Structure

We use a two-way linked list to store the list of connected clients. The addition and receipt of Heartbeat packet processing operations of this list keep the linked list in ascending order of the absolute time of the last heartbeat packet received. Use anotherHashtableTo store the ing between client objects and two-way linked list nodes.

 

Add new connection

Add a new client to the end of the queue (complexityO (1)), And put the client object and its corresponding double-stranded table Node objectHashtableMedium(Complexity is alsoO (1 )).

 

Heartbeat Packet Processing

Once the underlying transmission receives the "Heartbeat" packet sent by a clientHashtableExtracts the Node object of a two-way linked list (complexityO (1)) To move the node to the end of the linked list.(Complexity is alsoO (1 )).

 

Delete Connection

If a client Exits normally or times outHashtableExtracts the Node object of a two-way linked list (complexityO (1)), And thenHashtableDelete the node from the two-way linked list.(Complexity is alsoO (1 )).

 

"Heartbeat" timeout Detection

the heartbeat status of the client is detected one by one from the header node of the two-way linked list. The detection can be stopped after the first normal client is encountered.

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.