正在显示
3 个修改的文件
包含
48 行增加
和
5 行删除
| @@ -21,12 +21,27 @@ function start(){ | @@ -21,12 +21,27 @@ function start(){ | ||
| 21 | $pm->add(function (Process\Pool $pool, int $workerId){ | 21 | $pm->add(function (Process\Pool $pool, int $workerId){ |
| 22 | _echo("定时进程({$workerId})启动成功"); | 22 | _echo("定时进程({$workerId})启动成功"); |
| 23 | 23 | ||
| 24 | + // 每秒执行 | ||
| 24 | \Swoole\Timer::tick(1000,function() use(&$pool){ | 25 | \Swoole\Timer::tick(1000,function() use(&$pool){ |
| 25 | 26 | ||
| 26 | if(redis()->getOriginData('email_sync_stop_num') >= WORKER_NUM+1 ){ | 27 | if(redis()->getOriginData('email_sync_stop_num') >= WORKER_NUM+1 ){ |
| 27 | $pool->shutdown(); | 28 | $pool->shutdown(); |
| 28 | } | 29 | } |
| 29 | 30 | ||
| 31 | + // 每秒检查一次是否空闲redis | ||
| 32 | + foreach (\Lib\RedisPool::$instance as $redis){ | ||
| 33 | + if(time()-2 > $redis->lastTimer ){ | ||
| 34 | + $redis->close(); | ||
| 35 | + } | ||
| 36 | + } | ||
| 37 | + // 空闲超过10秒的db链接,关闭 | ||
| 38 | + foreach (\Lib\DbPool::$instance as $db){ | ||
| 39 | + if(time()-10 > $db->lastTimer ){ | ||
| 40 | + $db->close(); | ||
| 41 | + } | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + | ||
| 30 | }); | 45 | }); |
| 31 | 46 | ||
| 32 | 47 |
| @@ -23,6 +23,13 @@ class DbPool { | @@ -23,6 +23,13 @@ class DbPool { | ||
| 23 | private $client; | 23 | private $client; |
| 24 | 24 | ||
| 25 | /** | 25 | /** |
| 26 | + * @var | ||
| 27 | + */ | ||
| 28 | + private $cid; | ||
| 29 | + | ||
| 30 | + public $lastTimer; | ||
| 31 | + | ||
| 32 | + /** | ||
| 26 | * @return mixed | 33 | * @return mixed |
| 27 | * @author:dc | 34 | * @author:dc |
| 28 | * @time 2023/2/13 9:12 | 35 | * @time 2023/2/13 9:12 |
| @@ -31,12 +38,14 @@ class DbPool { | @@ -31,12 +38,14 @@ class DbPool { | ||
| 31 | if(!$this->client){ | 38 | if(!$this->client){ |
| 32 | $this->connect(); | 39 | $this->connect(); |
| 33 | } | 40 | } |
| 41 | + $this->lastTimer = time(); | ||
| 34 | return $this->client; | 42 | return $this->client; |
| 35 | } | 43 | } |
| 36 | 44 | ||
| 37 | 45 | ||
| 38 | - public function __construct() | 46 | + public function __construct($cid) |
| 39 | { | 47 | { |
| 48 | + $this->cid = $cid; | ||
| 40 | $this->connect(); | 49 | $this->connect(); |
| 41 | } | 50 | } |
| 42 | 51 | ||
| @@ -46,6 +55,7 @@ class DbPool { | @@ -46,6 +55,7 @@ class DbPool { | ||
| 46 | * @time 2023/3/23 9:14 | 55 | * @time 2023/3/23 9:14 |
| 47 | */ | 56 | */ |
| 48 | public function connect(){ | 57 | public function connect(){ |
| 58 | + $this->lastTimer = time(); | ||
| 49 | try { | 59 | try { |
| 50 | $this->client = new \PDO( | 60 | $this->client = new \PDO( |
| 51 | 'mysql:charset=utf8mb4;dbname='.DB_DATABASE.';host='.DB_HOST.';port='.DB_PORT, | 61 | 'mysql:charset=utf8mb4;dbname='.DB_DATABASE.';host='.DB_HOST.';port='.DB_PORT, |
| @@ -318,7 +328,7 @@ class DbPool { | @@ -318,7 +328,7 @@ class DbPool { | ||
| 318 | */ | 328 | */ |
| 319 | public static function instance($cid=0){ | 329 | public static function instance($cid=0){ |
| 320 | if(empty(static::$instance[$cid])){ | 330 | if(empty(static::$instance[$cid])){ |
| 321 | - static::$instance[$cid] = new DbPool(); | 331 | + static::$instance[$cid] = new DbPool($cid); |
| 322 | } | 332 | } |
| 323 | return static::$instance[$cid]; | 333 | return static::$instance[$cid]; |
| 324 | } | 334 | } |
| @@ -334,6 +344,7 @@ class DbPool { | @@ -334,6 +344,7 @@ class DbPool { | ||
| 334 | 344 | ||
| 335 | public function close(){ | 345 | public function close(){ |
| 336 | $this->client = null; | 346 | $this->client = null; |
| 347 | + unset(static::$instance[$this->cid]); | ||
| 337 | } | 348 | } |
| 338 | 349 | ||
| 339 | 350 |
| @@ -20,9 +20,21 @@ class RedisPool { | @@ -20,9 +20,21 @@ class RedisPool { | ||
| 20 | */ | 20 | */ |
| 21 | private $client; | 21 | private $client; |
| 22 | 22 | ||
| 23 | + /** | ||
| 24 | + * @var | ||
| 25 | + */ | ||
| 26 | + private $cid; | ||
| 27 | + | ||
| 28 | + /** | ||
| 29 | + * 最后执行的时间 | ||
| 30 | + * @var int | ||
| 31 | + */ | ||
| 32 | + public $lastTimer; | ||
| 33 | + | ||
| 23 | 34 | ||
| 24 | - public function __construct() | 35 | + public function __construct($cid) |
| 25 | { | 36 | { |
| 37 | + $this->cid = $cid; | ||
| 26 | $this->conn(); | 38 | $this->conn(); |
| 27 | } | 39 | } |
| 28 | 40 | ||
| @@ -32,9 +44,10 @@ class RedisPool { | @@ -32,9 +44,10 @@ class RedisPool { | ||
| 32 | * @time 2023/4/12 15:10 | 44 | * @time 2023/4/12 15:10 |
| 33 | */ | 45 | */ |
| 34 | private function conn(){ | 46 | private function conn(){ |
| 47 | + $this->lastTimer = time(); | ||
| 35 | $this->client = new \Redis(); | 48 | $this->client = new \Redis(); |
| 36 | 49 | ||
| 37 | - $this->client->pconnect(REDIS_HOST,REDIS_PORT,5); | 50 | + $this->client->pconnect(REDIS_HOST,REDIS_PORT,1); |
| 38 | // 密码 | 51 | // 密码 |
| 39 | REDIS_PASSWORD && $this->client->auth(REDIS_PASSWORD); | 52 | REDIS_PASSWORD && $this->client->auth(REDIS_PASSWORD); |
| 40 | // 用库4 | 53 | // 用库4 |
| @@ -251,6 +264,7 @@ class RedisPool { | @@ -251,6 +264,7 @@ class RedisPool { | ||
| 251 | }catch (\Throwable $e){ | 264 | }catch (\Throwable $e){ |
| 252 | $this->conn(); | 265 | $this->conn(); |
| 253 | } | 266 | } |
| 267 | + $this->lastTimer = time(); | ||
| 254 | return $this->client; | 268 | return $this->client; |
| 255 | } | 269 | } |
| 256 | 270 | ||
| @@ -269,8 +283,9 @@ class RedisPool { | @@ -269,8 +283,9 @@ class RedisPool { | ||
| 269 | * @time 2023/2/13 9:38 | 283 | * @time 2023/2/13 9:38 |
| 270 | */ | 284 | */ |
| 271 | public static function instance($cid){ | 285 | public static function instance($cid){ |
| 286 | + | ||
| 272 | if(empty(static::$instance[$cid])){ | 287 | if(empty(static::$instance[$cid])){ |
| 273 | - static::$instance[$cid] = new \Lib\RedisPool(); | 288 | + static::$instance[$cid] = new \Lib\RedisPool($cid); |
| 274 | } | 289 | } |
| 275 | 290 | ||
| 276 | return static::$instance[$cid]; | 291 | return static::$instance[$cid]; |
| @@ -291,7 +306,9 @@ class RedisPool { | @@ -291,7 +306,9 @@ class RedisPool { | ||
| 291 | 306 | ||
| 292 | } | 307 | } |
| 293 | 308 | ||
| 309 | + unset(static::$instance[$this->cid]); | ||
| 294 | $this->client = null; | 310 | $this->client = null; |
| 311 | + | ||
| 295 | } | 312 | } |
| 296 | 313 | ||
| 297 | 314 |
-
请 注册 或 登录 后发表评论