Merge branch 'v1.0'
This commit is contained in:
@@ -37,6 +37,8 @@ class Connection extends Component
|
|||||||
public string $charset = 'utf-8';
|
public string $charset = 'utf-8';
|
||||||
public string $tablePrefix = '';
|
public string $tablePrefix = '';
|
||||||
|
|
||||||
|
public string $database = '';
|
||||||
|
|
||||||
public int $timeout = 1900;
|
public int $timeout = 1900;
|
||||||
|
|
||||||
public int $maxNumber = 200;
|
public int $maxNumber = 200;
|
||||||
@@ -172,7 +174,10 @@ class Connection extends Component
|
|||||||
public function masterInstance(): PDO
|
public function masterInstance(): PDO
|
||||||
{
|
{
|
||||||
return $this->connections()->get([
|
return $this->connections()->get([
|
||||||
'cds' => $this->cds, 'username' => $this->username, 'password' => $this->password
|
'cds' => $this->cds,
|
||||||
|
'username' => $this->username,
|
||||||
|
'password' => $this->password,
|
||||||
|
'database' => $this->database
|
||||||
], true);
|
], true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+194
-193
@@ -20,221 +20,222 @@ 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 $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, bool $isMaster = false): mixed
|
public function get(mixed $config, bool $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
|
||||||
{
|
{
|
||||||
if (Coroutine::getCid() === -1) {
|
if (Coroutine::getCid() === -1) {
|
||||||
Runtime::enableCoroutine(false);
|
Runtime::enableCoroutine(false);
|
||||||
}
|
}
|
||||||
$link = new PDO($config['cds'], $config['username'], $config['password'], [
|
$cds = 'mysql:dbname=' . $config['database'] . ';host=' . $config['cds'];
|
||||||
PDO::ATTR_EMULATE_PREPARES => false,
|
$link = new PDO($cds, $config['username'], $config['password'], [
|
||||||
PDO::ATTR_CASE => PDO::CASE_NATURAL,
|
PDO::ATTR_EMULATE_PREPARES => false,
|
||||||
PDO::ATTR_TIMEOUT => $this->timeout,
|
PDO::ATTR_CASE => PDO::CASE_NATURAL,
|
||||||
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
|
PDO::ATTR_TIMEOUT => $this->timeout,
|
||||||
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . ($config['charset'] ?? 'utf8mb4')
|
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_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
$link->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);
|
$link->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
|
||||||
return $link;
|
$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);
|
||||||
Context::remove($coroutineName);
|
Context::remove($coroutineName);
|
||||||
$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, bool $isMaster = false)
|
public function disconnect($coroutineName, bool $isMaster = false)
|
||||||
{
|
{
|
||||||
Context::remove($coroutineName);
|
Context::remove($coroutineName);
|
||||||
$coroutineName = $this->name($coroutineName, $isMaster);
|
$coroutineName = $this->name($coroutineName, $isMaster);
|
||||||
$this->clean($coroutineName);
|
$this->clean($coroutineName);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,8 +53,7 @@ class Redis extends Pool
|
|||||||
*/
|
*/
|
||||||
public function get(mixed $config, bool $isMaster = false): mixed
|
public function get(mixed $config, bool $isMaster = false): mixed
|
||||||
{
|
{
|
||||||
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
|
$coroutineName = $this->name('redis', 'redis:' . $config['host'], $isMaster);
|
||||||
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
|
|
||||||
if (!Context::hasContext($coroutineName)) {
|
if (!Context::hasContext($coroutineName)) {
|
||||||
return Context::setContext($coroutineName, $this->getFromChannel($coroutineName, $config));
|
return Context::setContext($coroutineName, $this->getFromChannel($coroutineName, $config));
|
||||||
}
|
}
|
||||||
@@ -100,8 +99,7 @@ class Redis extends Pool
|
|||||||
*/
|
*/
|
||||||
public function release(array $config, bool $isMaster = false)
|
public function release(array $config, bool $isMaster = false)
|
||||||
{
|
{
|
||||||
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
|
$coroutineName = $this->name('redis', 'redis:' . $config['host'], $isMaster);
|
||||||
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
|
|
||||||
if (!Context::hasContext($coroutineName)) {
|
if (!Context::hasContext($coroutineName)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -118,8 +116,7 @@ class Redis extends Pool
|
|||||||
*/
|
*/
|
||||||
public function destroy(array $config, bool $isMaster = false)
|
public function destroy(array $config, bool $isMaster = false)
|
||||||
{
|
{
|
||||||
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
|
$coroutineName = $this->name('redis', 'redis:' . $config['host'], $isMaster);
|
||||||
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
|
|
||||||
if (Context::hasContext($coroutineName)) {
|
if (Context::hasContext($coroutineName)) {
|
||||||
$this->decrement($coroutineName);
|
$this->decrement($coroutineName);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user