This commit is contained in:
2021-02-23 16:56:34 +08:00
parent 3cef44886d
commit 7ae8d4ffc2
10 changed files with 500 additions and 492 deletions
+169 -168
View File
@@ -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;
}
}
+1 -1
View File
@@ -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
View File
@@ -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
View File
@@ -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);
}
}
+6 -1
View File
@@ -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;
+7 -6
View File
@@ -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();