This commit is contained in:
2021-05-07 19:38:37 +08:00
parent a0061d8c1c
commit 45f5787dfb
2 changed files with 209 additions and 200 deletions
+201 -197
View File
@@ -9,6 +9,7 @@ use PDO;
use Snowflake\Abstracts\Pool; use Snowflake\Abstracts\Pool;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Error; use Swoole\Error;
use Swoole\Runtime;
use Throwable; use Throwable;
/** /**
@@ -19,226 +20,229 @@ class Connection extends Pool
{ {
public int $timeout = 1900; public int $timeout = 1900;
/** /**
* @param $timeout * @param $timeout
*/ */
public function setTimeout($timeout) public function setTimeout($timeout)
{ {
$this->timeout = $timeout; $this->timeout = $timeout;
} }
/** /**
* @param $value * @param $value
*/ */
public function setLength($value) public function setLength($value)
{ {
$this->max = $value; $this->max = $value;
} }
/** /**
* @param $cds * @param $cds
* @return bool * @return bool
* *
* db is in transaction * db is in transaction
*/ */
public function inTransaction($cds): bool public function inTransaction($cds): bool
{ {
return Context::getContext('begin_' . $this->name('mysql', $cds, true)) == 0; return Context::getContext('begin_' . $this->name('mysql', $cds, true)) == 0;
} }
/** /**
* @param $coroutineName * @param $coroutineName
*/ */
public function beginTransaction($coroutineName) public function beginTransaction($coroutineName)
{ {
$coroutineName = $this->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); Context::increment('begin_' . $coroutineName);
if (!Context::getContext('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 instanceof PDO && !$connection->inTransaction()) {
$connection->beginTransaction(); $connection->beginTransaction();
} }
} }
/** /**
* @param $coroutineName * @param $coroutineName
*/ */
public function commit($coroutineName) public function commit($coroutineName)
{ {
$coroutineName = $this->name('mysql', $coroutineName, true); $coroutineName = $this->name('mysql', $coroutineName, true);
if (!Context::hasContext('begin_' . $coroutineName)) { if (!Context::hasContext('begin_' . $coroutineName)) {
return; return;
} }
if (Context::decrement('begin_' . $coroutineName) > 0) { if (Context::decrement('begin_' . $coroutineName) > 0) {
return; return;
} }
$connection = Context::getContext($coroutineName); $connection = Context::getContext($coroutineName);
if (!($connection instanceof PDO)) { if (!($connection instanceof PDO)) {
return; return;
} }
Context::setContext('begin_' . $coroutineName, 0); Context::setContext('begin_' . $coroutineName, 0);
if ($connection->inTransaction()) { if ($connection->inTransaction()) {
$connection->commit(); $connection->commit();
} }
} }
/** /**
* @param $name * @param $name
* @param false $isMaster * @param false $isMaster
* @return array * @return array
*/ */
private function getIndex($name, $isMaster = false): array private function getIndex($name, $isMaster = false): array
{ {
return [Coroutine::getCid(), $this->name('mysql', $name, $isMaster)]; return [Coroutine::getCid(), $this->name('mysql', $name, $isMaster)];
} }
/** /**
* @param $coroutineName * @param $coroutineName
*/ */
public function rollback($coroutineName) public function rollback($coroutineName)
{ {
$coroutineName = $this->name('mysql', $coroutineName, true); $coroutineName = $this->name('mysql', $coroutineName, true);
if (!Context::hasContext('begin_' . $coroutineName)) { if (!Context::hasContext('begin_' . $coroutineName)) {
return; return;
} }
if (Context::decrement('begin_' . $coroutineName) > 0) { if (Context::decrement('begin_' . $coroutineName) > 0) {
return; return;
} }
if (($connection = Context::getContext($coroutineName)) instanceof PDO) { if (($connection = Context::getContext($coroutineName)) instanceof PDO) {
if ($connection->inTransaction()) { if ($connection->inTransaction()) {
$connection->rollBack(); $connection->rollBack();
} }
} }
Context::setContext('begin_' . $coroutineName, 0); Context::setContext('begin_' . $coroutineName, 0);
} }
/** /**
* @param mixed $config * @param mixed $config
* @param bool $isMaster * @param bool $isMaster
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function get(mixed $config, $isMaster = false): mixed public function get(mixed $config, $isMaster = false): mixed
{ {
$coroutineName = $this->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;
} }
$connections = $this->getFromChannel($coroutineName, $config); $connections = $this->getFromChannel($coroutineName, $config);
if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) { if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) {
$number > 0 && $connections->beginTransaction(); $number > 0 && $connections->beginTransaction();
} }
return Context::setContext($coroutineName, $connections); return Context::setContext($coroutineName, $connections);
} }
/** /**
* @param string $name * @param string $name
* @param mixed $config * @param mixed $config
* @return PDO * @return PDO
* @throws Exception * @throws Exception
*/ */
public function createClient(string $name, mixed $config): PDO public function createClient(string $name, mixed $config): PDO
{ {
$link = new PDO($config['cds'], $config['username'], $config['password'], [ if (Coroutine::getCid() === -1) {
PDO::ATTR_EMULATE_PREPARES => false, Runtime::enableCoroutine(false);
PDO::ATTR_CASE => PDO::CASE_NATURAL, }
PDO::ATTR_TIMEOUT => $this->timeout, $link = new PDO($config['cds'], $config['username'], $config['password'], [
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true, PDO::ATTR_EMULATE_PREPARES => false,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . ($config['charset'] ?? 'utf8mb4') PDO::ATTR_CASE => PDO::CASE_NATURAL,
]); PDO::ATTR_TIMEOUT => $this->timeout,
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
$link->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false); PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . ($config['charset'] ?? 'utf8mb4')
$link->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING); ]);
return $link; $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
* @throws Exception * @throws Exception
*/ */
public function release($coroutineName, $isMaster) public function release($coroutineName, $isMaster)
{ {
$coroutineName = $this->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;
} }
if ($client->inTransaction()) { if ($client->inTransaction()) {
$client->commit(); $client->commit();
} }
$this->push($coroutineName, $client); $this->push($coroutineName, $client);
$this->lastTime = time(); $this->lastTime = time();
} }
/** /**
* @param $coroutineName * @param $coroutineName
* @return bool * @return bool
*/ */
private function hasClient($coroutineName): bool private function hasClient($coroutineName): bool
{ {
return Context::hasContext($coroutineName); return Context::hasContext($coroutineName);
} }
/** /**
* batch release * batch release
* @throws Exception * @throws Exception
*/ */
public function connection_clear() public function connection_clear()
{ {
$this->flush(0); $this->flush(0);
} }
/** /**
* @param string $name * @param string $name
* @param mixed $client * @param mixed $client
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function checkCanUse(string $name, mixed $client): bool public function checkCanUse(string $name, mixed $client): bool
{ {
try { try {
if (empty($client) || !($client instanceof PDO)) { if (empty($client) || !($client instanceof PDO)) {
$result = false; $result = false;
} else { } else {
$result = true; $result = true;
} }
} catch (Error | Throwable $exception) { } catch (Error | Throwable $exception) {
$result = $this->addError($exception, 'mysql'); $result = $this->addError($exception, 'mysql');
} finally { } finally {
if (!$result) { if (!$result) {
$this->decrement($name); $this->decrement($name);
} }
return $result; return $result;
} }
} }
/** /**
* @param $coroutineName * @param $coroutineName
* @param bool $isMaster * @param bool $isMaster
* @throws Exception * @throws Exception
*/ */
public function disconnect($coroutineName, $isMaster = false) public function disconnect($coroutineName, $isMaster = false)
{ {
$coroutineName = $this->name($coroutineName, $isMaster); $coroutineName = $this->name($coroutineName, $isMaster);
$this->clean($coroutineName); $this->clean($coroutineName);
} }
} }
+8 -3
View File
@@ -10,6 +10,8 @@ use HttpServer\Http\Context;
use Redis as SRedis; use Redis as SRedis;
use Snowflake\Abstracts\Pool; use Snowflake\Abstracts\Pool;
use Snowflake\Exception\RedisConnectException; use Snowflake\Exception\RedisConnectException;
use Swoole\Coroutine;
use Swoole\Runtime;
/** /**
* Class RedisClient * Class RedisClient
@@ -36,10 +38,10 @@ class Redis extends Pool
*/ */
public function canCreate(string $name): bool public function canCreate(string $name): bool
{ {
if (!isset($this->hasCreate[$name])) { if (!isset(static::$hasCreate[$name])) {
$this->hasCreate[$name] = 0; static::$hasCreate[$name] = 0;
} }
return $this->hasCreate[$name] >= $this->max; return static::$hasCreate[$name] >= $this->max;
} }
@@ -69,6 +71,9 @@ class Redis extends Pool
*/ */
public function createClient(string $name, mixed $config): SRedis public function createClient(string $name, mixed $config): SRedis
{ {
if (Coroutine::getCid() === -1) {
Runtime::enableCoroutine(false);
}
$redis = new SRedis(); $redis = new SRedis();
if (!$redis->pconnect($config['host'], (int)$config['port'], $config['timeout'])) { if (!$redis->pconnect($config['host'], (int)$config['port'], $config['timeout'])) {
throw new RedisConnectException(sprintf('The Redis Connect %s::%d Fail.', $config['host'], $config['port'])); throw new RedisConnectException(sprintf('The Redis Connect %s::%d Fail.', $config['host'], $config['port']));