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

316 lines
8.0 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace Snowflake\Pool;
use HttpServer\Http\Context;
use PDO;
use Exception;
2020-09-18 16:42:28 +08:00
use Snowflake\Abstracts\Config;
2021-02-23 16:56:34 +08:00
use Snowflake\Exception\ComponentException;
2020-08-31 01:27:08 +08:00
use Swoole\Coroutine;
use Snowflake\Abstracts\Pool;
2020-09-06 05:37:00 +08:00
use Swoole\Timer;
2020-08-31 01:27:08 +08:00
/**
* Class Connection
2020-08-31 12:38:32 +08:00
* @package Snowflake\Pool
2020-08-31 01:27:08 +08:00
*/
class Connection extends Pool
{
2021-02-17 00:51:21 +08:00
2021-02-22 00:56:05 +08:00
public array $hasCreate = [];
public int $timeout = 1900;
/** @var PDO[] */
protected array $connections = [];
/**
* @param $timeout
*/
public function setTimeout($timeout)
{
$this->timeout = $timeout;
}
/**
* @param $value
*/
public function setLength($value)
{
$this->max = $value;
}
/**
* @param $cds
* @return bool
*
* db is in transaction
*/
public function inTransaction($cds): bool
{
return Context::getContext('begin_' . $this->name('mysql', $cds, true)) == 0;
}
/**
* @param $coroutineName
*/
public function beginTransaction($coroutineName)
{
$coroutineName = $this->name('mysql', $coroutineName, true);
if (!Context::hasContext('begin_' . $coroutineName)) {
Context::setContext('begin_' . $coroutineName, 0);
}
2021-02-25 13:35:48 +08:00
Context::increment('begin_' . $coroutineName);
2021-02-22 00:56:05 +08:00
if (!Context::getContext('begin_' . $coroutineName) !== 0) {
return;
}
$connection = Context::getContext($coroutineName);
if ($connection instanceof PDO && !$connection->inTransaction()) {
$connection->beginTransaction();
}
}
/**
* @param $coroutineName
*/
public function commit($coroutineName)
{
$coroutineName = $this->name('mysql', $coroutineName, true);
if (!Context::hasContext('begin_' . $coroutineName)) {
return;
}
2021-02-25 13:35:48 +08:00
if (Context::decrement('begin_' . $coroutineName) > 0) {
2021-02-22 00:56:05 +08:00
return;
}
$connection = Context::getContext($coroutineName);
if (!($connection instanceof PDO)) {
return;
}
Context::setContext('begin_' . $coroutineName, 0);
if ($connection->inTransaction()) {
$connection->commit();
}
}
/**
* @param $name
* @param false $isMaster
* @return array
*/
private function getIndex($name, $isMaster = false): array
{
return [Coroutine::getCid(), $this->name('mysql', $name, $isMaster)];
}
/**
* @param $coroutineName
*/
public function rollback($coroutineName)
{
$coroutineName = $this->name('mysql', $coroutineName, true);
if (!Context::hasContext('begin_' . $coroutineName)) {
return;
}
2021-02-25 13:35:48 +08:00
if (Context::decrement('begin_' . $coroutineName) > 0) {
2021-02-22 00:56:05 +08:00
return;
}
if (($connection = Context::getContext($coroutineName)) instanceof PDO) {
if ($connection->inTransaction()) {
$connection->rollBack();
}
}
Context::setContext('begin_' . $coroutineName, 0);
}
/**
2021-02-23 16:56:34 +08:00
* @param mixed $config
2021-02-22 00:56:05 +08:00
* @param bool $isMaster
* @return mixed
* @throws Exception
*/
2021-02-23 16:56:34 +08:00
public function get(mixed $config, $isMaster = false): mixed
2021-02-22 00:56:05 +08:00
{
$coroutineName = $this->name('mysql', $config['cds'], $isMaster);
if (!isset($this->hasCreate[$coroutineName])) {
$this->hasCreate[$coroutineName] = 0;
}
if (($pdo = Context::getContext($coroutineName)) instanceof PDO) {
return $pdo;
}
2021-02-23 16:56:34 +08:00
$connections = $this->getFromChannel($coroutineName, $config);
2021-02-22 00:56:05 +08:00
if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) {
$number > 0 && $connections->beginTransaction();
}
return Context::setContext($coroutineName, $connections);
}
2021-02-22 17:44:24 +08:00
/**
* @param string $name
2021-02-23 16:56:34 +08:00
* @param mixed $config
2021-02-22 17:44:24 +08:00
* @return PDO
2021-02-23 16:56:34 +08:00
* @throws ComponentException
2021-02-22 17:44:24 +08:00
*/
2021-02-23 16:56:34 +08:00
public function createClient(string $name, mixed $config): PDO
2021-02-22 00:56:05 +08:00
{
$this->printClients($config['cds'], $name, true);
// TODO: Implement createClient() method.
$link = new PDO($config['cds'], $config['username'], $config['password'], [
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_TIMEOUT => $this->timeout,
]);
$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);
$charset = $config['charset'] ?? 'utf8mb4';
if (!empty($charset)) {
$link->query('SET NAMES ' . $charset);
}
return $link;
}
2021-02-23 16:56:34 +08:00
/**
* @param $cds
* @param $coroutineName
* @param false $isBefore
* @throws ComponentException
*/
2021-02-22 00:56:05 +08:00
public function printClients($cds, $coroutineName, $isBefore = false)
{
2021-02-26 19:29:23 +08:00
// $this->warning(($isBefore ? 'before ' : '') . 'create client[address: ' . $cds . ', ' . env('workerId') . ', coroutine: ' . Coroutine::getCid() . ', has num: ' . $this->size($coroutineName) . ', has create: ' . $this->hasCreate[$coroutineName] . ']');
2021-02-22 00:56:05 +08:00
}
/**
* @param $coroutineName
* @param $isMaster
*/
public function release($coroutineName, $isMaster)
{
$coroutineName = $this->name('mysql', $coroutineName, $isMaster);
if (!$this->hasClient($coroutineName)) {
return;
}
/** @var PDO $client */
$client = Context::getContext($coroutineName);
if ($client->inTransaction()) {
$client->commit();
}
$this->push($coroutineName, $client);
$this->remove($coroutineName);
$this->lastTime = time();
}
/**
* @param $coroutineName
* @return bool
*/
private function hasClient($coroutineName): bool
{
return Context::hasContext($coroutineName);
}
/**
* batch release
*/
public function connection_clear()
{
$connections = Context::getAllContext();
foreach ($connections as $name => $connection) {
if (empty($connection) || !($connection instanceof PDO)) {
continue;
}
/** @var PDO $pdoClient */
if ($connection->inTransaction()) {
$connection->commit();
}
$this->push($name, $connection);
$this->remove($name);
}
$this->hasCreate = [];
$this->creates = 0;
}
/**
* @param $coroutineName
*/
public function remove($coroutineName)
{
2021-02-25 13:35:48 +08:00
Context::remove($coroutineName);
2021-02-22 00:56:05 +08:00
}
/**
* @param $name
* @param $client
* @return bool
*/
public function checkCanUse($name, $client): bool
{
try {
if (empty($client) || !($client instanceof PDO)) {
return $result = false;
}
if (!$client->getAttribute(PDO::ATTR_SERVER_INFO)) {
return $result = false;
}
return $result = true;
} catch (\Swoole\Error | \Throwable $exception) {
return $result = false;
} finally {
if (!$result) {
$this->desc($name);
}
}
}
/**
* @param $coroutineName
* @throws Exception
*/
public function disconnect($coroutineName)
{
if (!$this->hasClient($coroutineName)) {
return;
}
$this->remove($coroutineName);
$this->clean($coroutineName);
}
/**
* @param $coroutineName
*/
public function incr($coroutineName)
{
if (!isset($this->hasCreate[$coroutineName])) {
$this->hasCreate[$coroutineName] = 0;
}
$this->hasCreate[$coroutineName] += 1;
}
/**
* @param string $name
*/
public function desc(string $name)
{
if (!isset($this->hasCreate[$name])) {
$this->hasCreate[$name] = 0;
}
$this->hasCreate[$name] -= 1;
}
2020-08-31 01:27:08 +08:00
}