1 | <?php |
||
7 | class TimeHeartbeat implements HeartbeatInterface |
||
8 | { |
||
9 | /** |
||
10 | * @var int |
||
11 | */ |
||
12 | private $heartbeatDelay; |
||
13 | |||
14 | /** |
||
15 | * @var int |
||
16 | */ |
||
17 | private $lastServerBeat; |
||
18 | |||
19 | /** |
||
20 | * @var int |
||
21 | */ |
||
22 | private $lastClientBeat; |
||
23 | |||
24 | /** |
||
25 | * @var float |
||
26 | */ |
||
27 | private $serverBeatFactor = 1.5; |
||
28 | |||
29 | /** |
||
30 | * @var float |
||
31 | */ |
||
32 | private $clientBeatFactor = 0.75; |
||
33 | |||
34 | /** |
||
35 | * @var callable|string |
||
36 | */ |
||
37 | private $timeFunction; |
||
38 | |||
39 | /** |
||
40 | * @param int $heartbeatDelay |
||
41 | * @param callable|string $timeFunction |
||
42 | */ |
||
43 | 27 | public function __construct($heartbeatDelay, $timeFunction = 'time') |
|
48 | |||
49 | /** |
||
50 | * Heartbeat factor for server beats. This is multiplier for heartbeat delay when waiting for server heartbeat. |
||
51 | * |
||
52 | * Possible values: |
||
53 | * - Value of 1 means server connection will be considered dead if heartbeat didn't arrive in exact time of heartbeat. |
||
54 | * - Value less than 1 would mean heartbeat would be expected in less than heartbeat delay, which does not make |
||
55 | * much sense. |
||
56 | * - Value more than 1 would mean we allow extra delay for server heartbeats, for example factor of 2 would mean |
||
57 | * we allow server to delay heartbeat up to double of its expected time. |
||
58 | * |
||
59 | * @param float $serverBeatFactor |
||
60 | * |
||
61 | * @return $this |
||
62 | */ |
||
63 | 3 | public function setServerBeatFactor($serverBeatFactor) |
|
64 | { |
||
65 | 3 | $this->serverBeatFactor = $serverBeatFactor; |
|
66 | |||
67 | 3 | return $this; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Heartbeat factor for client beats. This is multiplier for heartbeat delay when deciding to send heartbeat. |
||
72 | * |
||
73 | * Possible values: |
||
74 | * - Value of 1 means heartbeat would be sent after exactly time of heartbeat delay. |
||
75 | * - Value less than 1 would mean heartbeat would be send more frequently than expected. It is a good idea to send |
||
76 | * heartbeats a bit more frequently. |
||
77 | * - Value more than 1 would mean heartbeat would be send less frequently than expected. This may cause server to |
||
78 | * close connection because heartbeats will be delayed. |
||
79 | * |
||
80 | * @param float $clientBeatFactor |
||
81 | * |
||
82 | * @return $this |
||
83 | */ |
||
84 | 4 | public function setClientBeatFactor($clientBeatFactor) |
|
85 | { |
||
86 | 4 | $this->clientBeatFactor = $clientBeatFactor; |
|
87 | |||
88 | 4 | return $this; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | 21 | public function serverBeat() |
|
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | 21 | public function clientBeat() |
|
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | 21 | public function shouldSendHeartbeat() |
|
111 | { |
||
112 | 21 | if ($this->heartbeatDelay == 0) { |
|
113 | 1 | return false; |
|
114 | } |
||
115 | |||
116 | 20 | return $this->currentTime() - $this->lastClientBeat >= $this->heartbeatDelay * $this->clientBeatFactor; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | 4 | public function isServerHeartbeatMissing() |
|
123 | { |
||
124 | 4 | if ($this->heartbeatDelay == 0) { |
|
125 | 1 | return false; |
|
126 | } |
||
127 | |||
128 | 3 | return $this->currentTime() - $this->lastServerBeat > $this->heartbeatDelay * $this->serverBeatFactor; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * @return int |
||
133 | */ |
||
134 | 25 | private function currentTime() |
|
138 | } |
||
139 |