This commit is contained in:
2023-04-04 14:26:11 +08:00
parent faed634616
commit e55eb19768
+33 -20
View File
@@ -2,6 +2,7 @@
namespace Database\Mysql; namespace Database\Mysql;
use Co\WaitGroup;
use Database\Db; use Database\Db;
use Exception; use Exception;
use Kiri; use Kiri;
@@ -11,6 +12,7 @@ use Kiri\Server\Events\OnWorkerExit;
use PDOStatement; use PDOStatement;
use Kiri\Server\WorkerStatus; use Kiri\Server\WorkerStatus;
use Kiri\Server\Abstracts\StatusEnum; use Kiri\Server\Abstracts\StatusEnum;
use Swoole\Coroutine;
use Swoole\Timer; use Swoole\Timer;
/** /**
@@ -26,8 +28,6 @@ class PDO implements StopHeartbeatCheck
private int $_transaction = 0; private int $_transaction = 0;
private int $_last = 0;
public string $dbname; public string $dbname;
public string $cds; public string $cds;
public string $username; public string $username;
@@ -36,6 +36,10 @@ class PDO implements StopHeartbeatCheck
public int $connect_timeout; public int $connect_timeout;
public int $read_timeout; public int $read_timeout;
/** @var WaitGroup */
private WaitGroup $group;
private int $_timerId = -1; private int $_timerId = -1;
@@ -67,6 +71,8 @@ class PDO implements StopHeartbeatCheck
$eventProvider = Kiri::getDi()->get(EventProvider::class); $eventProvider = Kiri::getDi()->get(EventProvider::class);
$eventProvider->on(OnWorkerExit::class, [$this, 'onWorkerExit']); $eventProvider->on(OnWorkerExit::class, [$this, 'onWorkerExit']);
$this->_timerId = Timer::tick(60000, [$this, 'check']); $this->_timerId = Timer::tick(60000, [$this, 'check']);
$this->group = new WaitGroup();
} }
@@ -253,23 +259,23 @@ class PDO implements StopHeartbeatCheck
public function check(): bool public function check(): bool
{ {
return true; return true;
try { // try {
if ($this->_last == 0) $this->_last = time(); // if ($this->_last == 0) $this->_last = time();
if (time() - $this->_last >= 600) { // if (time() - $this->_last >= 600) {
return $result = false; // return $result = false;
} else if (!($this->pdo instanceof \PDO)) { // } else if (!($this->pdo instanceof \PDO)) {
return $result = false; // return $result = false;
} // }
$this->_pdo()->getAttribute(\PDO::ATTR_SERVER_INFO); // $this->_pdo()->getAttribute(\PDO::ATTR_SERVER_INFO);
$result = true; // $result = true;
} catch (\Throwable $throwable) { // } catch (\Throwable $throwable) {
if (!str_contains($throwable->getMessage(), 'Idle dis')) { // if (!str_contains($throwable->getMessage(), 'Idle dis')) {
Kiri::getLogger()->error($throwable->getMessage()); // Kiri::getLogger()->error($throwable->getMessage());
} // }
$result = false; // $result = false;
} finally { // } finally {
return $this->afterCheck($result); // return $this->afterCheck($result);
} // }
} }
@@ -358,6 +364,8 @@ class PDO implements StopHeartbeatCheck
*/ */
private function newClient(): \PDO private function newClient(): \PDO
{ {
$this->group->add();
Coroutine::create(function () {
$link = new \PDO('mysql:dbname=' . $this->dbname . ';host=' . $this->cds, $this->username, $this->password, [ $link = new \PDO('mysql:dbname=' . $this->dbname . ';host=' . $this->cds, $this->username, $this->password, [
\PDO::ATTR_EMULATE_PREPARES => false, \PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_CASE => \PDO::CASE_NATURAL, \PDO::ATTR_CASE => \PDO::CASE_NATURAL,
@@ -374,7 +382,12 @@ class PDO implements StopHeartbeatCheck
if (Db::inTransactionsActive()) { if (Db::inTransactionsActive()) {
$link->beginTransaction(); $link->beginTransaction();
} }
return $link;
$this->pdo = $link;
});
$this->group->done();
return $this->pdo;
} }
} }