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