Files
kiri-databases/Connection.php
T

387 lines
9.3 KiB
PHP
Raw Permalink Normal View History

2022-01-09 03:49:51 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 14:09
*/
declare(strict_types=1);
namespace Database;
use Database\Affair\BeginTransaction;
use Database\Affair\Commit;
use Database\Affair\Rollback;
use Exception;
2022-01-14 15:52:38 +08:00
use Kiri;
2023-08-25 09:37:58 +08:00
use Kiri\Server\Events\OnWorkerExit;
2022-01-09 03:49:51 +08:00
use Kiri\Abstracts\Component;
2022-12-12 17:31:12 +08:00
use Kiri\Di\Context;
2023-04-06 22:39:31 +08:00
use Kiri\Pool\Pool;
2022-03-03 18:30:59 +08:00
use Kiri\Events\EventProvider;
2023-08-16 12:36:45 +08:00
use Kiri\Error\StdoutLogger;
2023-08-16 12:00:10 +08:00
use Psr\Log\LoggerInterface;
2023-08-29 21:35:19 +08:00
use Kiri\Server\Events\OnWorkerStart;
use Kiri\Server\Events\OnTaskerStart;
2023-08-16 01:01:47 +08:00
use Kiri\Server\Events\OnAfterRequest;
2023-08-16 12:15:59 +08:00
use Kiri\Di\Inject\Container;
2023-08-29 21:35:19 +08:00
use Swoole\Timer;
2024-05-01 02:02:58 +08:00
use Database\Base\PDO;
//use PDO;
2022-01-09 03:49:51 +08:00
/**
* Class Connection
* @package Database
*/
class Connection extends Component
{
2023-05-26 09:20:29 +08:00
2024-04-16 17:24:22 +08:00
public string $id = 'db';
public string $cds = '';
public string $password = '';
public string $username = '';
public string $charset = 'utf-8';
public string $tablePrefix = '';
public string $database = '';
public int $timeout = 30;
public int $waite_time = 3;
public int $tick_time = 60;
public int $idle_count = 3;
public int $idle_time = 60;
public array $pool = ['max' => 10, 'min' => 1];
private int $storey = 0;
protected int $timerId = -1;
public bool $enableCache = false;
public string $cacheDriver = 'redis';
public array $attributes = [];
public array $slave = [];
protected ?\Closure $_println = null;
2023-05-26 09:20:29 +08:00
2023-08-16 12:15:59 +08:00
/**
2023-08-16 12:36:45 +08:00
* @var StdoutLogger
2023-08-16 12:15:59 +08:00
*/
#[Container(LoggerInterface::class)]
2023-08-16 12:36:45 +08:00
public StdoutLogger $logger;
2023-08-16 12:15:59 +08:00
2024-11-18 16:35:37 +08:00
/**
* @var EventProvider
*/
#[Container(EventProvider::class)]
public EventProvider $eventProvider;
2023-08-16 00:39:55 +08:00
/**
* @param Pool $connections
*/
2023-08-16 12:15:59 +08:00
public function __construct(public Pool $connections)
2023-08-16 00:39:55 +08:00
{
parent::__construct();
2024-01-10 17:43:10 +08:00
$this->_println = \config('databases.logger', null);
}
/**
2024-10-23 14:10:14 +08:00
* @param float $startTime
* @param float $endTime
2024-01-10 17:43:10 +08:00
* @param string $sql
* @param array $params
* @return void
*/
2024-10-23 14:10:14 +08:00
public function println(float $startTime, float $endTime, string $sql, array $params = []): void
2024-01-10 17:43:10 +08:00
{
if (is_callable($this->_println)) {
2024-10-23 14:10:14 +08:00
call_user_func($this->_println, $startTime, $endTime, $sql, $params);
2024-01-10 17:43:10 +08:00
}
2023-08-16 00:39:55 +08:00
}
2023-05-26 09:20:29 +08:00
/**
* @return void
2023-12-12 17:46:25 +08:00
* @throws
2023-05-26 09:20:29 +08:00
*/
public function init(): void
{
2024-11-18 16:35:37 +08:00
$this->eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0);
$this->eventProvider->on(Rollback::class, [$this, 'rollback'], 0);
$this->eventProvider->on(Commit::class, [$this, 'commit'], 0);
$this->eventProvider->on(OnAfterRequest::class, [$this, 'clear']);
$this->eventProvider->on(OnWorkerExit::class, [$this, 'disconnect']);
$this->eventProvider->on(OnWorkerStart::class, [$this, 'tick']);
$this->eventProvider->on(OnTaskerStart::class, [$this, 'tick']);
2023-08-29 21:35:19 +08:00
}
/**
* @return void
*/
public function tick(): void
{
2024-10-23 14:10:14 +08:00
$this->timerId = Timer::tick($this->tick_time, fn () => $this->checkClientHealth($this->pool()));
2023-08-29 21:35:19 +08:00
}
/**
* @param Pool $pool
* @return void
2023-12-12 17:46:25 +08:00
* @throws
2023-08-29 21:35:19 +08:00
*/
protected function checkClientHealth(Pool $pool): void
{
2024-05-01 02:06:14 +08:00
$pool->flush($this->getName(), $this->pool['min'] ?? 1);
$length = $pool->size($this->getName());
2023-08-29 21:35:19 +08:00
for ($i = 0; $i < $length; $i++) {
try {
2023-08-29 22:13:37 +08:00
if (($client = $this->validator($pool)) === false) {
2023-08-29 21:35:19 +08:00
break;
}
2024-05-01 02:06:14 +08:00
$pool->push($this->getName(), $client);
2023-08-29 21:35:19 +08:00
} catch (\Throwable $exception) {
2023-08-29 21:54:02 +08:00
if (!str_contains($exception->getMessage(), 'Client timeout.')) {
2023-08-29 22:13:37 +08:00
$this->logger->error(throwable($exception), [$this->cds]);
2023-08-29 21:54:02 +08:00
}
2023-08-29 21:35:19 +08:00
}
}
2023-05-26 09:20:29 +08:00
}
2024-05-01 02:06:14 +08:00
/**
* @return string
*/
private function getName(): string
{
return 'mysql.' . $this->cds;
}
2023-08-29 21:54:02 +08:00
/**
* @param Pool $pool
* @return PDO|bool
2023-12-12 17:46:25 +08:00
* @throws
2023-08-29 21:54:02 +08:00
*/
2023-08-29 22:13:37 +08:00
protected function validator(Pool $pool): PDO|bool
2023-08-29 21:54:02 +08:00
{
2023-08-29 22:20:30 +08:00
/** @var $client PDO */
2024-05-01 02:06:14 +08:00
if (($client = $pool->get($this->getName())) === false) {
2023-08-29 21:54:02 +08:00
return false;
}
if ($client->query('select 1') === false) {
throw new Exception($client->errorInfo()[1]);
}
return $client;
}
2023-05-26 09:20:29 +08:00
/**
* @return PDO
2023-12-12 17:46:25 +08:00
* @throws
2023-05-26 09:20:29 +08:00
*/
public function getConnection(): PDO
{
2023-08-16 00:55:05 +08:00
if (!$this->inTransaction()) {
2023-08-16 12:27:03 +08:00
return $this->getNormalClientHealth();
2023-08-16 00:55:05 +08:00
} else {
return $this->getTransactionClient();
}
2023-05-26 09:20:29 +08:00
}
2023-08-16 12:00:10 +08:00
/**
* @return PDO
2023-12-12 17:46:25 +08:00
* @throws
2023-08-16 12:00:10 +08:00
*/
2023-08-16 12:27:03 +08:00
protected function getNormalClientHealth(): PDO
2023-08-16 12:00:10 +08:00
{
2024-05-01 02:06:14 +08:00
$data = $this->pool()->get($this->getName(), $this->waite_time);
2023-08-29 22:13:37 +08:00
if ($data === false) {
throw new Exception('Client Waite timeout.');
2023-08-16 12:19:47 +08:00
}
2023-08-29 22:20:30 +08:00
return $data;
2023-08-16 12:19:47 +08:00
}
2023-08-24 11:46:27 +08:00
/**
* @return $this
2023-12-12 17:46:25 +08:00
* @throws
2023-08-24 11:46:27 +08:00
*/
public function beginTransaction(): static
{
2023-12-12 17:46:25 +08:00
if ($this->storey < 0) {
2023-12-12 17:40:51 +08:00
$this->storey = 0;
2023-08-24 11:46:27 +08:00
}
$this->storey++;
return $this;
}
2023-05-26 09:20:29 +08:00
/**
* @return PDO
2023-12-12 17:46:25 +08:00
* @throws
2023-05-26 09:20:29 +08:00
*/
public function getTransactionClient(): PDO
{
$pdo = Context::get($this->cds);
if ($pdo === null) {
2023-12-12 17:21:25 +08:00
/** @var PDO $pdo */
$pdo = Context::set($this->cds, $this->getNormalClientHealth());
2023-12-12 17:40:51 +08:00
}
if ($this->storey > 0 && !$pdo->inTransaction()) {
$pdo->beginTransaction();
2023-05-26 09:20:29 +08:00
}
return $pdo;
}
2023-08-24 12:10:08 +08:00
2023-05-26 09:20:29 +08:00
/**
* @return bool
2023-12-12 17:46:25 +08:00
* @throws
2023-05-26 09:20:29 +08:00
*/
public function inTransaction(): bool
{
return $this->storey > 0;
}
2023-08-24 12:10:08 +08:00
2023-05-26 09:20:29 +08:00
/**
2023-12-12 17:46:25 +08:00
* @throws
2023-05-26 09:20:29 +08:00
* 事务回滚
*/
public function rollback(): void
{
$this->storey--;
if ($this->storey == 0) {
2023-12-12 17:47:40 +08:00
if (!Context::exists($this->cds)) {
return;
}
2023-11-23 15:27:57 +08:00
$pdo = $this->getTransactionClient();
2023-11-23 15:30:57 +08:00
if ($pdo->inTransaction()) {
$pdo->rollback();
}
2023-08-29 22:20:30 +08:00
$this->release($pdo);
2023-05-26 09:20:29 +08:00
}
}
/**
2023-12-12 17:46:25 +08:00
* @throws
2023-05-26 09:20:29 +08:00
* 事务提交
*/
public function commit(): void
{
$this->storey--;
if ($this->storey == 0) {
2023-12-12 17:47:40 +08:00
if (!Context::exists($this->cds)) {
return;
}
2023-11-23 15:27:57 +08:00
$pdo = $this->getTransactionClient();
2023-11-23 15:30:57 +08:00
if ($pdo->inTransaction()) {
$pdo->commit();
}
2023-08-29 22:20:30 +08:00
$this->release($pdo);
2023-05-26 09:20:29 +08:00
}
}
2023-08-16 00:57:33 +08:00
/**
* @return void
2023-12-12 17:46:25 +08:00
* @throws
2023-08-16 00:57:33 +08:00
*/
public function clear(): void
{
/** @var PDO $pdo */
$pdo = Context::get($this->cds);
if ($pdo === null) {
return;
}
2023-08-24 14:13:22 +08:00
if ($this->inTransaction()) {
2023-08-16 00:57:33 +08:00
$pdo->rollback();
}
2023-08-29 22:20:30 +08:00
$this->release($pdo);
2023-08-16 00:57:33 +08:00
}
2023-05-26 09:20:29 +08:00
/**
2023-12-18 18:11:58 +08:00
* @param string $sql
2023-05-26 09:20:29 +08:00
* @param array $attributes
* @return Command
2023-12-12 15:35:35 +08:00
* @throws
2023-05-26 09:20:29 +08:00
*/
2023-12-18 18:11:58 +08:00
public function createCommand(string $sql, array $attributes = []): Command
2023-05-26 09:20:29 +08:00
{
2023-11-14 15:01:30 +08:00
return (new Command(['connection' => $this, 'sql' => $sql]))->bindValues($attributes);
2023-05-26 09:20:29 +08:00
}
/**
* 回收链接
* @throws
*/
2023-08-29 22:20:30 +08:00
public function release(PDO $pdo): void
2023-05-26 09:20:29 +08:00
{
2023-08-24 14:15:00 +08:00
if (!$this->inTransaction()) {
2024-05-01 02:06:14 +08:00
$this->pool()->push($this->getName(), $pdo);
2023-08-16 00:55:05 +08:00
}
2023-05-26 09:20:29 +08:00
}
/**
*
* 回收链接
* @throws
*/
public function clear_connection(): void
{
2024-05-01 02:06:14 +08:00
$this->pool()->flush($this->getName(), 0);
2023-05-26 09:20:29 +08:00
}
/**
2023-12-12 17:46:25 +08:00
* @throws
2023-05-26 09:20:29 +08:00
*/
public function disconnect(): void
{
2023-08-29 21:46:05 +08:00
if ($this->timerId > -1) {
Timer::clear($this->timerId);
}
2024-05-01 02:06:14 +08:00
$this->pool()->close($this->getName());
2023-05-26 09:20:29 +08:00
}
2023-08-14 14:14:40 +08:00
/**
2023-08-29 22:21:42 +08:00
* @return PDO
2023-08-14 14:14:40 +08:00
*/
2024-05-01 01:54:38 +08:00
public function newConnect(): \PDO
2023-08-14 14:14:40 +08:00
{
2024-05-01 02:02:58 +08:00
$pdo = new PDO($this->database, $this->cds, $this->username, $this->password, [
2024-10-24 10:53:00 +08:00
\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
2023-11-24 10:38:18 +08:00
]);
2023-09-01 10:56:06 +08:00
foreach ($this->attributes as $key => $attribute) {
$pdo->setAttribute($key, $attribute);
}
2023-09-01 10:54:55 +08:00
return $pdo;
2023-08-14 14:14:40 +08:00
}
2023-05-26 09:20:29 +08:00
/**
* @return Pool
*/
2023-08-29 21:35:19 +08:00
protected function pool(): Pool
2023-05-26 09:20:29 +08:00
{
2024-05-01 02:06:14 +08:00
if (!$this->connections->hasChannel($this->getName())) {
$this->connections->created($this->getName(), $this->pool['max'] ?? 1, [$this, 'newConnect']);
2023-05-26 09:20:29 +08:00
}
return $this->connections;
}
2022-01-09 03:49:51 +08:00
}