This commit is contained in:
2021-02-20 13:02:58 +08:00
parent 874085f97a
commit d05cb47f66
18 changed files with 56 additions and 49 deletions
+29 -25
View File
@@ -11,7 +11,7 @@ declare(strict_types=1);
namespace Database;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Abstracts\Component;
use Database\Mysql\Schema;
@@ -69,7 +69,7 @@ class Connection extends Component
*/
public function init()
{
$event = Snowflake::app()->event;
$event = Snowflake::app()->getEvent();
$event->on(Event::RELEASE_ALL, [$this, 'disconnect']);
$event->on(Event::EVENT_AFTER_REQUEST, [$this, 'clear_connection']);
}
@@ -85,7 +85,7 @@ class Connection extends Component
}
$this->beginTransaction();
$event = Snowflake::app()->event;
$event = Snowflake::app()->getEvent();
$event->on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true);
$event->on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true);
}
@@ -97,7 +97,7 @@ class Connection extends Component
*/
public function getConnect($sql = NULL): PDO
{
$connections = Snowflake::app()->connections;
$connections = $this->connections();
$connections->initConnections($this->cds, true, $this->maxNumber);
$connections->initConnections($this->slaveConfig['cds'], false, $this->maxNumber);
$connections->setTimeout($this->timeout);
@@ -108,10 +108,11 @@ class Connection extends Component
/**
* 初始化 Channel
* @throws ComponentException
*/
public function fill()
{
$connections = Snowflake::app()->connections;
$connections = $this->connections();
$connections->initConnections($this->cds, true, $this->maxNumber);
$connections->initConnections($this->slaveConfig['cds'], false, $this->maxNumber);
}
@@ -152,7 +153,7 @@ class Connection extends Component
* @param $sql
* @return bool
*/
public function isWrite($sql): bool
#[Pure] public function isWrite($sql): bool
{
if (empty($sql)) return false;
@@ -179,13 +180,11 @@ class Connection extends Component
*/
public function masterInstance(): PDO
{
$config = [
return $this->connections()->getConnection([
'cds' => $this->cds,
'username' => $this->username,
'password' => $this->password
];
$pool = Snowflake::app()->connections;
return $pool->getConnection($config, true);
], true);
}
/**
@@ -194,12 +193,20 @@ class Connection extends Component
*/
public function slaveInstance(): PDO
{
if (empty($this->slaveConfig)) {
if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) {
return $this->masterInstance();
}
return $this->connections()->getConnection($this->slaveConfig, false);
}
$connections = Snowflake::app()->connections;
return $connections->getConnection($this->slaveConfig, false);
/**
* @return \Snowflake\Pool\Connection
* @throws ComponentException
*/
private function connections(): \Snowflake\Pool\Connection
{
return Snowflake::app()->getConnections();
}
@@ -209,8 +216,7 @@ class Connection extends Component
*/
public function beginTransaction(): static
{
$connections = Snowflake::app()->connections;
$connections->beginTransaction($this->cds);
$this->connections()->beginTransaction($this->cds);
return $this;
}
@@ -220,8 +226,7 @@ class Connection extends Component
*/
public function inTransaction(): bool|static
{
$connections = Snowflake::app()->connections;
return $connections->inTransaction($this->cds);
return $this->connections()->inTransaction($this->cds);
}
/**
@@ -230,8 +235,7 @@ class Connection extends Component
*/
public function rollback()
{
$connections = Snowflake::app()->connections;
$connections->rollback($this->cds);
$this->connections()->rollback($this->cds);
}
/**
@@ -240,8 +244,7 @@ class Connection extends Component
*/
public function commit()
{
$connections = Snowflake::app()->connections;
$connections->commit($this->cds);
$this->connections()->commit($this->cds);
}
/**
@@ -287,7 +290,7 @@ class Connection extends Component
*/
public function release()
{
$connections = Snowflake::app()->connections;
$connections = $this->connections();
$connections->release($this->cds, true);
$connections->release($this->slaveConfig['cds'], false);
@@ -296,10 +299,11 @@ class Connection extends Component
/**
* 临时回收
* @throws ComponentException
*/
public function recovery()
{
$connections = Snowflake::app()->connections;
$connections = $this->connections();
$connections->release($this->cds, true);
$connections->release($this->slaveConfig['cds'], false);
@@ -312,7 +316,7 @@ class Connection extends Component
*/
public function clear_connection()
{
$connections = Snowflake::app()->connections;
$connections = $this->connections();
$connections->release($this->cds, true);
$connections->release($this->slaveConfig['cds'], false);
@@ -324,7 +328,7 @@ class Connection extends Component
*/
public function disconnect()
{
$connections = Snowflake::app()->connections;
$connections = $this->connections();
$connections->disconnect($this->cds);
$connections->disconnect($this->slaveConfig['cds']);
}
+2 -2
View File
@@ -43,7 +43,7 @@ class Db
*/
public static function commit()
{
$event = Snowflake::app()->event;
$event = Snowflake::app()->getEvent();
$event->trigger(Connection::TRANSACTION_COMMIT);
$event->offName(Connection::TRANSACTION_COMMIT);
static::$isActive = false;
@@ -54,7 +54,7 @@ class Db
*/
public static function rollback()
{
$event = Snowflake::app()->event;
$event = Snowflake::app()->getEvent();
$event->trigger(Connection::TRANSACTION_ROLLBACK);
$event->offName(Connection::TRANSACTION_ROLLBACK);
static::$isActive = false;
+1 -1
View File
@@ -39,7 +39,7 @@ abstract class Callback extends Application
$logger->write($this->_MESSAGE[$message] . $worker_id);
$logger->clear();
$event = Snowflake::app()->event;
$event = Snowflake::app()->getEvent();
$event->offName(Event::EVENT_AFTER_REQUEST);
$event->offName(Event::EVENT_BEFORE_REQUEST);
$this->eventNotify($message, $event);
+1 -1
View File
@@ -27,7 +27,7 @@ class OnConnect extends Callback
*/
public function onHandler(\Swoole\Server $server, int $fd, int $reactorId)
{
$event = Snowflake::app()->event;
$event = Snowflake::app()->getEvent();
$event->trigger(Event::RECEIVE_CONNECTION, [$server, $fd, $reactorId]);
}
+1 -1
View File
@@ -23,7 +23,7 @@ class OnManagerStart extends Callback
$this->debug('manager start.');
Snowflake::setWorkerId($server->manager_pid);
$events = Snowflake::app()->event;
$events = Snowflake::app()->getEvent();
$events->trigger(Event::SERVER_MANAGER_START, null, $server);
if (Snowflake::isLinux()) {
$prefix = Config::get('id', false, 'system:');
+1 -1
View File
@@ -25,7 +25,7 @@ class OnManagerStop extends Callback
{
$this->warning('manager stop.');
$events = Snowflake::app()->event;
$events = Snowflake::app()->getEvent();
$events->trigger(Event::SERVER_MANAGER_STOP, [$server]);
$runPath = storage(null, 'worker');
+1 -1
View File
@@ -49,7 +49,7 @@ class OnPacket extends Callback
$response = DataResolve::pack($this->pack, $response);
return $server->sendto($clientInfo['address'], $clientInfo['port'], $response);
} finally {
$event = Snowflake::app()->event;
$event = Snowflake::app()->getEvent();
$event->trigger(Event::SERVER_WORKER_STOP);
}
}
+1 -1
View File
@@ -52,7 +52,7 @@ class OnReceive extends Callback
$response = DataResolve::pack($this->pack, $response);
return $server->send($fd, $response);
} finally {
$event = Snowflake::app()->event;
$event = Snowflake::app()->getEvent();
$event->trigger(Event::SERVER_WORKER_STOP);
}
}
+1 -1
View File
@@ -24,7 +24,7 @@ class OnStart extends Callback
if (Snowflake::isLinux()) {
name(Config::get('id', false, 'system:') . ': master.');
}
$event = Snowflake::app()->event;
$event = Snowflake::app()->getEvent();
$event->trigger(Event::SERVER_EVENT_START, null, $server);
}
+1 -1
View File
@@ -108,7 +108,7 @@ class OnTask extends Callback
$finish['info'] = $this->format($exception);
$this->error($exception, 'Task');
} finally {
$event = Snowflake::app()->event;
$event = Snowflake::app()->getEvent();
$event->trigger(Event::RELEASE_ALL);
Timer::clearAll();
}
+1 -1
View File
@@ -53,7 +53,7 @@ class OnWorkerStart extends Callback
{
try {
$this->debug(sprintf('Worker #%d is start.....', $worker_id));
$event = Snowflake::app()->event;
$event = Snowflake::app()->getEvent();
$event->trigger(Event::SERVER_WORKER_START, [$worker_id]);
} catch (\Throwable $exception) {
write($exception->getMessage(), 'worker');
+1 -1
View File
@@ -199,7 +199,7 @@ class Response extends Application
$string .= 'Command End!' . PHP_EOL . PHP_EOL;
echo $string;
$event = Snowflake::app()->event;
$event = Snowflake::app()->getEvent();
$event->trigger('CONSOLE_END');
return $result;
+1 -1
View File
@@ -314,7 +314,7 @@ class Server extends Application
if (!is_array($config['events'])) {
return;
}
$event = Snowflake::app()->event;
$event = Snowflake::app()->getEvent();
foreach ($config['events'] as $name => $_event) {
$event->on($name, $_event);
}
+1 -1
View File
@@ -173,7 +173,7 @@ abstract class BaseApplication extends Service
if (!isset($config['events']) || !is_array($config['events'])) {
return;
}
$event = Snowflake::app()->event;
$event = Snowflake::app()->getEvent();
foreach ($config['events'] as $key => $value) {
if (is_string($value)) {
if (!class_exists($value)) {
+3 -3
View File
@@ -76,7 +76,7 @@ class Component extends BaseObject
*/
public function trigger($name, $event = null, $params = [], $isRemove = false)
{
$aEvents = Snowflake::app()->event;
$aEvents = Snowflake::app()->getEvent();
if (isset($this->_events[$name])) {
$events = $this->_events[$name];
foreach ($events as $key => $_event) {
@@ -100,7 +100,7 @@ class Component extends BaseObject
*/
public function off($name, $handler = NULL): void
{
$aEvents = Snowflake::app()->event;
$aEvents = Snowflake::app()->getEvent();
if (!isset($this->_events[$name])) {
$aEvents->of($name, $handler);
return;
@@ -129,7 +129,7 @@ class Component extends BaseObject
public function offAll()
{
$this->_events = [];
$aEvents = Snowflake::app()->event;
$aEvents = Snowflake::app()->getEvent();
$aEvents->clean();
}
+3 -2
View File
@@ -12,6 +12,7 @@ namespace Snowflake\Cache;
use Exception;
use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\Component;
use Swoole\Coroutine\System;
@@ -86,7 +87,7 @@ class File extends Component implements ICache
/**
* @param string $key
* @param string $hashKey
* @return string|int|bool
* @return string|int|bool|null
*/
public function hGet(string $key, string $hashKey): string|int|bool|null
{
@@ -119,7 +120,7 @@ class File extends Component implements ICache
* @param $key
* @return bool
*/
public function exists($key): bool
#[Pure] public function exists($key): bool
{
return file_exists($key);
}
+5 -3
View File
@@ -36,7 +36,7 @@ class Redis extends Component
*/
public function init()
{
$event = Snowflake::app()->event;
$event = Snowflake::app()->getEvent();
$event->on(Event::RELEASE_ALL, [$this, 'destroy']);
$event->on(Event::EVENT_AFTER_REQUEST, [$this, 'release']);
$event->on(Event::SERVER_WORKER_START, [$this, 'createPool']);
@@ -46,10 +46,11 @@ class Redis extends Component
/**
* @throws ConfigException
* @throws ComponentException
*/
public function createPool()
{
$connections = Snowflake::app()->pool->redis;
$connections = Snowflake::app()->getPool()->getRedis();
$config = $this->get_config();
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
@@ -112,10 +113,11 @@ SCRIPT;
/**
* 释放连接池
* @throws ConfigException
* @throws ComponentException
*/
public function release()
{
$connections = Snowflake::app()->pool->redis;
$connections = Snowflake::app()->getPool()->getRedis();
$connections->release($this->get_config(), true);
}
+2 -2
View File
@@ -74,7 +74,7 @@ class ErrorHandler extends Component implements ErrorInterface
{
$this->category = 'exception';
$event = Snowflake::app()->event;
$event = Snowflake::app()->getEvent();
$event->trigger(Event::RELEASE_ALL);
$this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine());
@@ -102,7 +102,7 @@ class ErrorHandler extends Component implements ErrorInterface
logger()->error($data, 'error');
$event = Snowflake::app()->event;
$event = Snowflake::app()->getEvent();
$event->trigger(Event::RELEASE_ALL);
throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]);