This commit is contained in:
2023-04-04 14:26:11 +08:00
parent faed634616
commit e55eb19768
+49 -36
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,23 +364,30 @@ class PDO implements StopHeartbeatCheck
*/ */
private function newClient(): \PDO private function newClient(): \PDO
{ {
$link = new \PDO('mysql:dbname=' . $this->dbname . ';host=' . $this->cds, $this->username, $this->password, [ $this->group->add();
\PDO::ATTR_EMULATE_PREPARES => false, Coroutine::create(function () {
\PDO::ATTR_CASE => \PDO::CASE_NATURAL, $link = new \PDO('mysql:dbname=' . $this->dbname . ';host=' . $this->cds, $this->username, $this->password, [
\PDO::ATTR_PERSISTENT => true, \PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_TIMEOUT => $this->connect_timeout, \PDO::ATTR_CASE => \PDO::CASE_NATURAL,
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset \PDO::ATTR_PERSISTENT => true,
]); \PDO::ATTR_TIMEOUT => $this->connect_timeout,
$link->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset
$link->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false); ]);
$link->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_EMPTY_STRING); $link->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
foreach ($this->attributes as $key => $attribute) { $link->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false);
$link->setAttribute($key, $attribute); $link->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_EMPTY_STRING);
} foreach ($this->attributes as $key => $attribute) {
if (Db::inTransactionsActive()) { $link->setAttribute($key, $attribute);
$link->beginTransaction(); }
} if (Db::inTransactionsActive()) {
return $link; $link->beginTransaction();
}
$this->pdo = $link;
});
$this->group->done();
return $this->pdo;
} }
} }