改名
This commit is contained in:
+44
-35
@@ -17,6 +17,7 @@ use JetBrains\PhpStorm\Pure;
|
|||||||
use PDO;
|
use PDO;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Snowflake\Abstracts\Component;
|
use Snowflake\Abstracts\Component;
|
||||||
|
use Snowflake\Abstracts\Config;
|
||||||
use Snowflake\Event;
|
use Snowflake\Event;
|
||||||
use Snowflake\Exception\NotFindClassException;
|
use Snowflake\Exception\NotFindClassException;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
@@ -106,9 +107,11 @@ class Connection extends Component
|
|||||||
public function fill()
|
public function fill()
|
||||||
{
|
{
|
||||||
$connections = $this->connections();
|
$connections = $this->connections();
|
||||||
$connections->initConnections('mysql', $this->cds, true, $this->maxNumber);
|
$pool = Config::get('databases.pool.max',10);
|
||||||
if (!empty($this->slaveConfig)) {
|
|
||||||
$connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber);
|
$connections->initConnections('mysql', $this->cds, true, $pool);
|
||||||
|
if (!empty($this->slaveConfig) && $this->cds != $this->slaveConfig['cds']) {
|
||||||
|
$connections->initConnections('mysql', $this->slaveConfig['cds'], false, $pool);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -257,13 +260,14 @@ class Connection extends Component
|
|||||||
return $instance;
|
return $instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $sql
|
* @param null $sql
|
||||||
* @param array $attributes
|
* @param string $dbname
|
||||||
* @return Command
|
* @param array $attributes
|
||||||
* @throws
|
* @return Command
|
||||||
*/
|
* @throws Exception
|
||||||
public function createCommand($sql = null, $dbname = '', array $attributes = []): Command
|
*/
|
||||||
|
public function createCommand($sql = null, string $dbname = '', array $attributes = []): Command
|
||||||
{
|
{
|
||||||
if (!empty($dbname)) {
|
if (!empty($dbname)) {
|
||||||
$sql = $this->clear($dbname, $sql);
|
$sql = $this->clear($dbname, $sql);
|
||||||
@@ -273,7 +277,12 @@ class Connection extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function clear($dbname, $sql)
|
/**
|
||||||
|
* @param $dbname
|
||||||
|
* @param $sql
|
||||||
|
* @return array|string|null
|
||||||
|
*/
|
||||||
|
private function clear($dbname, $sql): array|string|null
|
||||||
{
|
{
|
||||||
$substr = strtoupper(substr($sql, 0, 6));
|
$substr = strtoupper(substr($sql, 0, 6));
|
||||||
return match ($substr) {
|
return match ($substr) {
|
||||||
@@ -297,45 +306,45 @@ class Connection extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $dbname
|
* @param $dbname
|
||||||
* @param $sql
|
* @param $sql
|
||||||
* @return array|string|string[]|null
|
* @return array|string|null
|
||||||
*/
|
*/
|
||||||
private function innerJoinMatch($dbname, $sql)
|
private function innerJoinMatch($dbname, $sql): array|string|null
|
||||||
{
|
{
|
||||||
return preg_replace('/(INNER|LEFT|RIGHT) JOIN\s+(\w+)\s/', '$1 JOIN `' . $dbname . '`.$2 ', $sql);
|
return preg_replace('/(INNER|LEFT|RIGHT) JOIN\s+(\w+)\s/', '$1 JOIN `' . $dbname . '`.$2 ', $sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $dbname
|
* @param $dbname
|
||||||
* @param $sql
|
* @param $sql
|
||||||
* @return array|string|string[]|null
|
* @return array|string|null
|
||||||
*/
|
*/
|
||||||
private function updateMatch($dbname, $sql)
|
private function updateMatch($dbname, $sql): array|string|null
|
||||||
{
|
{
|
||||||
return preg_replace('/UPDATE\s+(\w+)\s+SET/', 'UPDATE `' . $dbname . '`.$1 SET', $sql);
|
return preg_replace('/UPDATE\s+(\w+)\s+SET/', 'UPDATE `' . $dbname . '`.$1 SET', $sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $dbname
|
* @param $dbname
|
||||||
* @param $sql
|
* @param $sql
|
||||||
* @return array|string|string[]|null
|
* @return array|string|null
|
||||||
*/
|
*/
|
||||||
private function insertMatch($dbname, $sql)
|
private function insertMatch($dbname, $sql): array|string|null
|
||||||
{
|
{
|
||||||
return preg_replace('/INSERT INTO\s+(\w+)/', 'INSERT INTO `' . $dbname . '`.$1', $sql);
|
return preg_replace('/INSERT INTO\s+(\w+)/', 'INSERT INTO `' . $dbname . '`.$1', $sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $dbname
|
* @param $dbname
|
||||||
* @param $sql
|
* @param $sql
|
||||||
* @return array|string|string[]|null
|
* @return array|string|null
|
||||||
*/
|
*/
|
||||||
private function truncateMatch($dbname, $sql)
|
private function truncateMatch($dbname, $sql): array|string|null
|
||||||
{
|
{
|
||||||
return preg_replace('/TRUNCATE\s+(\w+)\s+/', 'TRUNCATE `' . $dbname . '`.$1', $sql);
|
return preg_replace('/TRUNCATE\s+(\w+)\s+/', 'TRUNCATE `' . $dbname . '`.$1', $sql);
|
||||||
}
|
}
|
||||||
|
|||||||
+31
-16
@@ -73,17 +73,16 @@ abstract class Pool extends Component
|
|||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function Heartbeat_detection()
|
public function Heartbeat_detection(string $name)
|
||||||
{
|
{
|
||||||
if (env('state') == 'exit') {
|
if (env('state') == 'exit') {
|
||||||
Timer::clear($this->creates);
|
Timer::clear($this->creates);
|
||||||
$this->creates = -1;
|
$this->creates = -1;
|
||||||
} else if ($this->lastTime != 0) {
|
} else {
|
||||||
[$firstClear, $lastClear] = $this->getClearTime();
|
$min = Config::get('databases.pool.min', 1);
|
||||||
if ($this->lastTime + $firstClear < time()) {
|
$channel = $this->getChannel($name);
|
||||||
$this->flush(0);
|
if ($channel->length() > $min) {
|
||||||
} else if ($this->lastTime + $lastClear < time()) {
|
$this->flush($min);
|
||||||
$this->flush(2);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -147,7 +146,7 @@ abstract class Pool extends Component
|
|||||||
* @param false $isMaster
|
* @param false $isMaster
|
||||||
* @param int $max
|
* @param int $max
|
||||||
*/
|
*/
|
||||||
public function initConnections($driver, $name, $isMaster = false, $max = 60)
|
public function initConnections($driver, $name, bool $isMaster = false, int $max = 60)
|
||||||
{
|
{
|
||||||
$name = $this->name($driver, $name, $isMaster);
|
$name = $this->name($driver, $name, $isMaster);
|
||||||
if (isset(static::$_items[$name]) && static::$_items[$name] instanceof Channel) {
|
if (isset(static::$_items[$name]) && static::$_items[$name] instanceof Channel) {
|
||||||
@@ -156,8 +155,23 @@ abstract class Pool extends Component
|
|||||||
if (Coroutine::getCid() === -1) {
|
if (Coroutine::getCid() === -1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
static::$_items[$name] = new Channel((int)$max);
|
static::$_items[$name] = new Channel($max);
|
||||||
$this->max = (int)$max;
|
$this->max = $max;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $name
|
||||||
|
* @return Channel
|
||||||
|
* @throws ConfigException
|
||||||
|
*/
|
||||||
|
private function getChannel($name): Channel
|
||||||
|
{
|
||||||
|
$channel = static::$_items[$name] ?? new Channel(Config::get('databases.pool.max', 10));
|
||||||
|
if (!((static::$_items[$name] ?? null) instanceof Channel)) {
|
||||||
|
static::$_items[$name] = $channel;
|
||||||
|
}
|
||||||
|
return $channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -172,16 +186,17 @@ abstract class Pool extends Component
|
|||||||
if (Coroutine::getCid() === -1) {
|
if (Coroutine::getCid() === -1) {
|
||||||
return $this->createClient($name, $callback);
|
return $this->createClient($name, $callback);
|
||||||
}
|
}
|
||||||
$channel = static::$_items[$name] ?? new Channel($this->max);
|
|
||||||
if (!((static::$_items[$name] ?? null) instanceof Channel)) {
|
$channel = $this->getChannel($name);
|
||||||
static::$_items[$name] = $channel;
|
|
||||||
}
|
|
||||||
if (!$channel->isEmpty()) {
|
if (!$channel->isEmpty()) {
|
||||||
$connection = $channel->pop();
|
$connection = $channel->pop();
|
||||||
if ($this->checkCanUse($name, $connection)) {
|
if ($this->checkCanUse($name, $connection)) {
|
||||||
return $connection;
|
return $connection;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if ($this->creates === -1 && !is_callable($callback)) {
|
||||||
|
$this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection'], $name);
|
||||||
|
}
|
||||||
return $this->createClient($name, $callback);
|
return $this->createClient($name, $callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,7 +236,7 @@ abstract class Pool extends Component
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param $client
|
* @param mixed $client
|
||||||
* @return bool
|
* @return bool
|
||||||
* 检查连接可靠性
|
* 检查连接可靠性
|
||||||
*/
|
*/
|
||||||
@@ -233,7 +248,7 @@ abstract class Pool extends Component
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @param $isMaster
|
* @param bool $isMaster
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user