改名
This commit is contained in:
@@ -107,7 +107,7 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
|
||||
*/
|
||||
public function getModel(): ActiveRecord
|
||||
{
|
||||
return Snowflake::app()->getObject()->getConnection([$this->model], false);
|
||||
return Snowflake::app()->getObject()->get($this->model, false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -899,7 +899,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
||||
public static function populate(array $data): static
|
||||
{
|
||||
$object = Snowflake::app()->getObject();
|
||||
$model = $object->getConnection([static::class], false);
|
||||
$model = $object->get(static::class);
|
||||
$model->setAttributes($data);
|
||||
$model->setIsCreate(false);
|
||||
$model->refresh();
|
||||
|
||||
@@ -51,7 +51,7 @@ class CollectionIterator extends \ArrayIterator
|
||||
protected function newModel($current): ActiveRecord
|
||||
{
|
||||
$object = Snowflake::app()->getObject();
|
||||
$model = $object->getConnection([$this->model], false);
|
||||
$model = $object->get($this->model, false);
|
||||
|
||||
return $model->setAttributes($current);
|
||||
|
||||
|
||||
+258
-260
@@ -29,308 +29,306 @@ use Snowflake\Snowflake;
|
||||
*/
|
||||
class Connection extends Component
|
||||
{
|
||||
const TRANSACTION_COMMIT = 'transaction::commit';
|
||||
const TRANSACTION_ROLLBACK = 'transaction::rollback';
|
||||
const TRANSACTION_COMMIT = 'transaction::commit';
|
||||
const TRANSACTION_ROLLBACK = 'transaction::rollback';
|
||||
|
||||
public string $id = 'db';
|
||||
public string $cds = '';
|
||||
public string $password = '';
|
||||
public string $username = '';
|
||||
public string $charset = 'utf-8';
|
||||
public string $tablePrefix = '';
|
||||
public string $id = 'db';
|
||||
public string $cds = '';
|
||||
public string $password = '';
|
||||
public string $username = '';
|
||||
public string $charset = 'utf-8';
|
||||
public string $tablePrefix = '';
|
||||
|
||||
public int $timeout = 1900;
|
||||
public int $timeout = 1900;
|
||||
|
||||
public int $maxNumber = 200;
|
||||
public int $maxNumber = 200;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
* enable database cache
|
||||
*/
|
||||
public bool $enableCache = false;
|
||||
public string $cacheDriver = 'redis';
|
||||
/**
|
||||
* @var bool
|
||||
* enable database cache
|
||||
*/
|
||||
public bool $enableCache = false;
|
||||
public string $cacheDriver = 'redis';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*
|
||||
* @example [
|
||||
* 'cds' => 'mysql:dbname=dbname;host=127.0.0.1',
|
||||
* 'username' => 'root',
|
||||
* 'password' => 'root'
|
||||
* ]
|
||||
*/
|
||||
public array $slaveConfig = [];
|
||||
/**
|
||||
* @var array
|
||||
*
|
||||
* @example [
|
||||
* 'cds' => 'mysql:dbname=dbname;host=127.0.0.1',
|
||||
* 'username' => 'root',
|
||||
* 'password' => 'root'
|
||||
* ]
|
||||
*/
|
||||
public array $slaveConfig = [];
|
||||
|
||||
private ?Schema $_schema = null;
|
||||
private ?Schema $_schema = null;
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$event = Snowflake::app()->getEvent();
|
||||
$event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'disconnect']);
|
||||
$event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'clear_connection']);
|
||||
}
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$event = Snowflake::app()->getEvent();
|
||||
$event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'disconnect']);
|
||||
$event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'clear_connection']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function enablingTransactions()
|
||||
{
|
||||
if (!Db::transactionsActive()) {
|
||||
return;
|
||||
}
|
||||
$this->beginTransaction();
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function enablingTransactions()
|
||||
{
|
||||
if (!Db::transactionsActive()) {
|
||||
return;
|
||||
}
|
||||
$this->beginTransaction();
|
||||
|
||||
$event = Snowflake::app()->getEvent();
|
||||
$event->on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true);
|
||||
$event->on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true);
|
||||
}
|
||||
$event = Snowflake::app()->getEvent();
|
||||
$event->on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true);
|
||||
$event->on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $sql
|
||||
* @return PDO
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getConnect($sql = NULL): PDO
|
||||
{
|
||||
$connections = $this->connections();
|
||||
$connections->initConnections('mysql', $this->cds, true, $this->maxNumber);
|
||||
$connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber);
|
||||
$connections->setTimeout($this->timeout);
|
||||
/**
|
||||
* @param null $sql
|
||||
* @return PDO
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getConnect($sql = NULL): PDO
|
||||
{
|
||||
$connections = $this->connections();
|
||||
$connections->initConnections('mysql', $this->cds, true, $this->maxNumber);
|
||||
$connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber);
|
||||
$connections->setTimeout($this->timeout);
|
||||
|
||||
return $this->getPdo($sql);
|
||||
}
|
||||
return $this->getPdo($sql);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 初始化 Channel
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function fill()
|
||||
{
|
||||
$connections = $this->connections();
|
||||
$connections->initConnections('mysql', $this->cds, true, $this->maxNumber);
|
||||
$connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber);
|
||||
}
|
||||
/**
|
||||
* 初始化 Channel
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function fill()
|
||||
{
|
||||
$connections = $this->connections();
|
||||
$connections->initConnections('mysql', $this->cds, true, $this->maxNumber);
|
||||
$connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $sql
|
||||
* @return PDO
|
||||
* @throws Exception
|
||||
*/
|
||||
private function getPdo($sql): PDO
|
||||
{
|
||||
if ($this->isWrite($sql)) {
|
||||
$connect = $this->masterInstance();
|
||||
} else {
|
||||
$connect = $this->slaveInstance();
|
||||
}
|
||||
return $connect;
|
||||
}
|
||||
/**
|
||||
* @param $sql
|
||||
* @return PDO
|
||||
* @throws Exception
|
||||
*/
|
||||
private function getPdo($sql): PDO
|
||||
{
|
||||
if ($this->isWrite($sql)) {
|
||||
$connect = $this->masterInstance();
|
||||
} else {
|
||||
$connect = $this->slaveInstance();
|
||||
}
|
||||
return $connect;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function getSchema(): mixed
|
||||
{
|
||||
if ($this->_schema === null) {
|
||||
$this->_schema = Snowflake::createObject([
|
||||
'class' => Schema::class,
|
||||
'db' => $this
|
||||
]);
|
||||
}
|
||||
return $this->_schema;
|
||||
}
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function getSchema(): mixed
|
||||
{
|
||||
if ($this->_schema === null) {
|
||||
$this->_schema = Snowflake::createObject([
|
||||
'class' => Schema::class,
|
||||
'db' => $this
|
||||
]);
|
||||
}
|
||||
return $this->_schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $sql
|
||||
* @return bool
|
||||
*/
|
||||
#[Pure] public function isWrite($sql): bool
|
||||
{
|
||||
if (empty($sql)) return false;
|
||||
/**
|
||||
* @param $sql
|
||||
* @return bool
|
||||
*/
|
||||
#[Pure] public function isWrite($sql): bool
|
||||
{
|
||||
if (empty($sql)) return false;
|
||||
|
||||
$prefix = strtolower(mb_substr($sql, 0, 6));
|
||||
$prefix = strtolower(mb_substr($sql, 0, 6));
|
||||
|
||||
return in_array($prefix, ['insert', 'update', 'delete']);
|
||||
}
|
||||
return in_array($prefix, ['insert', 'update', 'delete']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function getCacheDriver(): mixed
|
||||
{
|
||||
if (!$this->enableCache) {
|
||||
return null;
|
||||
}
|
||||
return Snowflake::app()->get($this->cacheDriver);
|
||||
}
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function getCacheDriver(): mixed
|
||||
{
|
||||
if (!$this->enableCache) {
|
||||
return null;
|
||||
}
|
||||
return Snowflake::app()->get($this->cacheDriver);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PDO
|
||||
* @throws Exception
|
||||
*/
|
||||
public function masterInstance(): PDO
|
||||
{
|
||||
return $this->connections()->getConnection([
|
||||
'cds' => $this->cds,
|
||||
'username' => $this->username,
|
||||
'password' => $this->password
|
||||
], true);
|
||||
}
|
||||
/**
|
||||
* @return PDO
|
||||
* @throws Exception
|
||||
*/
|
||||
public function masterInstance(): PDO
|
||||
{
|
||||
return $this->connections()->get([
|
||||
'cds' => $this->cds, 'username' => $this->username, 'password' => $this->password
|
||||
], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PDO
|
||||
* @throws Exception
|
||||
*/
|
||||
public function slaveInstance(): PDO
|
||||
{
|
||||
if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) {
|
||||
return $this->masterInstance();
|
||||
}
|
||||
return $this->connections()->getConnection($this->slaveConfig, false);
|
||||
}
|
||||
/**
|
||||
* @return PDO
|
||||
* @throws Exception
|
||||
*/
|
||||
public function slaveInstance(): PDO
|
||||
{
|
||||
if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) {
|
||||
return $this->masterInstance();
|
||||
}
|
||||
return $this->connections()->get($this->slaveConfig, false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return \Snowflake\Pool\Connection
|
||||
* @throws ComponentException
|
||||
*/
|
||||
private function connections(): \Snowflake\Pool\Connection
|
||||
{
|
||||
return Snowflake::app()->getConnections();
|
||||
}
|
||||
/**
|
||||
* @return \Snowflake\Pool\Connection
|
||||
* @throws ComponentException
|
||||
*/
|
||||
private function connections(): \Snowflake\Pool\Connection
|
||||
{
|
||||
return Snowflake::app()->getConnections();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
*/
|
||||
public function beginTransaction(): static
|
||||
{
|
||||
$this->connections()->beginTransaction($this->cds);
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
*/
|
||||
public function beginTransaction(): static
|
||||
{
|
||||
$this->connections()->beginTransaction($this->cds);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this|bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function inTransaction(): bool|static
|
||||
{
|
||||
return $this->connections()->inTransaction($this->cds);
|
||||
}
|
||||
/**
|
||||
* @return $this|bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function inTransaction(): bool|static
|
||||
{
|
||||
return $this->connections()->inTransaction($this->cds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* 事务回滚
|
||||
*/
|
||||
public function rollback()
|
||||
{
|
||||
$this->connections()->rollback($this->cds);
|
||||
}
|
||||
/**
|
||||
* @throws Exception
|
||||
* 事务回滚
|
||||
*/
|
||||
public function rollback()
|
||||
{
|
||||
$this->connections()->rollback($this->cds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* 事务提交
|
||||
*/
|
||||
public function commit()
|
||||
{
|
||||
$this->connections()->commit($this->cds);
|
||||
}
|
||||
/**
|
||||
* @throws Exception
|
||||
* 事务提交
|
||||
*/
|
||||
public function commit()
|
||||
{
|
||||
$this->connections()->commit($this->cds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $sql
|
||||
* @return PDO
|
||||
* @throws Exception
|
||||
*/
|
||||
public function refresh($sql): PDO
|
||||
{
|
||||
if ($this->isWrite($sql)) {
|
||||
$instance = $this->masterInstance();
|
||||
} else {
|
||||
$instance = $this->slaveInstance();
|
||||
}
|
||||
return $instance;
|
||||
}
|
||||
/**
|
||||
* @param $sql
|
||||
* @return PDO
|
||||
* @throws Exception
|
||||
*/
|
||||
public function refresh($sql): PDO
|
||||
{
|
||||
if ($this->isWrite($sql)) {
|
||||
$instance = $this->masterInstance();
|
||||
} else {
|
||||
$instance = $this->slaveInstance();
|
||||
}
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $sql
|
||||
* @param array $attributes
|
||||
* @return Command
|
||||
* @throws
|
||||
*/
|
||||
public function createCommand($sql = null, $attributes = []): Command
|
||||
{
|
||||
$command = new Command(['db' => $this, 'sql' => $sql]);
|
||||
return $command->bindValues($attributes);
|
||||
}
|
||||
/**
|
||||
* @param $sql
|
||||
* @param array $attributes
|
||||
* @return Command
|
||||
* @throws
|
||||
*/
|
||||
public function createCommand($sql = null, $attributes = []): Command
|
||||
{
|
||||
$command = new Command(['db' => $this, 'sql' => $sql]);
|
||||
return $command->bindValues($attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Select
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getBuild(): Select
|
||||
{
|
||||
return $this->getSchema()->getQueryBuilder();
|
||||
}
|
||||
/**
|
||||
* @return Select
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getBuild(): Select
|
||||
{
|
||||
return $this->getSchema()->getQueryBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 回收链接
|
||||
* @throws
|
||||
*/
|
||||
public function release()
|
||||
{
|
||||
$connections = $this->connections();
|
||||
/**
|
||||
*
|
||||
* 回收链接
|
||||
* @throws
|
||||
*/
|
||||
public function release()
|
||||
{
|
||||
$connections = $this->connections();
|
||||
|
||||
$connections->release($this->cds, true);
|
||||
$connections->release($this->slaveConfig['cds'], false);
|
||||
}
|
||||
$connections->release($this->cds, true);
|
||||
$connections->release($this->slaveConfig['cds'], false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 临时回收
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function recovery()
|
||||
{
|
||||
$connections = $this->connections();
|
||||
/**
|
||||
* 临时回收
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function recovery()
|
||||
{
|
||||
$connections = $this->connections();
|
||||
|
||||
$connections->release($this->cds, true);
|
||||
$connections->release($this->slaveConfig['cds'], false);
|
||||
}
|
||||
$connections->release($this->cds, true);
|
||||
$connections->release($this->slaveConfig['cds'], false);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 回收链接
|
||||
* @throws
|
||||
*/
|
||||
public function clear_connection()
|
||||
{
|
||||
$connections = $this->connections();
|
||||
/**
|
||||
*
|
||||
* 回收链接
|
||||
* @throws
|
||||
*/
|
||||
public function clear_connection()
|
||||
{
|
||||
$connections = $this->connections();
|
||||
|
||||
$connections->release($this->cds, true);
|
||||
$connections->release($this->slaveConfig['cds'], false);
|
||||
}
|
||||
$connections->release($this->cds, true);
|
||||
$connections->release($this->slaveConfig['cds'], false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function disconnect()
|
||||
{
|
||||
$connections = $this->connections();
|
||||
$connections->disconnect($this->cds);
|
||||
$connections->disconnect($this->slaveConfig['cds']);
|
||||
}
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function disconnect()
|
||||
{
|
||||
$connections = $this->connections();
|
||||
$connections->disconnect($this->cds);
|
||||
$connections->disconnect($this->slaveConfig['cds']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+169
-168
@@ -10,6 +10,7 @@ use HttpServer\Http\Context;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use PDO;
|
||||
use Redis;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Pool\Timeout;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Coroutine\Channel;
|
||||
@@ -22,199 +23,199 @@ use Swoole\Timer;
|
||||
abstract class Pool extends Component
|
||||
{
|
||||
|
||||
/** @var Channel[] */
|
||||
private array $_items = [];
|
||||
/** @var Channel[] */
|
||||
private array $_items = [];
|
||||
|
||||
protected int $max = 60;
|
||||
protected int $max = 60;
|
||||
|
||||
use Timeout;
|
||||
use Timeout;
|
||||
|
||||
/**
|
||||
* @param $driver
|
||||
* @param $name
|
||||
* @param false $isMaster
|
||||
* @param int $max
|
||||
*/
|
||||
public function initConnections($driver, $name, $isMaster = false, $max = 60)
|
||||
{
|
||||
$name = $this->name($driver, $name, $isMaster);
|
||||
if (isset($this->_items[$name]) && $this->_items[$name] instanceof Channel) {
|
||||
return;
|
||||
}
|
||||
if (!Context::inCoroutine()) {
|
||||
return;
|
||||
}
|
||||
$this->_items[$name] = new Channel((int)$max);
|
||||
$this->max = (int)$max;
|
||||
}
|
||||
/**
|
||||
* @param $driver
|
||||
* @param $name
|
||||
* @param false $isMaster
|
||||
* @param int $max
|
||||
*/
|
||||
public function initConnections($driver, $name, $isMaster = false, $max = 60)
|
||||
{
|
||||
$name = $this->name($driver, $name, $isMaster);
|
||||
if (isset($this->_items[$name]) && $this->_items[$name] instanceof Channel) {
|
||||
return;
|
||||
}
|
||||
if (!Context::inCoroutine()) {
|
||||
return;
|
||||
}
|
||||
$this->_items[$name] = new Channel((int)$max);
|
||||
$this->max = (int)$max;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param array $callback
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function get($name, array $callback): mixed
|
||||
{
|
||||
if (!Context::inCoroutine()) {
|
||||
return $this->createClient($name, $callback);
|
||||
}
|
||||
if (!$this->hasItem($name)) {
|
||||
if ($this->creates === -1) {
|
||||
$this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']);
|
||||
}
|
||||
if (!Context::hasContext('create::client::ing::' . $name)) {
|
||||
$this->push($name, $this->createClient($name, $callback));
|
||||
Context::deleteContext('create::client::ing::' . $name);
|
||||
}
|
||||
}
|
||||
$connection = $this->_items[$name]->pop(-1);
|
||||
if (!$this->checkCanUse($name, $connection)) {
|
||||
return $this->createClient($name, $callback);
|
||||
} else {
|
||||
return $connection;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param $name
|
||||
* @param array $callback
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function getFromChannel($name, array $callback): mixed
|
||||
{
|
||||
if (!Context::inCoroutine()) {
|
||||
return $this->createClient($name, $callback);
|
||||
}
|
||||
if (!$this->hasItem($name)) {
|
||||
if ($this->creates === -1) {
|
||||
$this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']);
|
||||
}
|
||||
if (!Context::hasContext('create::client::ing::' . $name)) {
|
||||
$this->push($name, $this->createClient($name, $callback));
|
||||
Context::deleteContext('create::client::ing::' . $name);
|
||||
}
|
||||
}
|
||||
$connection = $this->_items[$name]->pop(-1);
|
||||
if (!$this->checkCanUse($name, $connection)) {
|
||||
return $this->createClient($name, $callback);
|
||||
} else {
|
||||
return $connection;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $cds
|
||||
* @param $coroutineName
|
||||
* @param false $isBefore
|
||||
*/
|
||||
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->_create . ']');
|
||||
}
|
||||
/**
|
||||
* @param $cds
|
||||
* @param $coroutineName
|
||||
* @param false $isBefore
|
||||
* @throws ComponentException
|
||||
*/
|
||||
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->_create . ']');
|
||||
}
|
||||
|
||||
|
||||
abstract public function createClient(string $name, array $config): mixed;
|
||||
abstract public function createClient(string $name, mixed $config): mixed;
|
||||
|
||||
|
||||
/**
|
||||
* @param $driver
|
||||
* @param $cds
|
||||
* @param false $isMaster
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] public function name($driver, $cds, $isMaster = false): string
|
||||
{
|
||||
if ($isMaster === true) {
|
||||
return $cds . '_master';
|
||||
} else {
|
||||
return $cds . '_slave';
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param $driver
|
||||
* @param $cds
|
||||
* @param false $isMaster
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] public function name($driver, $cds, $isMaster = false): string
|
||||
{
|
||||
if ($isMaster === true) {
|
||||
return $cds . '_master';
|
||||
} else {
|
||||
return $cds . '_slave';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $time
|
||||
* @param $client
|
||||
* @return mixed
|
||||
* 检查连接可靠性
|
||||
* @throws Exception
|
||||
*/
|
||||
public function checkCanUse(string $name, mixed $client): mixed
|
||||
{
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* @param string $name
|
||||
* @param $client
|
||||
* @return mixed
|
||||
* 检查连接可靠性
|
||||
*/
|
||||
public function checkCanUse(string $name, mixed $client): mixed
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @throws Exception
|
||||
*/
|
||||
public function desc(string $name)
|
||||
{
|
||||
throw new Exception('Undefined system processing function.');
|
||||
}
|
||||
/**
|
||||
* @param $name
|
||||
* @throws Exception
|
||||
*/
|
||||
public function desc(string $name)
|
||||
{
|
||||
throw new Exception('Undefined system processing function.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @param $isMaster
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getConnection(array $config, bool $isMaster)
|
||||
{
|
||||
throw new Exception('Undefined system processing function.');
|
||||
}
|
||||
/**
|
||||
* @param array $config
|
||||
* @param $isMaster
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get(mixed $config, bool $isMaster): mixed
|
||||
{
|
||||
throw new Exception('Undefined system processing function.');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasItem(string $name): bool
|
||||
{
|
||||
if (isset($this->_items[$name])) {
|
||||
return !$this->_items[$name]->isEmpty();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* @param $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasItem(string $name): bool
|
||||
{
|
||||
if (isset($this->_items[$name])) {
|
||||
return !$this->_items[$name]->isEmpty();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function size(string $name): mixed
|
||||
{
|
||||
if (!Context::inCoroutine()) {
|
||||
return 0;
|
||||
}
|
||||
if (!isset($this->_items[$name])) {
|
||||
return 0;
|
||||
}
|
||||
return $this->_items[$name]->length();
|
||||
}
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function size(string $name): mixed
|
||||
{
|
||||
if (!Context::inCoroutine()) {
|
||||
return 0;
|
||||
}
|
||||
if (!isset($this->_items[$name])) {
|
||||
return 0;
|
||||
}
|
||||
return $this->_items[$name]->length();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $client
|
||||
*/
|
||||
public function push(string $name, mixed $client)
|
||||
{
|
||||
if (!Context::inCoroutine()) {
|
||||
return;
|
||||
}
|
||||
if (!isset($this->_items[$name])) {
|
||||
$this->_items[$name] = new Channel($this->max);
|
||||
}
|
||||
if (!$this->_items[$name]->isFull()) {
|
||||
$this->_items[$name]->push($client);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param $name
|
||||
* @param $client
|
||||
*/
|
||||
public function push(string $name, mixed $client)
|
||||
{
|
||||
if (!Context::inCoroutine()) {
|
||||
return;
|
||||
}
|
||||
if (!isset($this->_items[$name])) {
|
||||
$this->_items[$name] = new Channel($this->max);
|
||||
}
|
||||
if (!$this->_items[$name]->isFull()) {
|
||||
$this->_items[$name]->push($client);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @throws Exception
|
||||
*/
|
||||
public function clean(string $name)
|
||||
{
|
||||
if (!Context::inCoroutine()) {
|
||||
return;
|
||||
}
|
||||
if (!isset($this->_items[$name])) {
|
||||
return;
|
||||
}
|
||||
$channel = $this->_items[$name];
|
||||
while ($client = $channel->pop(0.001)) {
|
||||
unset($client);
|
||||
$this->desc($name);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param string $name
|
||||
* @throws Exception
|
||||
*/
|
||||
public function clean(string $name)
|
||||
{
|
||||
if (!Context::inCoroutine()) {
|
||||
return;
|
||||
}
|
||||
if (!isset($this->_items[$name])) {
|
||||
return;
|
||||
}
|
||||
$channel = $this->_items[$name];
|
||||
while ($client = $channel->pop(0.001)) {
|
||||
unset($client);
|
||||
$this->desc($name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Channel[]
|
||||
*/
|
||||
protected function getChannels(): array
|
||||
{
|
||||
return $this->_items;
|
||||
}
|
||||
/**
|
||||
* @return Channel[]
|
||||
*/
|
||||
protected function getChannels(): array
|
||||
{
|
||||
return $this->_items;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -141,7 +141,7 @@ SCRIPT;
|
||||
{
|
||||
$connections = Snowflake::app()->getPool()->getRedis();
|
||||
|
||||
$client = $connections->getConnection($this->get_config(), true);
|
||||
$client = $connections->get($this->get_config(), true);
|
||||
if (!($client instanceof \Redis)) {
|
||||
throw new Exception('Redis connections more.');
|
||||
}
|
||||
|
||||
+13
-10
@@ -7,6 +7,7 @@ use HttpServer\Http\Context;
|
||||
use PDO;
|
||||
use Exception;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Swoole\Coroutine;
|
||||
use Snowflake\Abstracts\Pool;
|
||||
use Swoole\Timer;
|
||||
@@ -129,12 +130,12 @@ class Connection extends Pool
|
||||
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @param mixed $config
|
||||
* @param bool $isMaster
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getConnection(array $config, $isMaster = false): mixed
|
||||
public function get(mixed $config, $isMaster = false): mixed
|
||||
{
|
||||
$coroutineName = $this->name('mysql', $config['cds'], $isMaster);
|
||||
if (!isset($this->hasCreate[$coroutineName])) {
|
||||
@@ -143,7 +144,7 @@ class Connection extends Pool
|
||||
if (($pdo = Context::getContext($coroutineName)) instanceof PDO) {
|
||||
return $pdo;
|
||||
}
|
||||
$connections = $this->get($coroutineName, $config);
|
||||
$connections = $this->getFromChannel($coroutineName, $config);
|
||||
if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) {
|
||||
$number > 0 && $connections->beginTransaction();
|
||||
}
|
||||
@@ -153,10 +154,11 @@ class Connection extends Pool
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $config
|
||||
* @param mixed $config
|
||||
* @return PDO
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function createClient(string $name, array $config): PDO
|
||||
public function createClient(string $name, mixed $config): PDO
|
||||
{
|
||||
$this->printClients($config['cds'], $name, true);
|
||||
// TODO: Implement createClient() method.
|
||||
@@ -177,11 +179,12 @@ class Connection extends Pool
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $cds
|
||||
* @param $coroutineName
|
||||
* @param false $isBefore
|
||||
*/
|
||||
/**
|
||||
* @param $cds
|
||||
* @param $coroutineName
|
||||
* @param false $isBefore
|
||||
* @throws ComponentException
|
||||
*/
|
||||
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] . ']');
|
||||
|
||||
+43
-43
@@ -17,54 +17,54 @@ class ObjectPool extends \Snowflake\Abstracts\Pool
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* set pool max length
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->max = 100;
|
||||
}
|
||||
/**
|
||||
* set pool max length
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->max = 100;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @param bool $isMaster
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getConnection(array $config, bool $isMaster): mixed
|
||||
{
|
||||
if (is_object($config[0])) {
|
||||
$config[0] = get_class($config[0]) ;
|
||||
}
|
||||
return $this->get(md5($config[0]), $config);
|
||||
}
|
||||
/**
|
||||
* @param array $config
|
||||
* @param bool $isMaster
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get(mixed $config, bool $isMaster = false): mixed
|
||||
{
|
||||
if (is_object($config)) {
|
||||
return $config;
|
||||
}
|
||||
return $this->getFromChannel(md5($config), $config);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $config
|
||||
* @return mixed
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function createClient(string $name, array $config): mixed
|
||||
{
|
||||
// TODO: Implement createClient() method.
|
||||
return Snowflake::createObject(array_shift($config));
|
||||
}
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $config
|
||||
* @return mixed
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function createClient(string $name, mixed $config): mixed
|
||||
{
|
||||
// TODO: Implement createClient() method.
|
||||
return Snowflake::createObject(array_shift($config));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param $object
|
||||
*/
|
||||
public function release(string $name, mixed $object)
|
||||
{
|
||||
if (method_exists($object, 'clean')) {
|
||||
$object->clean();
|
||||
}
|
||||
$this->push($name, $object);
|
||||
}
|
||||
/**
|
||||
* @param string $name
|
||||
* @param $object
|
||||
*/
|
||||
public function release(string $name, mixed $object)
|
||||
{
|
||||
if (method_exists($object, 'clean')) {
|
||||
$object->clean();
|
||||
}
|
||||
$this->push($name, $object);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,7 +37,12 @@ class Pool extends \Snowflake\Abstracts\Pool
|
||||
}
|
||||
|
||||
|
||||
public function createClient(string $name, array $config): mixed
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $config
|
||||
* @return mixed
|
||||
*/
|
||||
public function createClient(string $name, mixed $config): mixed
|
||||
{
|
||||
// TODO: Implement createClient() method.
|
||||
return null;
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace Snowflake\Pool;
|
||||
use HttpServer\Http\Context;
|
||||
use Redis as SRedis;
|
||||
use RedisException;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\RedisConnectException;
|
||||
use Swoole\Coroutine;
|
||||
use Exception;
|
||||
@@ -33,29 +34,29 @@ class Redis extends Pool
|
||||
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @param mixed $config
|
||||
* @param bool $isMaster
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getConnection(array $config, $isMaster = false): mixed
|
||||
public function get(mixed $config, $isMaster = false): mixed
|
||||
{
|
||||
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
|
||||
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
|
||||
if (($redis = Context::getContext($coroutineName)) instanceof \Redis) {
|
||||
return $redis;
|
||||
}
|
||||
return Context::setContext($coroutineName, $this->get($coroutineName, $config));
|
||||
return Context::setContext($coroutineName, $this->getFromChannel($coroutineName, $config));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $config
|
||||
* @param mixed $config
|
||||
* @return SRedis
|
||||
* @throws RedisConnectException
|
||||
* @throws RedisConnectException|ComponentException
|
||||
*/
|
||||
public function createClient(string $name, array $config): SRedis
|
||||
public function createClient(string $name, mixed $config): SRedis
|
||||
{
|
||||
$this->printClients($config['host'], $name, true);
|
||||
$redis = new SRedis();
|
||||
|
||||
Reference in New Issue
Block a user