Files
kiri-core/kiri-engine/Pool/Connection.php
T

271 lines
5.6 KiB
PHP
Raw Normal View History

2022-01-09 03:50:38 +08:00
<?php
declare(strict_types=1);
namespace Kiri\Pool;
use Closure;
2022-02-25 18:37:49 +08:00
use Database\Db;
2022-01-09 03:50:38 +08:00
use Exception;
2022-01-14 15:52:38 +08:00
use Kiri;
2022-01-09 03:50:38 +08:00
use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config;
2022-12-12 17:31:12 +08:00
use Kiri\Di\Context;
2023-04-02 00:50:42 +08:00
use Kiri\Exception\ConfigException;
2022-12-12 17:31:12 +08:00
use PDO;
2022-01-09 03:50:38 +08:00
use Swoole\Error;
use Throwable;
/**
* Class Connection
* @package Kiri\Pool
*/
class Connection extends Component
{
2022-02-24 15:24:29 +08:00
2022-06-08 16:00:54 +08:00
private array $master = [];
2023-04-03 00:14:29 +08:00
private int $total = 0;
2022-06-08 16:00:54 +08:00
2022-02-24 15:24:29 +08:00
/**
2022-03-03 17:25:47 +08:00
* @param Pool $pool
* @param array $config
* @throws Exception
2022-02-24 15:24:29 +08:00
*/
2022-03-03 17:25:47 +08:00
public function __construct(public Pool $pool, array $config = [])
2022-02-24 15:24:29 +08:00
{
2022-03-03 17:25:47 +08:00
parent::__construct($config);
2022-02-24 15:24:29 +08:00
}
2022-01-09 03:50:38 +08:00
/**
2022-02-24 13:51:44 +08:00
* @param $name
2022-01-09 03:50:38 +08:00
* @return bool
*/
2022-02-24 11:00:01 +08:00
public function inTransaction($name): bool
2022-01-09 03:50:38 +08:00
{
$connection = Context::getContext($name);
2023-04-02 23:23:55 +08:00
if ($connection instanceof \Database\Mysql\PDO) {
2022-01-09 03:50:38 +08:00
return $connection->inTransaction();
}
return false;
}
2022-02-24 13:51:44 +08:00
2022-01-09 03:50:38 +08:00
/**
* @param $coroutineName
* @throws Exception
*/
public function beginTransaction($coroutineName)
{
2022-02-25 18:14:22 +08:00
$connection = $this->get($coroutineName);
2023-04-02 23:23:55 +08:00
if ($connection instanceof \Database\Mysql\PDO) {
2022-01-09 03:50:38 +08:00
$connection->beginTransaction();
}
}
/**
* @param $coroutineName
* @throws Exception
*/
public function commit($coroutineName)
{
$connection = Context::getContext($coroutineName);
2023-04-02 23:23:55 +08:00
if ($connection instanceof \Database\Mysql\PDO) {
2022-01-09 03:50:38 +08:00
$connection->commit();
}
}
/**
* @param $coroutineName
* @throws Exception
*/
public function rollback($coroutineName)
{
$connection = Context::getContext($coroutineName);
2023-04-02 23:23:55 +08:00
if ($connection instanceof \Database\Mysql\PDO) {
2022-01-09 03:50:38 +08:00
$connection->rollBack();
}
}
/**
* @param mixed $config
* @return PDO|null
2023-04-02 00:50:42 +08:00
* @throws ConfigException
2022-01-09 03:50:38 +08:00
*/
2023-04-02 23:23:55 +08:00
public function get(mixed $config): ?\Database\Mysql\PDO
2022-01-09 03:50:38 +08:00
{
2023-03-31 23:44:16 +08:00
if (!$this->pool->hasChannel($config['cds'])) {
$this->pool->initConnections($config['cds'], $config['pool']['max']);
2023-02-07 17:16:10 +08:00
}
2023-04-02 23:23:55 +08:00
return $this->pool->get($config['cds'], $this->create($config));
2022-06-17 11:59:19 +08:00
}
/**
* @param array $config
* @return Closure
*/
2023-04-02 00:34:55 +08:00
public function generate(array $config): Closure
2022-06-17 11:59:19 +08:00
{
2023-04-02 00:34:55 +08:00
return static function () use ($config) {
Kiri::getDi()->get(Kiri\Error\StdoutLoggerInterface::class)->alert('create database connect(' . $config['cds'] . ')');
$link = new \PDO('mysql:dbname=' . $config['dbname'] . ';host=' . $config['cds'], $config['username'], $config['password'], [
\PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_CASE => \PDO::CASE_NATURAL,
\PDO::ATTR_PERSISTENT => true,
\PDO::ATTR_TIMEOUT => $config['connect_timeout'],
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . ($config['charset'] ?? 'utf8mb4')
]);
$link->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$link->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false);
$link->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_EMPTY_STRING);
foreach ($config['attributes'] as $key => $attribute) {
$link->setAttribute($key, $attribute);
}
if (Db::inTransactionsActive()) {
$link->beginTransaction();
}
return $link;
};
2022-06-17 11:59:19 +08:00
}
/**
* @param string $name
2022-06-17 14:07:16 +08:00
* @return array
2022-06-17 11:59:19 +08:00
* @throws Kiri\Exception\ConfigException
*/
2022-06-17 14:07:16 +08:00
public function check(string $name): array
2022-06-17 11:59:19 +08:00
{
2022-06-17 14:07:16 +08:00
return $this->pool->check($name);
2022-01-09 03:50:38 +08:00
}
/**
* @param $config
* @return Closure
*/
2023-04-02 23:23:55 +08:00
public function create($config): Closure
2022-01-09 03:50:38 +08:00
{
2023-04-03 00:14:29 +08:00
return function () use ($config) {
if ($this->total >= 300) {
2023-04-03 00:43:06 +08:00
$connect = $this->pool->waite($config['cds'], 60);
if (!($connect instanceof \Database\Mysql\PDO)) {
2023-04-03 00:24:51 +08:00
throw new Exception("Get database link wait timeout.");
}
2023-04-03 00:14:29 +08:00
}
$this->total += 1;
2023-04-02 23:23:55 +08:00
return new \Database\Mysql\PDO($config);
2022-01-09 03:50:38 +08:00
};
}
2022-01-14 16:05:12 +08:00
/**
* @param string $name
2023-04-02 23:23:55 +08:00
* @param \Database\Mysql\PDO $PDO $PDO
2022-01-14 16:05:12 +08:00
* @return void
2023-04-02 23:23:55 +08:00
* @throws ConfigException
2022-01-14 16:05:12 +08:00
*/
2023-04-02 23:23:55 +08:00
public function addItem(string $name, \Database\Mysql\PDO $PDO): void
2022-01-14 16:05:12 +08:00
{
2022-02-24 15:24:29 +08:00
$this->pool->push($name, $PDO);
2022-01-14 16:05:12 +08:00
}
2022-01-09 03:50:38 +08:00
/**
* @param $name
* @param $max
* @throws Exception
*/
2022-02-24 11:00:01 +08:00
public function initConnections($name, $max)
2022-01-09 03:50:38 +08:00
{
2022-02-24 15:24:29 +08:00
$this->pool->initConnections($name, $max);
2022-01-09 03:50:38 +08:00
}
/**
* @param $coroutineName
2022-01-14 16:50:01 +08:00
* @throws Kiri\Exception\ConfigException
2022-01-09 03:50:38 +08:00
* @throws Exception
*/
2022-02-24 11:00:01 +08:00
public function release($coroutineName)
2022-01-09 03:50:38 +08:00
{
2022-02-18 14:45:59 +08:00
$client = Context::getContext($coroutineName);
2023-04-02 23:23:55 +08:00
if (!($client instanceof \Database\Mysql\PDO) || $client->inTransaction()) {
2022-01-09 03:50:38 +08:00
return;
}
2022-02-18 14:45:59 +08:00
2022-02-24 15:24:29 +08:00
$this->pool->push($coroutineName, $client);
2022-01-09 03:50:38 +08:00
Context::remove($coroutineName);
}
2022-09-19 18:39:28 +08:00
/**
* @throws Exception
*/
2022-09-20 18:23:19 +08:00
public function flush($coroutineName, $minNumber = 1)
{
2022-09-19 18:39:28 +08:00
$this->pool->flush($coroutineName, $minNumber);
}
2022-01-09 03:50:38 +08:00
/**
* @param $coroutineName
* @return bool
*/
private function hasClient($coroutineName): bool
{
return Context::hasContext($coroutineName);
}
/**
* batch release
* @throws Exception
*/
2022-02-24 11:00:01 +08:00
public function connection_clear($name)
2022-01-09 03:50:38 +08:00
{
2022-02-24 15:24:29 +08:00
$this->pool->clean($name);
2022-01-09 03:50:38 +08:00
}
/**
* @param string $name
* @param mixed $client
* @return bool
* @throws Exception
*/
public function checkCanUse(string $name, mixed $client): bool
{
try {
if (empty($client) || !($client instanceof PDO)) {
$result = false;
} else {
$result = true;
}
2022-01-14 15:52:38 +08:00
} catch (Error|Throwable $exception) {
2022-02-23 16:32:08 +08:00
$result = $this->logger->addError($exception, 'mysql');
2022-01-09 03:50:38 +08:00
} finally {
return $result;
}
}
/**
* @param $coroutineName
* @throws Exception
*/
2022-02-24 11:00:01 +08:00
public function disconnect($coroutineName)
2022-01-09 03:50:38 +08:00
{
Context::remove($coroutineName);
2022-02-24 15:24:29 +08:00
$this->pool->clean($coroutineName);
2022-01-09 03:50:38 +08:00
}
}