Muduo Network Programming Example (eight) kick off the idle connection with Timing wheel

Source: Internet
Author: User
Tags data structures

This article describes how to use the timing wheel to kick off an idle connection, and a connection is considered an idle connection if it does not receive data for several seconds.

The code for this article is shown in Http://code.google.com/p/muduo/source/browse/trunk/examples/idleconnection

In a serious network program, the application layer's heartbeat protocol is essential. The heartbeat message should be used to determine if the other process is working correctly, and "kicking off the Idle connection" is only a temporary expedient. I would like to say a few words about the use of shared_ptr and weak_ptr.

If a connection is disconnected for several seconds (after 8s for example), there are two simple and outrageous ways to do it:

Each connection saves "the last time the data is received Lastreceivetime", and then uses a timer, traversing all connections every second, disconnecting those (now-connection.lastreceivetime) > 8s connection. There's only one repeated timer on the whole, but every time timeout check all the connections, this step can be time-consuming if the number of connections is larger (thousands of tens of thousands).

Each connection is set to a one-shot timer, the timeout is 8s, and the connection is disconnected when the timeout is exceeded. Of course, every time you receive the data, you update the timer. This approach requires a lot of one-shot timer, which updates timers frequently. If the number of connections is large, it may cause pressure on the reactor timer queue.

The use of timing wheel can avoid the shortcomings of these two approaches. Timing Wheel can be translated as "time Wheel" or "dial", this article is reserved in English.

Connection timeout does not require precise timing, as long as the approximate 8 seconds to break the line, more than a second less than a minute relationship. Processing a connection timeout can be a simple data structure: A 8-barrel loop queue. The first bucket drops the connection that will timeout for one second, and the second drops the connection that will timeout for 2 seconds. Once each connection receives the data, it puts itself in the 8th bucket, then disconnects the first barrel in the callback per second, moving the empty bucket to the end of the line. This can be done roughly 8 seconds without data timeout disconnected. More importantly, every time you do not have to check all the connection, just check the first bucket of connections, the equivalent of the task scattered.

Timing Wheel Principle

"Hashed and hierarchical timing wheels:efficient data structures for implementing a timer facility" This paper is a detailed comparison of the implementation of the timer of the various database structures , and puts forward the new structure of hierarchical timing wheel and hash timing wheel. To solve the problem in this article, we do not need to implement a universal timer, only the realization of simple timing wheel can be.

The basic structure of simple timing wheel is a circular queue, and a pointer to the tail of the team (tail), which moves one grid at a second, just like the hand hour on the clock, timing wheel hence the name.

The following is the state of timing wheel in a moment, the number in the lattice is the countdown (contrary to the usual timing wheel), indicating the remaining life of the connection in this lattice (bucket).

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.