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 | * @param int $heartbeatDelay |
||
36 | */ |
||
37 | 27 | public function __construct($heartbeatDelay) |
|
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) |
|
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) |
|
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 21 | public function serverBeat() |
|
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | 21 | public function clientBeat() |
|
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | 21 | public function shouldSendHeartbeat() |
|
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | 4 | public function isServerHeartbeatMissing() |
|
123 | |||
124 | /** |
||
125 | * @return int |
||
126 | */ |
||
127 | 17 | protected function time() |
|
131 | } |
||
132 |