10, 'min' => 1]; private int $storey = 0; protected int $timerId = -1; /** * @var bool * enable database cache */ public bool $enableCache = false; /** * @var string */ public string $cacheDriver = 'redis'; /** * @var array */ public array $attributes = []; /** * @var Schema|null */ private ?Schema $_schema = null; /** * @var StdoutLogger */ #[Container(LoggerInterface::class)] public StdoutLogger $logger; /** * @param Pool $connections */ public function __construct(public Pool $connections) { parent::__construct(); } /** * @return void * @throws Exception */ public function init(): void { $eventProvider = Kiri::getDi()->get(EventProvider::class); $eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0); $eventProvider->on(Rollback::class, [$this, 'rollback'], 0); $eventProvider->on(Commit::class, [$this, 'commit'], 0); $eventProvider->on(OnAfterRequest::class, [$this, 'clear']); $eventProvider->on(OnWorkerExit::class, [$this, 'disconnect']); $eventProvider->on(OnWorkerStart::class, [$this, 'tick']); $eventProvider->on(OnTaskerStart::class, [$this, 'tick']); } /** * @return void */ public function tick(): void { $this->timerId = Timer::tick($this->tick_time, fn() => $this->checkClientHealth($this->pool())); } /** * @param Pool $pool * @return void * @throws Exception */ protected function checkClientHealth(Pool $pool): void { $pool->flush($this->cds, $this->pool['min'] ?? 1); $length = $pool->size($this->cds); for ($i = 0; $i < $length; $i++) { try { if (($client = $this->validator($pool)) === false) { break; } $pool->push($this->cds, $client); } catch (\Throwable $exception) { if (!str_contains($exception->getMessage(), 'Client timeout.')) { $this->logger->error(throwable($exception), [$this->cds]); } } } } /** * @param Pool $pool * @return PDO|bool * @throws Exception */ protected function validator(Pool $pool): PDO|bool { /** @var $client PDO */ if (($client = $pool->get($this->cds)) === false) { return false; } if ($client->query('select 1') === false) { throw new Exception($client->errorInfo()[1]); } return $client; } /** * @return mixed * @throws ReflectionException * @throws NotFindClassException * @throws Exception */ public function getSchema(): Schema { if ($this->_schema === null) { $this->_schema = Kiri::createObject([ 'class' => Schema::class, 'db' => $this ]); } return $this->_schema; } /** * @return PDO * @throws Exception */ public function getConnection(): PDO { if (!$this->inTransaction()) { return $this->getNormalClientHealth(); } else { return $this->getTransactionClient(); } } /** * @return PDO * @throws Exception */ protected function getNormalClientHealth(): PDO { $data = $this->pool()->get($this->cds, $this->waite_time); if ($data === false) { throw new Exception('Client Waite timeout.'); } return $data; } /** * @return $this * @throws Exception */ public function beginTransaction(): static { if ($this->storey == 0) { /** @var PDO $pdo */ $pdo = Context::get($this->cds); if ($pdo == null) { $pdo = $this->getTransactionClient(); } if (!$pdo->inTransaction()) { $pdo->beginTransaction(); } } $this->storey++; return $this; } /** * @return PDO * @throws Exception */ public function getTransactionClient(): PDO { $pdo = Context::get($this->cds); if ($pdo === null) { $pdo = $this->getNormalClientHealth(); if (!$pdo->inTransaction()) { $pdo->beginTransaction(); } Context::set($this->cds, $pdo); } return $pdo; } /** * @return bool * @throws Exception */ public function inTransaction(): bool { return $this->storey > 0; } /** * @throws Exception * 事务回滚 */ public function rollback(): void { $this->storey--; if ($this->storey == 0) { /** @var PDO $pdo */ $pdo = Context::get($this->cds); if ($pdo === null) { throw new Exception('Failed to rollback transaction: connection was exists.'); } if ($this->inTransaction()) { $pdo->rollback(); } $this->release($pdo); } } /** * @throws Exception * 事务提交 */ public function commit(): void { $this->storey--; if ($this->storey == 0) { $pdo = Context::get($this->cds); if ($pdo === null) { throw new Exception('Failed to commit transaction: connection was exists.'); } if ($this->inTransaction()) { $pdo->commit(); } $this->release($pdo); } } /** * @return void * @throws Exception */ public function clear(): void { /** @var PDO $pdo */ $pdo = Context::get($this->cds); if ($pdo === null) { return; } if ($this->inTransaction()) { $pdo->rollback(); } $this->release($pdo); } /** * @param null $sql * @param array $attributes * @return Command * @throws Exception */ public function createCommand($sql = null, array $attributes = []): Command { $command = new Command(['connection' => $this, 'sql' => $sql]); return $command->bindValues($attributes); } /** * 回收链接 * @throws */ public function release(PDO $pdo): void { if (!$this->inTransaction()) { $this->pool()->push($this->cds, $pdo); } } /** * * 回收链接 * @throws */ public function clear_connection(): void { $this->pool()->flush($this->cds, 0); } /** * @throws Exception */ public function disconnect(): void { if ($this->timerId > -1) { Timer::clear($this->timerId); } $this->pool()->close($this->cds); } /** * @return PDO */ public function newConnect(): PDO { $pdo = new PDO('mysql:dbname=' . $this->database . ';host=' . $this->cds, $this->username, $this->password, [ PDO::ATTR_CASE => PDO::CASE_NATURAL, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, PDO::ATTR_STRINGIFY_FETCHES => false, PDO::ATTR_EMULATE_PREPARES => true, PDO::ATTR_TIMEOUT => $this->timeout, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset ]); array_walk($this->attributes, function ($value, $key) use ($pdo) { $pdo->setAttribute($key, $value); }); return $pdo; } /** * @return Pool */ protected function pool(): Pool { if (!$this->connections->hasChannel($this->cds)) { $this->connections->created($this->cds, $this->pool['max'] ?? 1, [$this, 'newConnect']); } return $this->connections; } }