This commit is contained in:
2021-08-16 18:18:35 +08:00
parent 37a43ef66e
commit 9d78dc9e48
+9 -1
View File
@@ -4,7 +4,9 @@ namespace Database\Mysql;
use Exception; use Exception;
use HttpServer\Http\Context; use HttpServer\Http\Context;
use Kiri\Events\EventProvider;
use PDOStatement; use PDOStatement;
use Server\Events\OnWorkerExit;
use Swoole\Timer; use Swoole\Timer;
class PDO class PDO
@@ -19,6 +21,8 @@ class PDO
private int $_transaction = 0; private int $_transaction = 0;
private EventProvider $eventProvider;
private int $_timer = -1; private int $_timer = -1;
private int $_last = 0; private int $_last = 0;
@@ -35,6 +39,9 @@ class PDO
public string $username, public string $password, public string $chatset = 'utf8mb4') public string $username, public string $password, public string $chatset = 'utf8mb4')
{ {
$this->heartbeat_check(); $this->heartbeat_check();
$this->eventProvider = di(EventProvider::class);
$this->eventProvider->on(OnWorkerExit::class, [$this, 'stopHeartbeatCheck']);
} }
@@ -57,9 +64,11 @@ class PDO
try { try {
if (time() - $this->_last > 10 * 60) { if (time() - $this->_last > 10 * 60) {
$this->stopHeartbeatCheck(); $this->stopHeartbeatCheck();
$this->pdo = null;
} }
if (!$this->_pdo()->getAttribute(\PDO::ATTR_SERVER_INFO)) { if (!$this->_pdo()->getAttribute(\PDO::ATTR_SERVER_INFO)) {
$this->stopHeartbeatCheck(); $this->stopHeartbeatCheck();
$this->pdo = null;
} }
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
error($throwable); error($throwable);
@@ -74,7 +83,6 @@ class PDO
*/ */
public function stopHeartbeatCheck(): void public function stopHeartbeatCheck(): void
{ {
$this->pdo = null;
if (Context::inCoroutine()) { if (Context::inCoroutine()) {
Timer::clear($this->_timer); Timer::clear($this->_timer);
} }