Skip to content

Commit

Permalink
feat: 添加数据库心跳,避免数据库连接超时
Browse files Browse the repository at this point in the history
  • Loading branch information
lk3407105 committed Sep 25, 2024
1 parent a518759 commit 9658229
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Webworker.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use think\webworker\support\think\App;
use think\webworker\support\workerman\Response;
use Workerman\Worker;
use Workerman\Timer;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Throwable;

class Webworker
{
Expand Down Expand Up @@ -173,6 +175,30 @@ public function onWorkerStart(Worker $worker): void
$this->app = new App();
// 初始化
$this->app->initialize();

$this->db_hart();
}

/**
* 避免数据库连接超时
* @access public
* @return void
*/
public function db_heart(): void
{
$db = $this->app->db;
Timer::add(55, function() use($db) {
$intances = $db->getInstance();
if (count($intances) <= 0) {
return;
}

foreach($intances as $connection) {
try {
$connection->query('select 1');
} catch (Throwable $e) {}
}
});
}

/**
Expand Down

0 comments on commit 9658229

Please sign in to comment.