4
头图

In the previous version, the heartbeat detection function of multi-port monitoring can only be configured on the main service, and the heartbeat time cannot be set separately for each port.

For example, you need to set 30 9501 port, and 60 seconds on the 9502 It v4.7 version 060e7211227921, let’s take a look at it in detail below.

Configuration item

Two configuration items are provided in Server heartbeat_check_interval and heartbeat_idle_time .

You can use the following configuration items to increase heartbeat detection:

$server->set([
    'heartbeat_check_interval' => 60,
    'heartbeat_idle_time'      => 120,
]);

heartbeat_check_interval indicates how often to cycle, in seconds. For example, heartbeat_check_interval => 60 means that all connections are traversed 60

If the connection 120 seconds (when heartbeat_idle_time not set, the default is interval ), the connection will be forcibly closed.

heartbeat_idle_time indicates the maximum allowable idle time for the connection.

Example

Here is a code for multi-port monitoring for testing, setting heartbeat detection for different ports:

To facilitate testing, set the heartbeat detection time to 1 second
use Swoole\Server;

$server = new Server('127.0.0.1', 9501, SWOOLE_BASE);
$server->set([
    'heartbeat_check_interval' => 1,
    'heartbeat_idle_time' => 1,
]);
$server->on('connect', function ($server, $fd) {
    $time = date('Y-m-d H:i:s');
    echo "[{$time}] Client#{$fd}: Connect.\n";
});
$server->on('receive', function ($server, $fd, $reactorId, $data) {
    $server->send($fd, 'ok');
});
$server->on('close', function ($server, $fd) {
    $time = date('Y-m-d H:i:s');
    echo "[{$time}] Client#{$fd}: Close.\n";
});

$port2 = $server->listen('127.0.0.1', 9502, SWOOLE_SOCK_TCP);
$port2->set([
    'heartbeat_idle_time' => 2,
]);

$port3 = $server->listen('127.0.0.1', 9503, SWOOLE_SOCK_TCP);
$port3->set([
    'heartbeat_idle_time' => 10,
]);

$server->start();

You can use telnet or Swoole's TCP client for testing.

telnet is used here for testing, and 3 ports of 060e7211227b are connected respectively

telnet 127.0.0.1 9501
telnet 127.0.0.1 9502
telnet 127.0.0.1 9503

Using the v4.6 version to test will output:

[2021-07-05 10:06:44] Client#1: Connect.
[2021-07-05 10:06:45] Client#2: Connect.
[2021-07-05 10:06:46] Client#3: Connect.
[2021-07-05 10:06:46] Client#1: Close.
[2021-07-05 10:06:47] Client#2: Close.
[2021-07-05 10:06:48] Client#3: Close.

The connections 1 , 2 , 3 are all disconnected after 2

Then use the latest v4.7 version to test:

[2021-07-05 10:02:50] Client#1: Connect.
[2021-07-05 10:02:51] Client#2: Connect.
[2021-07-05 10:02:51] Client#1: Close.
[2021-07-05 10:02:52] Client#3: Connect.
[2021-07-05 10:02:53] Client#2: Close.
[2021-07-05 10:03:02] Client#3: Close.
  • The connection 1 disconnected after 1
  • The connection 2 disconnected after 2
  • The connection 3 disconnected after 10

Such output results are in line with the configured heartbeat detection configuration, and users who need to use this function can have an upgrade experience.


沈唁
1.9k 声望1.2k 粉丝