Heartbeat of swoole-php Tutorial

Source: Internet
Author: User
Talking about swoole's heartbeat comes from a bucket's article about swoole's heartbeat. as a Swoole consultant, I have to push this article, the last step is to leave only one configuration. In fact, I am not very clear about the principle. I am wondering if the Lan still needs a heartbeat?

----------------------------------------------------------------------

Swoole provides a heartbeat function, and many friends are confused.

What is heartbeat?

As the name suggests, heartbeat is a standard for determining whether a thing is born or dead. in swoole, heartbeat is used to determine whether a connection is normal or disconnected.

Starting from the TCP protocol

We all know that a quintuple identifies a network connection, creates a connection with three handshakes, and disconnects a connection with four waves. Whether it is a server or a client

When the connection is closed, the entire process of four waves is completed. in this way, everything is perfect. The system recycles this fd, and the application layer can also handle related things through the onClose callback.

What is fd?

Fd is a file descriptor. in unix philosophy, everything is a file. this fd is the identifier that the system layer exposes to the business layer to represent a 5-tuple network connection. You can simply understand it as an index. through the operation on this fd, the system layer can find the corresponding connection and perform a series of operations, such as sending data to the network pupil, to close the connection.

Why heartbeat?

As mentioned earlier, if we want to close a connection, we can initiate a disconnect operation on fd at the business layer. take swoole as an example:

$ Server-> close ($ fd );

Under normal circumstances, the entire four waves are passed (swoole has an onClose callback), and the system recycles fd to be allocated to other connections.

Why does the system need to recycle fd? because fd resources are limited, reuse is required.

However, in some cases, if the network cable is suddenly unplugged or the optical cable is cut off during the blue Xiang drill, the server cannot detect the exception of the connection, but the connection is actually no longer valid, if there is no recycling mechanism, this type of connection will use up all fd, and the system will no longer be able to accept new connection requests, so there will be a heartbeat mechanism.

What is the heartbeat mechanism?

The heartbeat mechanism is a method used by the business layer to determine whether a connection is alive. There are two implementation methods:

1: The client regularly sends a Heartbeat packet to tell the server that I am still alive. the server periodically checks the list of all clients to see if the last Heartbeat packet is too long, if no heartbeat exists, the connection is determined to be dead and the connection is closed.

2: The server regularly asks all clients. are you still alive? If you are alive, give me a feedback. if you do not receive any feedback, do not.

What are the differences between the two heartbeat schemes?

The first solution is less stress on servers and networks, and more flexible, but the client needs to work with timed sending of heartbeat packets.

The second solution puts more pressure on servers and networks. We do not recommend this solution.

Implementation of heartbeat in swoole

Swoole adopts the first solution

Swoole starts a heartbeat thread independently in the main process and regularly polls all connections to determine the connection life and death. Therefore, the heartbeat of swoole does not block any business logic.

So how can we determine whether the connection is dead? Swoole has the time_t last_time field in the connection struct, which is used to store the timestamp of the last packet collection, and then compares with this time to determine whether the packet is alive.

Therefore, swoole has two configurations:

Heartbeat_check_interval: the time when the server regularly checks the online list.

Heartbeat_idle_time: The maximum idle time of the connection (if the difference between the Last Heartbeat packet time and the current time exceeds this value, the connection is considered invalid)

Configuration recommendations

We recommend that heartbeat_idle_time be more than twice that of heartbeat_check_interval.

This is twice as high as fault tolerance. one packet can be dropped.

More importantly, the network latency is taken into account.

You can adjust the renewal rate based on your actual business (several packets are allowed to be dropped ).

Supplement

1. the heartbeat mechanism is also provided at the system level, but the granularity is relatively rough and the time is longer, and the application layer is not flexible.

2. swoole also provides the ping function. by configuring the ping value, the swoole kernel can determine that it is only a heartbeat packet, but it does not need to forward data packets to the application layer (onReceive ).

3. heartbeat is not only exclusive to swoole. most tcp network services will consider this issue.

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.