Repository navigation
Expand file tree
/
Copy pathWorkerLifetimeTimer.php
More file actions
135 lines (119 loc) · 4.51 KB
/
Copy pathWorkerLifetimeTimer.php
File metadata and controls
135 lines (119 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace HttpForPHP;
use Workerman\Worker;
/**
* 连接存活定时器
*/
class WorkerLifetimeTimer
{
protected $heartbeat_time = 0;
protected $max_idle_time = 0;
protected $timerMs = 0; //定时 毫秒
protected $maxTime = 0;
protected $microtime = 0;
protected $isIdle = false;
protected $timerId = 0;
protected static $instance;
/**
* @var callable
*/
public $onHeartbeat = null;
/**
* @return bool
* @var callable
*/
public $onIdle = null;
/**
* @param int $heartbeat_time
* @param int $max_idle_time
* @return WorkerLifetimeTimer
*/
public static function instance($heartbeat_time = 0, $max_idle_time = 0)
{
if (!self::$instance) {
self::$instance = new self($heartbeat_time, $max_idle_time);
}
return self::$instance;
}
/**
* ConnLifetimeTimer constructor.
* @param int $heartbeat_time
* @param int $max_idle_time
*/
public function __construct($heartbeat_time = 0, $max_idle_time = 0)
{
//存活心跳定时 允许最大空闲定时
if ($heartbeat_time == 0) $heartbeat_time = defined('CONN_HEARTBEAT_TIME') ? CONN_HEARTBEAT_TIME : 0;
if ($max_idle_time == 0) $max_idle_time = defined('CONN_MAX_IDLE_TIME') ? CONN_MAX_IDLE_TIME : 0;
$this->heartbeat_time = (int)$heartbeat_time;
$this->max_idle_time = (int)$max_idle_time;
$this->timerMs = $this->minTime($heartbeat_time, $max_idle_time) * 1000;
$this->maxTime = max($heartbeat_time, $max_idle_time);
if ($this->timerMs < 0) $this->timerMs = 0;
}
public function run()
{
if ($this->timerMs <= 0) return; //未配置定时时间
$this->isIdle = false;
$this->microtime = microtime(true);
if (!$this->timerId) { //重启定时
$this->runTimer();
}
}
protected function minTime($heartbeat_time, $max_idle_time)
{
if ($max_idle_time <= 0) {
return $heartbeat_time;
}
if ($heartbeat_time <= 0) {
return $max_idle_time;
}
return min($heartbeat_time, $max_idle_time);
}
protected function runTimer()
{
$this->timerId = SrvBase::$instance->server->tick($this->timerMs, function () {
//if ($this->microtime == 0) return; //该进程没有任何请求
$now_microtime = microtime(true);
$diff = round($now_microtime - $this->microtime);
if ($diff >= $this->maxTime) { //更新触发时间
$this->microtime = $now_microtime;
}
$max_idle_time = $this->max_idle_time;
$onIdle = $this->onIdle;
//存活检查
try {
if ($this->heartbeat_time > 0 && $this->onHeartbeat !== null && $diff >= $this->heartbeat_time) {
call_user_func($this->onHeartbeat);
$msg = date("Y-m-d H:i:s") . ' worker:' . SrvBase::$instance->workerId() . ' heartbeat ';
if (SrvBase::$isConsole) SrvBase::safeEcho($msg . PHP_EOL);
}
} catch (\Exception $e) {
Log::write($e->getMessage(), 'onHeartbeat');
//用于清除定时
$max_idle_time = 1;
if ($onIdle === null) $onIdle = function () {};
} catch (\Error $e) {
Log::write($e->getMessage(), 'onHeartbeat2');
$max_idle_time = 1;
if ($onIdle === null) $onIdle = function () {};
}
//空闲处理
try {
if ($max_idle_time > 0 && $onIdle !== null && $diff >= $max_idle_time) {
call_user_func($onIdle);
$this->isIdle = true;
$msg = ' worker:' . SrvBase::$instance->workerId() . ' onIdle to close, timer:' . $this->timerId . ' to clear';
if (SrvBase::$isConsole) SrvBase::safeEcho(date("Y-m-d H:i:s") . $msg . PHP_EOL);
//else \HttpForPHP\Log::write($msg);
SrvBase::$instance->server->clearTimer($this->timerId); //空闲时清除定时器
$this->timerId = 0;
}
} catch (\Exception $e) {
Log::write($e->getMessage(), 'onIdle');
}
});
$msg = ' worker:' . SrvBase::$instance->workerId() . ' timer ' . $this->timerId . ' start';
if (SrvBase::$isConsole) SrvBase::safeEcho(date("Y-m-d H:i:s") . $msg . PHP_EOL);
}
}