|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ButterAMQP\Heartbeat; |
|
4
|
|
|
|
|
5
|
|
|
use ButterAMQP\HeartbeatInterface; |
|
6
|
|
|
|
|
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
|
|
|
* @param int $heartbeatDelay |
|
36
|
|
|
*/ |
|
37
|
27 |
|
public function __construct($heartbeatDelay) |
|
38
|
|
|
{ |
|
39
|
27 |
|
$this->heartbeatDelay = $heartbeatDelay; |
|
40
|
27 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Heartbeat factor for server beats. This is multiplier for heartbeat delay when waiting for server heartbeat. |
|
44
|
|
|
* |
|
45
|
|
|
* Possible values: |
|
46
|
|
|
* - Value of 1 means server connection will be considered dead if heartbeat didn't arrive in exact time of heartbeat. |
|
47
|
|
|
* - Value less than 1 would mean heartbeat would be expected in less than heartbeat delay, which does not make |
|
48
|
|
|
* much sense. |
|
49
|
|
|
* - Value more than 1 would mean we allow extra delay for server heartbeats, for example factor of 2 would mean |
|
50
|
|
|
* we allow server to delay heartbeat up to double of its expected time. |
|
51
|
|
|
* |
|
52
|
|
|
* @param float $serverBeatFactor |
|
53
|
|
|
* |
|
54
|
|
|
* @return $this |
|
55
|
|
|
*/ |
|
56
|
3 |
|
public function setServerBeatFactor($serverBeatFactor) |
|
57
|
|
|
{ |
|
58
|
3 |
|
$this->serverBeatFactor = $serverBeatFactor; |
|
59
|
|
|
|
|
60
|
3 |
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Heartbeat factor for client beats. This is multiplier for heartbeat delay when deciding to send heartbeat. |
|
65
|
|
|
* |
|
66
|
|
|
* Possible values: |
|
67
|
|
|
* - Value of 1 means heartbeat would be sent after exactly time of heartbeat delay. |
|
68
|
|
|
* - Value less than 1 would mean heartbeat would be send more frequently than expected. It is a good idea to send |
|
69
|
|
|
* heartbeats a bit more frequently. |
|
70
|
|
|
* - Value more than 1 would mean heartbeat would be send less frequently than expected. This may cause server to |
|
71
|
|
|
* close connection because heartbeats will be delayed. |
|
72
|
|
|
* |
|
73
|
|
|
* @param float $clientBeatFactor |
|
74
|
|
|
* |
|
75
|
|
|
* @return $this |
|
76
|
|
|
*/ |
|
77
|
4 |
|
public function setClientBeatFactor($clientBeatFactor) |
|
78
|
|
|
{ |
|
79
|
4 |
|
$this->clientBeatFactor = $clientBeatFactor; |
|
80
|
|
|
|
|
81
|
4 |
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritdoc} |
|
86
|
|
|
*/ |
|
87
|
21 |
|
public function serverBeat() |
|
88
|
|
|
{ |
|
89
|
21 |
|
$this->lastServerBeat = $this->time(); |
|
90
|
21 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
21 |
|
public function clientBeat() |
|
96
|
|
|
{ |
|
97
|
21 |
|
$this->lastClientBeat = $this->time(); |
|
98
|
21 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
21 |
|
public function shouldSendHeartbeat() |
|
104
|
|
|
{ |
|
105
|
21 |
|
if ($this->heartbeatDelay == 0) { |
|
106
|
1 |
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
20 |
|
return $this->time() - $this->lastClientBeat >= $this->heartbeatDelay * $this->clientBeatFactor; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* {@inheritdoc} |
|
114
|
|
|
*/ |
|
115
|
4 |
|
public function isServerHeartbeatMissing() |
|
116
|
|
|
{ |
|
117
|
4 |
|
if ($this->heartbeatDelay == 0) { |
|
118
|
1 |
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
3 |
|
return $this->time() - $this->lastServerBeat > $this->heartbeatDelay * $this->serverBeatFactor; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return int |
|
126
|
|
|
*/ |
|
127
|
17 |
|
protected function time() |
|
128
|
|
|
{ |
|
129
|
17 |
|
return time(); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|