Home  >  Article  >  What does heartbeat mechanism mean?

What does heartbeat mechanism mean?

coldplay.xixi
coldplay.xixiOriginal
2020-06-28 15:22:183898browse

The heartbeat mechanism is a mechanism that regularly sends a custom structure [heartbeat packet] to let the other party know that it is still alive to ensure the validity of the connection. It is sent every fixed time like a heartbeat.

What does heartbeat mechanism mean?

Heartbeat mechanism means:

The receiving and sending data in the network are all done using the operating system SOCKET is implemented. But if this socket has been disconnected, there will definitely be problems when sending and receiving data. But how to determine whether this socket can still be used? This requires creating a heartbeat mechanism in the system.

Related learning recommendations: PHP Programming from beginner to master

In fact, TCP has already implemented a mechanism called heartbeat for us. If you set a heartbeat, TCP will send the number of heartbeats you set (for example, 2 times) within a certain period of time (for example, you set it to 3 seconds), and this information will not affect the protocol you defined. . The so-called "heartbeat" is to send a custom structure (heartbeat packet or heartbeat frame) regularly to let the other party know that it is "online". to ensure the validity of the link.

The so-called heartbeat packet is that the client regularly sends simple information to the server to tell it that I am still there. The code is to send a fixed message to the server every few minutes, and the server will reply with a fixed message after receiving it. If the server does not receive the client message within a few minutes, the client will be considered disconnected.

For example, if some communication software is not used for a long time, if you want to know whether its status is online or offline, you need heartbeat packets and send and receive packets regularly. Contract sender: It can be the client or the server, whichever is more convenient and reasonable. Usually the client. The server can also periodically poll and send heartbeats.

Heartbeat packetThe reason why it is called heartbeat packet is that it is sent every fixed time like a heartbeat to tell the server that the client is still alive. In fact, this is to maintain a long connection. As for the content of this packet, there are no special regulations, but it is generally a very small packet, or an empty packet containing only the header.

In the TCP mechanism, there is a heartbeat packet mechanism, which is a TCP option. The system default setting is a 2-hour heartbeat frequency. But it cannot detect machine power outages, network cable unplugging, or firewall disconnections. Moreover, the logic layer may not be so easy to deal with disconnection.

Generally, if it is just used to keep alive, it is still ok. Heartbeat packet Generally speaking, it is implemented by sending empty packets at the logical layer. The next timer sends an empty packet to the client at a certain time interval, and then the client feedbacks a same empty packet back. If the server cannot receive the feedback packet sent by the client within a certain period of time, it can only determine Said it was offline. Just send or recv. If the result is zero, it means it is offline.

However, under long connection, there may be no data exchange for a long time. Theoretically, this connection is always connected, but in practice, it is difficult to know if there is any failure on the intermediate node. What's even worse is that some nodes (firewalls) will automatically disconnect connections that have no data interaction within a certain period of time.

At this time, we need our heartbeat packet to maintain long connections and keep them alive. After learning of the disconnection, the server logic may need to do some things, such as cleaning the data after the disconnection and reconnecting. Of course, this is naturally done by the logic layer according to the needs. In general, heartbeat packets are mainly used for keep-alive and disconnection processing of long connections. For general applications, a judgment time of 30-40 seconds is relatively good. If the requirements are really high, then set it to 6-9 seconds.

What does heartbeat mechanism mean?

The above is the detailed content of What does heartbeat mechanism mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn