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

348 lines
7.6 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;
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
{
2020-10-29 18:17:25 +08:00
public array $hasCreate = [];
2020-08-31 01:27:08 +08:00
2020-10-29 18:17:25 +08:00
public int $timeout = 1900;
2020-08-31 01:27:08 +08:00
/** @var PDO[] */
2020-10-29 18:17:25 +08:00
protected array $connections = [];
2020-08-31 01:27:08 +08:00
2021-02-17 00:47:25 +08:00
public int $creates = 0;
2020-09-04 19:10:08 +08:00
2020-08-31 01:27:08 +08:00
/**
* @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
*/
2020-12-17 14:09:14 +08:00
public function inTransaction($cds): bool
2020-08-31 01:27:08 +08:00
{
2020-09-17 14:12:14 +08:00
$coroutineName = $this->name($cds, true);
2020-09-05 03:29:58 +08:00
if (!Context::hasContext('begin_' . $coroutineName)) {
2020-08-31 01:27:08 +08:00
return false;
}
2020-09-05 03:29:58 +08:00
return Context::getContext('begin_' . $coroutineName) == 0;
2020-08-31 01:27:08 +08:00
}
/**
* @param $coroutineName
*/
public function beginTransaction($coroutineName)
{
2020-09-17 14:12:14 +08:00
$coroutineName = $this->name($coroutineName, true);
2020-09-05 03:29:58 +08:00
if (!Context::hasContext('begin_' . $coroutineName)) {
Context::setContext('begin_' . $coroutineName, 0);
2020-08-31 01:27:08 +08:00
}
2020-09-05 03:29:58 +08:00
Context::autoIncr('begin_' . $coroutineName);
2020-09-06 04:28:57 +08:00
if (!Context::getContext('begin_' . $coroutineName) !== 0) {
return;
}
$connection = Context::getContext($coroutineName);
if ($connection instanceof PDO && !$connection->inTransaction()) {
$connection->beginTransaction();
}
2020-08-31 01:27:08 +08:00
}
/**
* @param $coroutineName
*/
public function commit($coroutineName)
{
2020-09-17 14:12:14 +08:00
$coroutineName = $this->name($coroutineName, true);
2020-09-05 03:29:58 +08:00
if (!Context::hasContext('begin_' . $coroutineName)) {
2020-08-31 01:27:08 +08:00
return;
}
2020-09-05 03:29:58 +08:00
if (Context::autoDecr('begin_' . $coroutineName) > 0) {
2020-08-31 01:27:08 +08:00
return;
}
$connection = Context::getContext($coroutineName);
2020-09-06 04:28:57 +08:00
if (!($connection instanceof PDO)) {
return;
}
Context::setContext('begin_' . $coroutineName, 0);
if ($connection->inTransaction()) {
$this->info('connection commit.');
$connection->commit();
2020-08-31 01:27:08 +08:00
}
}
/**
* @param $name
* @param false $isMaster
* @return array
*/
2020-12-17 14:09:14 +08:00
private function getIndex($name, $isMaster = false): array
2020-08-31 01:27:08 +08:00
{
2020-09-17 14:04:22 +08:00
return [Coroutine::getCid(), $this->name($name, $isMaster)];
2020-08-31 01:27:08 +08:00
}
/**
* @param $coroutineName
*/
public function rollback($coroutineName)
{
2020-09-17 14:12:14 +08:00
$coroutineName = $this->name($coroutineName, true);
2020-09-05 03:29:58 +08:00
if (!Context::hasContext('begin_' . $coroutineName)) {
2020-08-31 01:27:08 +08:00
return;
}
2020-09-05 03:29:58 +08:00
if (Context::autoDecr('begin_' . $coroutineName) > 0) {
2020-08-31 01:27:08 +08:00
return;
}
if ($this->hasClient($coroutineName)) {
/** @var PDO $connection */
$connection = Context::getContext($coroutineName);
if ($connection->inTransaction()) {
$this->info('connection rollBack.');
$connection->rollBack();
}
}
2020-09-05 03:29:58 +08:00
Context::setContext('begin_' . $coroutineName, 0);
2020-08-31 01:27:08 +08:00
}
/**
* @param array $config
* @param bool $isMaster
* @return mixed
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function getConnection(array $config, $isMaster = false): mixed
2020-08-31 01:27:08 +08:00
{
2020-09-17 14:12:14 +08:00
$coroutineName = $this->name($config['cds'], $isMaster);
2020-09-05 01:26:11 +08:00
if (!isset($this->hasCreate[$coroutineName])) {
$this->hasCreate[$coroutineName] = 0;
}
2020-08-31 01:27:08 +08:00
if (Context::hasContext($coroutineName)) {
return Context::getContext($coroutineName);
2020-09-05 01:23:09 +08:00
}
2021-02-15 20:45:35 +08:00
if (!$this->hasItem($coroutineName)) {
2021-02-16 23:47:41 +08:00
return $this->newClient($config, $coroutineName);
2021-02-15 20:45:35 +08:00
}
[$time, $connections] = $this->get($coroutineName);
if (!($connections instanceof PDO)) {
throw new Exception('Database exception.');
2020-09-05 02:01:31 +08:00
}
2021-01-04 18:40:11 +08:00
return Context::setContext($coroutineName, $connections);
2020-08-31 01:27:08 +08:00
}
/**
* @param $config
2021-01-04 18:40:11 +08:00
* @param $coroutineName
2021-01-05 10:23:20 +08:00
* @return PDO|null
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
2021-01-05 10:23:20 +08:00
private function newClient($config, $coroutineName): PDO|null
2020-08-31 01:27:08 +08:00
{
2021-02-16 23:51:48 +08:00
$this->printClients($config['cds'], $coroutineName, true);
2021-02-16 23:42:13 +08:00
return $this->createConnect($this->parseConfig($config, $coroutineName), $coroutineName, function ($cds, $username, $password, $charset, $coroutineName) {
2021-01-04 18:40:11 +08:00
$link = new PDO($cds, $username, $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);
if (!empty($charset)) {
$link->query('SET NAMES ' . $charset);
}
$this->incr($coroutineName);
if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) {
$number > 0 && $link->beginTransaction();
}
if ($this->creates === 0) {
2021-02-15 20:31:01 +08:00
$this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']);
2021-01-04 18:40:11 +08:00
}
return $link;
});
2020-08-31 01:27:08 +08:00
}
2020-09-18 16:42:28 +08:00
/**
* @param $config
2021-01-05 18:30:58 +08:00
* @param $name
2020-09-18 16:42:28 +08:00
* @return array
*/
2021-01-04 18:40:11 +08:00
private function parseConfig($config, $name): array
2020-09-18 16:42:28 +08:00
{
2021-01-04 18:40:11 +08:00
return [$config['cds'], $config['username'], $config['password'], $config['charset'] ?? 'utf8mb4', $name];
2020-09-18 16:42:28 +08:00
}
2021-01-05 18:30:58 +08:00
/**
* @param $cds
* @param $coroutineName
* @param false $isBefore
*/
public function printClients($cds, $coroutineName, $isBefore = false)
{
2021-02-14 21:47:41 +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-01-05 18:30:58 +08:00
}
2020-08-31 01:27:08 +08:00
/**
* @param $coroutineName
* @param $isMaster
*/
public function release($coroutineName, $isMaster)
{
2020-09-17 14:12:14 +08:00
$coroutineName = $this->name($coroutineName, $isMaster);
2020-08-31 01:27:08 +08:00
if (!$this->hasClient($coroutineName)) {
return;
}
/** @var PDO $client */
$client = Context::getContext($coroutineName);
if ($client->inTransaction()) {
$client->commit();
}
$this->push($coroutineName, $client);
$this->remove($coroutineName);
2020-09-06 05:37:00 +08:00
$this->lastTime = time();
2020-08-31 01:27:08 +08:00
}
/**
* @param $coroutineName
* @return bool
*/
2020-12-17 14:09:14 +08:00
private function hasClient($coroutineName): bool
2020-08-31 01:27:08 +08:00
{
return Context::hasContext($coroutineName);
}
/**
* batch release
*/
public function connection_clear()
{
$this->debug('receive all clients.');
$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);
}
2020-09-04 19:10:08 +08:00
$this->hasCreate = [];
2020-09-06 17:35:11 +08:00
$this->creates = 0;
2020-08-31 01:27:08 +08:00
}
/**
* @param $coroutineName
*/
public function remove($coroutineName)
{
Context::deleteId($coroutineName);
}
/**
2020-09-04 19:10:08 +08:00
* @param $name
2020-08-31 01:27:08 +08:00
* @param $time
2020-12-17 14:09:14 +08:00
* @param $client
2020-08-31 01:27:08 +08:00
* @return bool
*/
2020-12-17 14:09:14 +08:00
public function checkCanUse($name, $time, $client): bool
2020-08-31 01:27:08 +08:00
{
try {
2020-09-05 02:34:52 +08:00
if ($time + 60 * 10 > time()) {
2020-09-05 02:36:15 +08:00
return $result = true;
2020-09-05 02:34:52 +08:00
}
2020-12-17 14:09:14 +08:00
if (empty($client) || !($client instanceof PDO)) {
2020-09-04 19:10:08 +08:00
return $result = false;
2020-08-31 01:27:08 +08:00
}
2020-12-17 14:09:14 +08:00
if (!$client->getAttribute(PDO::ATTR_SERVER_INFO)) {
2020-09-04 19:10:08 +08:00
return $result = false;
2020-08-31 01:27:08 +08:00
}
2020-09-04 19:10:08 +08:00
return $result = true;
2020-08-31 01:27:08 +08:00
} catch (\Swoole\Error | \Throwable $exception) {
2020-09-04 19:10:08 +08:00
return $result = false;
} finally {
if (!$result) {
$this->desc($name);
}
2020-08-31 01:27:08 +08:00
}
}
/**
2020-09-05 01:35:52 +08:00
* @param $coroutineName
2020-08-31 01:27:08 +08:00
* @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;
}
/**
2021-01-04 16:17:59 +08:00
* @param string $name
2020-08-31 01:27:08 +08:00
*/
2021-01-04 16:17:59 +08:00
public function desc(string $name)
2020-08-31 01:27:08 +08:00
{
2021-01-04 16:17:59 +08:00
if (!isset($this->hasCreate[$name])) {
2021-01-04 16:20:08 +08:00
$this->hasCreate[$name] = 0;
2020-08-31 01:27:08 +08:00
}
2021-01-04 16:17:59 +08:00
$this->hasCreate[$name] -= 1;
2020-08-31 01:27:08 +08:00
}
}