This commit is contained in:
2021-03-01 16:01:32 +08:00
parent a59e8dbeee
commit 1f828ad01d
4 changed files with 255 additions and 255 deletions
+2 -2
View File
@@ -317,8 +317,8 @@ class Connection extends Component
public function disconnect() public function disconnect()
{ {
$connections = $this->connections(); $connections = $this->connections();
$connections->disconnect($this->cds); $connections->disconnect($this->cds, true);
$connections->disconnect($this->slaveConfig['cds']); $connections->disconnect($this->slaveConfig['cds'], false);
} }
} }
+2 -1
View File
@@ -35,8 +35,9 @@ abstract class Callback extends HttpService
try { try {
fire(Event::SYSTEM_RESOURCE_CLEAN); fire(Event::SYSTEM_RESOURCE_CLEAN);
\logger()->insert();
Snowflake::clearProcessId($server->worker_pid); Snowflake::clearProcessId($server->worker_pid);
Timer::clearAll();
$logger = Snowflake::app()->getLogger(); $logger = Snowflake::app()->getLogger();
$logger->write($this->_MESSAGE[$message] . $worker_id); $logger->write($this->_MESSAGE[$message] . $worker_id);
+247 -246
View File
@@ -20,136 +20,136 @@ class Connection extends Pool
{ {
public array $hasCreate = []; public array $hasCreate = [];
public int $timeout = 1900; public int $timeout = 1900;
/** @var PDO[] */ /** @var PDO[] */
protected array $connections = []; protected array $connections = [];
/** /**
* @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 (!isset($this->hasCreate[$coroutineName])) { if (!isset($this->hasCreate[$coroutineName])) {
$this->hasCreate[$coroutineName] = 0; $this->hasCreate[$coroutineName] = 0;
} }
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);
} }
/** /**
@@ -158,25 +158,25 @@ class Connection extends Pool
* @return PDO * @return PDO
* @throws ComponentException * @throws ComponentException
*/ */
public function createClient(string $name, mixed $config): PDO public function createClient(string $name, mixed $config): PDO
{ {
$this->printClients($config['cds'], $name, true); $this->printClients($config['cds'], $name, true);
// TODO: Implement createClient() method. // TODO: Implement createClient() method.
$link = new PDO($config['cds'], $config['username'], $config['password'], [ $link = new PDO($config['cds'], $config['username'], $config['password'], [
PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_CASE => PDO::CASE_NATURAL, PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_TIMEOUT => $this->timeout, PDO::ATTR_TIMEOUT => $this->timeout,
]); ]);
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$link->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false); $link->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
$link->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING); $link->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);
$charset = $config['charset'] ?? 'utf8mb4'; $charset = $config['charset'] ?? 'utf8mb4';
if (!empty($charset)) { if (!empty($charset)) {
$link->query('SET NAMES ' . $charset); $link->query('SET NAMES ' . $charset);
} }
return $link; return $link;
} }
/** /**
@@ -185,131 +185,132 @@ class Connection extends Pool
* @param false $isBefore * @param false $isBefore
* @throws ComponentException * @throws ComponentException
*/ */
public function printClients($cds, $coroutineName, $isBefore = false) public function printClients($cds, $coroutineName, $isBefore = false)
{ {
// $this->warning(($isBefore ? 'before ' : '') . 'create client[address: ' . $cds . ', ' . env('workerId') . ', coroutine: ' . Coroutine::getCid() . ', has num: ' . $this->size($coroutineName) . ', has create: ' . $this->hasCreate[$coroutineName] . ']'); // $this->warning(($isBefore ? 'before ' : '') . 'create client[address: ' . $cds . ', ' . env('workerId') . ', coroutine: ' . Coroutine::getCid() . ', has num: ' . $this->size($coroutineName) . ', has create: ' . $this->hasCreate[$coroutineName] . ']');
} }
/** /**
* @param $coroutineName * @param $coroutineName
* @param $isMaster * @param $isMaster
*/ */
public function release($coroutineName, $isMaster) public function release($coroutineName, $isMaster)
{ {
$coroutineName = $this->name('mysql', $coroutineName, $isMaster); $coroutineName = $this->name('mysql', $coroutineName, $isMaster);
if (!$this->hasClient($coroutineName)) { if (!$this->hasClient($coroutineName)) {
return; return;
} }
/** @var PDO $client */ /** @var PDO $client */
$client = Context::getContext($coroutineName); $client = Context::getContext($coroutineName);
if ($client->inTransaction()) { if ($client->inTransaction()) {
$client->commit(); $client->commit();
} }
$this->push($coroutineName, $client); $this->push($coroutineName, $client);
$this->remove($coroutineName); $this->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
*/ */
public function connection_clear() public function connection_clear()
{ {
$connections = Context::getAllContext(); $connections = Context::getAllContext();
foreach ($connections as $name => $connection) { foreach ($connections as $name => $connection) {
if (empty($connection) || !($connection instanceof PDO)) { if (empty($connection) || !($connection instanceof PDO)) {
continue; continue;
} }
/** @var PDO $pdoClient */ /** @var PDO $pdoClient */
if ($connection->inTransaction()) { if ($connection->inTransaction()) {
$connection->commit(); $connection->commit();
} }
$this->push($name, $connection); $this->push($name, $connection);
$this->remove($name); $this->remove($name);
} }
$this->hasCreate = []; $this->hasCreate = [];
$this->creates = 0; $this->creates = 0;
} }
/** /**
* @param $coroutineName * @param $coroutineName
*/ */
public function remove($coroutineName) public function remove($coroutineName)
{ {
Context::remove($coroutineName); Context::remove($coroutineName);
} }
/** /**
* @param $name * @param $name
* @param $client * @param $client
* @return bool * @return bool
*/ */
public function checkCanUse($name, $client): bool public function checkCanUse($name, $client): bool
{ {
try { try {
if (empty($client) || !($client instanceof PDO)) { if (empty($client) || !($client instanceof PDO)) {
return $result = false; return $result = false;
} }
if (!$client->getAttribute(PDO::ATTR_SERVER_INFO)) { if (!$client->getAttribute(PDO::ATTR_SERVER_INFO)) {
return $result = false; return $result = false;
} }
return $result = true; return $result = true;
} catch (\Swoole\Error | \Throwable $exception) { } catch (\Swoole\Error | \Throwable $exception) {
return $result = false; return $result = false;
} finally { } finally {
if (!$result) { if (!$result) {
$this->desc($name); $this->desc($name);
} }
} }
} }
/** /**
* @param $coroutineName * @param $coroutineName
* @throws Exception * @param bool $isMaster
*/ * @throws Exception
public function disconnect($coroutineName) */
{ public function disconnect($coroutineName, $isMaster = false)
if (!$this->hasClient($coroutineName)) { {
return; $coroutineName = $this->name($coroutineName, $isMaster);
} if ($this->hasClient($coroutineName)) {
$this->remove($coroutineName); $this->remove($coroutineName);
$this->clean($coroutineName); }
} $this->clean($coroutineName);
}
/** /**
* @param $coroutineName * @param $coroutineName
*/ */
public function incr($coroutineName) public function incr($coroutineName)
{ {
if (!isset($this->hasCreate[$coroutineName])) { if (!isset($this->hasCreate[$coroutineName])) {
$this->hasCreate[$coroutineName] = 0; $this->hasCreate[$coroutineName] = 0;
} }
$this->hasCreate[$coroutineName] += 1; $this->hasCreate[$coroutineName] += 1;
} }
/** /**
* @param string $name * @param string $name
*/ */
public function desc(string $name) public function desc(string $name)
{ {
if (!isset($this->hasCreate[$name])) { if (!isset($this->hasCreate[$name])) {
$this->hasCreate[$name] = 0; $this->hasCreate[$name] = 0;
} }
$this->hasCreate[$name] -= 1; $this->hasCreate[$name] -= 1;
} }
} }
+4 -6
View File
@@ -104,13 +104,11 @@ class Redis extends Pool
{ {
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster); $coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
if (!Context::hasContext($coroutineName)) { if (Context::hasContext($coroutineName)) {
return; $this->desc($coroutineName);
$this->remove($coroutineName);
} }
$this->desc($coroutineName);
$this->remove($coroutineName);
$this->clean($coroutineName); $this->clean($coroutineName);
} }