This commit is contained in:
as2252258@163.com
2021-08-08 02:42:55 +08:00
parent b471ea3f5e
commit 8c74ed4d00
6 changed files with 543 additions and 599 deletions
-11
View File
@@ -439,16 +439,6 @@ abstract class BaseApplication extends Component
} }
/**
* @return Pool
* @throws Exception
*/
public function getClientsPool(): Pool
{
return $this->get('clientsPool');
}
/** /**
* @param $array * @param $array
* @throws \ReflectionException * @throws \ReflectionException
@@ -493,7 +483,6 @@ abstract class BaseApplication extends Component
'error' => ['class' => ErrorHandler::class], 'error' => ['class' => ErrorHandler::class],
'connections' => ['class' => Connection::class], 'connections' => ['class' => Connection::class],
'redis_connections' => ['class' => SRedis::class], 'redis_connections' => ['class' => SRedis::class],
'clientsPool' => ['class' => Pool::class],
'config' => ['class' => Config::class], 'config' => ['class' => Config::class],
'logger' => ['class' => Logger::class], 'logger' => ['class' => Logger::class],
'annotation' => ['class' => SAnnotation::class], 'annotation' => ['class' => SAnnotation::class],
-1
View File
@@ -48,7 +48,6 @@ use Snowflake\Pool\Pool;
* @property HttpFilter $filter * @property HttpFilter $filter
* @property RPCProducer $rpc * @property RPCProducer $rpc
* @property Shutdown $shutdown * @property Shutdown $shutdown
* @property Pool $clientsPool
*/ */
trait TraitApplication trait TraitApplication
{ {
+24
View File
@@ -0,0 +1,24 @@
<?php
namespace Snowflake\Pool;
use JetBrains\PhpStorm\Pure;
trait Alias
{
/**
* @param $cds
* @param false $isMaster
* @return string
*/
#[Pure] public function name($cds, bool $isMaster = false): string
{
if ($isMaster === true) {
return $cds . '_master';
} else {
return $cds . '_slave';
}
}
}
+43 -68
View File
@@ -20,7 +20,7 @@ use Throwable;
class Connection extends Component class Connection extends Component
{ {
private ?Pool $clientsPool = null; use Alias;
/** /**
@@ -32,7 +32,7 @@ class Connection extends Component
*/ */
public function inTransaction($cds): bool public function inTransaction($cds): bool
{ {
$name = $this->getPool()->name('Mysql:' . $cds, true); $name = $this->name('Mysql:' . $cds, true);
return Context::getContext('begin_' . $name) == 0; return Context::getContext('begin_' . $name) == 0;
} }
@@ -42,16 +42,15 @@ class Connection extends Component
*/ */
public function beginTransaction($coroutineName) public function beginTransaction($coroutineName)
{ {
$coroutineName = $this->getPool()->name('Mysql:' . $coroutineName, true); $coroutineName = $this->name('Mysql:' . $coroutineName, true);
if (!Context::hasContext('begin_' . $coroutineName)) { if (!Context::hasContext('begin_' . $coroutineName)) {
Context::setContext('begin_' . $coroutineName, 0); Context::setContext('begin_' . $coroutineName, 0);
} }
Context::increment('begin_' . $coroutineName); if (Context::increment('begin_' . $coroutineName) != 0) {
if (Context::getContext('begin_' . $coroutineName) != 0) {
return; return;
} }
$connection = Context::getContext($coroutineName); $connection = Context::getContext($coroutineName);
if ($connection instanceof PDO && !$connection->inTransaction()) { if (!$connection->inTransaction()) {
$connection->beginTransaction(); $connection->beginTransaction();
} }
} }
@@ -62,18 +61,11 @@ class Connection extends Component
*/ */
public function commit($coroutineName) public function commit($coroutineName)
{ {
$coroutineName = $this->getPool()->name('Mysql:' . $coroutineName, true); $coroutineName = $this->name('Mysql:' . $coroutineName, true);
if (!Context::hasContext('begin_' . $coroutineName)) { if (Context::decrement('begin_' . $coroutineName) != 0) {
return;
}
if (Context::decrement('begin_' . $coroutineName) > 0) {
return; return;
} }
$connection = Context::getContext($coroutineName); $connection = Context::getContext($coroutineName);
if (!($connection instanceof PDO)) {
return;
}
Context::setContext('begin_' . $coroutineName, 0);
if ($connection->inTransaction()) { if ($connection->inTransaction()) {
$connection->commit(); $connection->commit();
} }
@@ -86,11 +78,8 @@ class Connection extends Component
*/ */
public function rollback($coroutineName) public function rollback($coroutineName)
{ {
$coroutineName = $this->getPool()->name('Mysql:' . $coroutineName, true); $coroutineName = $this->name('Mysql:' . $coroutineName, true);
if (!Context::hasContext('begin_' . $coroutineName)) { if (Context::decrement('begin_' . $coroutineName) != 0) {
return;
}
if (Context::decrement('begin_' . $coroutineName) > 0) {
return; return;
} }
if (($connection = Context::getContext($coroutineName)) instanceof PDO) { if (($connection = Context::getContext($coroutineName)) instanceof PDO) {
@@ -98,7 +87,6 @@ class Connection extends Component
$connection->rollBack(); $connection->rollBack();
} }
} }
Context::setContext('begin_' . $coroutineName, 0);
} }
@@ -108,21 +96,14 @@ class Connection extends Component
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function get(mixed $config, bool $isMaster = false): mixed public function get(mixed $config, bool $isMaster = false): ?PDO
{ {
$coroutineName = $this->getPool()->name('Mysql:' . $config['cds'], $isMaster); $coroutineName = $this->name('Mysql:' . $config['cds'], $isMaster);
if (($pdo = Context::getContext($coroutineName)) instanceof PDO) { if (($pdo = Context::getContext($coroutineName)) instanceof PDO) {
return $pdo; return $pdo;
} }
if (Coroutine::getCid() === -1) {
$connections = $this->createClient($coroutineName, $config);
} else {
/** @var PDO $connections */ /** @var PDO $connections */
$connections = $this->getPool()->getFromChannel($coroutineName); $connections = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config));
if (empty($connections)) {
$connections = $this->createClient($coroutineName, $config);
}
}
if ($number = Context::getContext('begin_' . $coroutineName)) { if ($number = Context::getContext('begin_' . $coroutineName)) {
$number > 0 && $connections->beginTransaction(); $number > 0 && $connections->beginTransaction();
} }
@@ -130,6 +111,33 @@ class Connection extends Component
} }
/**
* @param $coroutineName
* @param $config
* @return \Closure
*/
public function create($coroutineName, $config)
{
return static function () use ($coroutineName, $config) {
if (Coroutine::getCid() === -1) {
Runtime::enableCoroutine(false);
}
$cds = 'mysql:dbname=' . $config['database'] . ';host=' . $config['cds'];
$link = new PDO($cds, $config['username'], $config['password'], [
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_TIMEOUT => 60,
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
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);
return $link;
};
}
/** /**
* @param $name * @param $name
* @param $isMaster * @param $isMaster
@@ -142,32 +150,6 @@ class Connection extends Component
} }
/**
* @param string $name
* @param mixed $config
* @return PDO
* @throws Exception
*/
public function createClient(string $name, mixed $config): PDO
{
if (Coroutine::getCid() === -1) {
Runtime::enableCoroutine(false);
}
$cds = 'mysql:dbname=' . $config['database'] . ';host=' . $config['cds'];
$link = new PDO($cds, $config['username'], $config['password'], [
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_TIMEOUT => 60,
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
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);
return $link;
}
/** /**
* @param $coroutineName * @param $coroutineName
* @param $isMaster * @param $isMaster
@@ -175,8 +157,7 @@ class Connection extends Component
*/ */
public function release($coroutineName, $isMaster) public function release($coroutineName, $isMaster)
{ {
$coroutineName = $this->getPool()->name('Mysql:' . $coroutineName, $isMaster); $coroutineName = $this->name('Mysql:' . $coroutineName, $isMaster);
/** @var PDO $client */ /** @var PDO $client */
if (!($client = Context::getContext($coroutineName)) instanceof PDO) { if (!($client = Context::getContext($coroutineName)) instanceof PDO) {
return; return;
@@ -205,7 +186,7 @@ class Connection extends Component
*/ */
public function connection_clear($name, $isMaster) public function connection_clear($name, $isMaster)
{ {
$this->getPool()->clean($this->getPool()->name($name, $isMaster)); $this->getPool()->clean($this->name($name, $isMaster));
} }
@@ -226,9 +207,6 @@ class Connection extends Component
} catch (Error | Throwable $exception) { } catch (Error | Throwable $exception) {
$result = $this->addError($exception, 'mysql'); $result = $this->addError($exception, 'mysql');
} finally { } finally {
if (!$result) {
$this->getPool()->decrement($name);
}
return $result; return $result;
} }
} }
@@ -242,7 +220,7 @@ class Connection extends Component
public function disconnect($coroutineName, bool $isMaster = false) public function disconnect($coroutineName, bool $isMaster = false)
{ {
Context::remove($coroutineName); Context::remove($coroutineName);
$coroutineName = $this->getPool()->name('Mysql:' . $coroutineName, $isMaster); $coroutineName = $this->name('Mysql:' . $coroutineName, $isMaster);
$this->getPool()->clean($coroutineName); $this->getPool()->clean($coroutineName);
} }
@@ -253,10 +231,7 @@ class Connection extends Component
*/ */
public function getPool(): Pool public function getPool(): Pool
{ {
if (!$this->clientsPool) { return Snowflake::getDi()->get(Pool::class);
$this->clientsPool = Snowflake::app()->getClientsPool();
}
return $this->clientsPool;
} }
} }
+25 -58
View File
@@ -29,37 +29,9 @@ class Pool extends Component
public int $creates = -1; public int $creates = -1;
private array $_times = []; private array $_times = [];
protected static array $hasCreate = []; use Alias;
/**
* @param string $name
*/
public function increment(string $name)
{
if (!isset(static::$hasCreate[$name])) {
static::$hasCreate[$name] = 0;
}
static::$hasCreate[$name] += 1;
}
/**
* @param string $name
*/
public function decrement(string $name)
{
if (!isset(static::$hasCreate[$name])) {
return;
}
if (static::$hasCreate[$name] <= 0) {
return;
}
static::$hasCreate[$name] -= 1;
}
/** /**
@@ -112,20 +84,28 @@ class Pool extends Component
$this->flush($channel, $min); $this->flush($channel, $min);
} }
$num[$key] += ($length = $channel->length()); $num[$key] += ($length = $channel->length());
if (str_starts_with($key, 'Mysql') && (Snowflake::isWorker() || Snowflake::isTask()) && $length > 0) {
$this->debug('Worker #' . env('worker') . ' use client -> ' . $key . ':' . $length);
}
$total += $length; $total += $length;
} }
$this->clear($total, $num);
}
/**
* @param $total
* @throws \Exception
*/
private function clear($total, $num)
{
write(var_export($num, true), 'connections'); write(var_export($num, true), 'connections');
if ($total < 1) { if ($total >= 1) {
return;
}
Timer::clear($this->creates); Timer::clear($this->creates);
if (Snowflake::isWorker() || Snowflake::isTask()) { if (Snowflake::isWorker() || Snowflake::isTask()) {
$this->debug('Worker #' . env('worker') . ' clear time tick.'); $this->debug('Worker #' . env('worker') . ' clear time tick.');
} }
$this->creates = -1; $this->creates = -1;
} }
}
/** /**
@@ -191,7 +171,7 @@ class Pool extends Component
if (!isset(static::$_connections[$name])) { if (!isset(static::$_connections[$name])) {
static::$_connections[$name] = new Channel(Config::get('databases.pool.max', 10)); static::$_connections[$name] = new Channel(Config::get('databases.pool.max', 10));
} }
if (static::$_connections[$name]->errCode == SWOOLE_CHANNEL_CLOSED){ if (static::$_connections[$name]->errCode == SWOOLE_CHANNEL_CLOSED) {
throw new Exception('Channel is Close.'); throw new Exception('Channel is Close.');
} }
if ($this->creates === -1) { if ($this->creates === -1) {
@@ -206,8 +186,11 @@ class Pool extends Component
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
public function getFromChannel($name): mixed public function get($name, $callback): mixed
{ {
if (Coroutine::getCid() === -1) {
return $callback();
}
$this->_times[$name] = time(); $this->_times[$name] = time();
$channel = $this->getChannel($name); $channel = $this->getChannel($name);
if (!$channel->isEmpty()) { if (!$channel->isEmpty()) {
@@ -216,22 +199,18 @@ class Pool extends Component
return $connection; return $connection;
} }
} }
return null; return $callback();
} }
/** /**
* @param $cds * @param $name
* @param false $isMaster * @return bool
* @return string * @throws \Snowflake\Exception\ConfigException
*/ */
#[Pure] public function name($cds, bool $isMaster = false): string public function isNull($name): bool
{ {
if ($isMaster === true) { return $this->getChannel($name)->isEmpty();
return $cds . '_master';
} else {
return $cds . '_slave';
}
} }
@@ -247,18 +226,6 @@ class Pool extends Component
} }
/**
* @param array $config
* @param bool $isMaster
* @return mixed
* @throws Exception
*/
public function get(mixed $config, bool $isMaster): mixed
{
throw new Exception('Undefined system processing function.');
}
/** /**
* @param string $name * @param string $name
* @return bool * @return bool
+9 -19
View File
@@ -22,7 +22,7 @@ use Swoole\Runtime;
class Redis extends Component class Redis extends Component
{ {
private ?Pool $clientsPool = null; use Alias;
/** /**
@@ -33,17 +33,11 @@ class Redis extends Component
*/ */
public function get(mixed $config, bool $isMaster = false): mixed public function get(mixed $config, bool $isMaster = false): mixed
{ {
$coroutineName = $this->getPool()->name('Redis:' . $config['host'], $isMaster); $coroutineName = $this->name('Redis:' . $config['host'], $isMaster);
if (Context::hasContext($coroutineName)) { if (Context::hasContext($coroutineName)) {
return Context::getContext($coroutineName); return Context::getContext($coroutineName);
} }
if (Coroutine::getCid() === -1) { $clients = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config));
return Context::setContext($coroutineName, $this->createClient($coroutineName, $config));
}
$clients = $this->getPool()->getFromChannel($coroutineName);
if (empty($clients)) {
return Context::setContext($coroutineName, $this->createClient($coroutineName, $config));
}
return Context::setContext($coroutineName, $clients); return Context::setContext($coroutineName, $clients);
} }
@@ -55,8 +49,9 @@ class Redis extends Component
* @throws RedisConnectException * @throws RedisConnectException
* @throws Exception * @throws Exception
*/ */
public function createClient(string $name, mixed $config): SRedis public function create(string $name, mixed $config): \Closure
{ {
return static function () use ($name, $config) {
if (Coroutine::getCid() === -1) { if (Coroutine::getCid() === -1) {
Runtime::enableCoroutine(false); Runtime::enableCoroutine(false);
} }
@@ -73,10 +68,8 @@ class Redis extends Component
$redis->select($config['databases']); $redis->select($config['databases']);
$redis->setOption(SRedis::OPT_READ_TIMEOUT, $config['read_timeout']); $redis->setOption(SRedis::OPT_READ_TIMEOUT, $config['read_timeout']);
$redis->setOption(SRedis::OPT_PREFIX, $config['prefix']); $redis->setOption(SRedis::OPT_PREFIX, $config['prefix']);
$this->getPool()->increment($name);
return $redis; return $redis;
};
} }
@@ -88,7 +81,7 @@ class Redis extends Component
*/ */
public function release(array $config, bool $isMaster = false) public function release(array $config, bool $isMaster = false)
{ {
$coroutineName = $this->getPool()->name('Redis:' . $config['host'], $isMaster); $coroutineName = $this->name('Redis:' . $config['host'], $isMaster);
if (!Context::hasContext($coroutineName)) { if (!Context::hasContext($coroutineName)) {
return; return;
} }
@@ -104,7 +97,7 @@ class Redis extends Component
*/ */
public function destroy(array $config, bool $isMaster = false) public function destroy(array $config, bool $isMaster = false)
{ {
$coroutineName = $this->getPool()->name('Redis:' . $config['host'], $isMaster); $coroutineName = $this->name('Redis:' . $config['host'], $isMaster);
if (Context::hasContext($coroutineName)) { if (Context::hasContext($coroutineName)) {
$this->getPool()->decrement($coroutineName); $this->getPool()->decrement($coroutineName);
} }
@@ -119,10 +112,7 @@ class Redis extends Component
*/ */
public function getPool(): Pool public function getPool(): Pool
{ {
if (!$this->clientsPool) { return Snowflake::getDi()->get(Pool::class);
$this->clientsPool = Snowflake::app()->getClientsPool();
}
return $this->clientsPool;
} }