作者 邓超

x

... ... @@ -21,12 +21,27 @@ function start(){
$pm->add(function (Process\Pool $pool, int $workerId){
_echo("定时进程({$workerId})启动成功");
// 每秒执行
\Swoole\Timer::tick(1000,function() use(&$pool){
if(redis()->getOriginData('email_sync_stop_num') >= WORKER_NUM+1 ){
$pool->shutdown();
}
// 每秒检查一次是否空闲redis
foreach (\Lib\RedisPool::$instance as $redis){
if(time()-2 > $redis->lastTimer ){
$redis->close();
}
}
// 空闲超过10秒的db链接,关闭
foreach (\Lib\DbPool::$instance as $db){
if(time()-10 > $db->lastTimer ){
$db->close();
}
}
});
... ...
... ... @@ -23,6 +23,13 @@ class DbPool {
private $client;
/**
* @var
*/
private $cid;
public $lastTimer;
/**
* @return mixed
* @author:dc
* @time 2023/2/13 9:12
... ... @@ -31,12 +38,14 @@ class DbPool {
if(!$this->client){
$this->connect();
}
$this->lastTimer = time();
return $this->client;
}
public function __construct()
public function __construct($cid)
{
$this->cid = $cid;
$this->connect();
}
... ... @@ -46,6 +55,7 @@ class DbPool {
* @time 2023/3/23 9:14
*/
public function connect(){
$this->lastTimer = time();
try {
$this->client = new \PDO(
'mysql:charset=utf8mb4;dbname='.DB_DATABASE.';host='.DB_HOST.';port='.DB_PORT,
... ... @@ -318,7 +328,7 @@ class DbPool {
*/
public static function instance($cid=0){
if(empty(static::$instance[$cid])){
static::$instance[$cid] = new DbPool();
static::$instance[$cid] = new DbPool($cid);
}
return static::$instance[$cid];
}
... ... @@ -334,6 +344,7 @@ class DbPool {
public function close(){
$this->client = null;
unset(static::$instance[$this->cid]);
}
... ...
... ... @@ -20,9 +20,21 @@ class RedisPool {
*/
private $client;
/**
* @var
*/
private $cid;
/**
* 最后执行的时间
* @var int
*/
public $lastTimer;
public function __construct()
public function __construct($cid)
{
$this->cid = $cid;
$this->conn();
}
... ... @@ -32,9 +44,10 @@ class RedisPool {
* @time 2023/4/12 15:10
*/
private function conn(){
$this->lastTimer = time();
$this->client = new \Redis();
$this->client->pconnect(REDIS_HOST,REDIS_PORT,5);
$this->client->pconnect(REDIS_HOST,REDIS_PORT,1);
// 密码
REDIS_PASSWORD && $this->client->auth(REDIS_PASSWORD);
// 用库4
... ... @@ -251,6 +264,7 @@ class RedisPool {
}catch (\Throwable $e){
$this->conn();
}
$this->lastTimer = time();
return $this->client;
}
... ... @@ -269,8 +283,9 @@ class RedisPool {
* @time 2023/2/13 9:38
*/
public static function instance($cid){
if(empty(static::$instance[$cid])){
static::$instance[$cid] = new \Lib\RedisPool();
static::$instance[$cid] = new \Lib\RedisPool($cid);
}
return static::$instance[$cid];
... ... @@ -291,7 +306,9 @@ class RedisPool {
}
unset(static::$instance[$this->cid]);
$this->client = null;
}
... ...