This commit is contained in:
2021-05-07 17:06:10 +08:00
parent 13888f7430
commit a0061d8c1c
5 changed files with 71 additions and 72 deletions
+15 -15
View File
@@ -17,16 +17,16 @@ use Snowflake\Snowflake;
class Producer extends Component class Producer extends Component
{ {
private array $producers = []; private static array $producers = [];
private array $classAlias = []; private static array $classAlias = [];
private array $consumers = []; private static array $consumers = [];
private array $cods = []; private static array $cods = [];
/** /**
@@ -36,11 +36,11 @@ class Producer extends Component
*/ */
public function addProducer(string $name, array $handler, array $node) public function addProducer(string $name, array $handler, array $node)
{ {
$this->classAlias[$handler[0]::class] = $name; static::$classAlias[$handler[0]::class] = $name;
$this->consumers[$name] = $handler[0]; static::$consumers[$name] = $handler[0];
$this->producers[$name] = $node; static::$producers[$name] = $node;
} }
@@ -52,13 +52,13 @@ class Producer extends Component
{ {
$class = $handler[0]::class; $class = $handler[0]::class;
if (!isset($this->classAlias[$class])) { if (!isset(static::$classAlias[$class])) {
return; return;
} }
$name = $this->classAlias[$class]; $name = static::$classAlias[$class];
$this->cods[$name . '.' . $cmd] = $handler; static::$cods[$name . '.' . $cmd] = $handler;
} }
@@ -69,7 +69,7 @@ class Producer extends Component
*/ */
public function dispatch($cmd, mixed ...$params): mixed public function dispatch($cmd, mixed ...$params): mixed
{ {
$handler = $this->cods[$cmd] ?? null; $handler = static::$cods[$cmd] ?? null;
if (empty($handler)) { if (empty($handler)) {
return false; return false;
} }
@@ -84,10 +84,10 @@ class Producer extends Component
*/ */
public function get($name): mixed public function get($name): mixed
{ {
if (!isset($this->consumers[$name])) { if (!isset(static::$consumers[$name])) {
throw new Exception('Unknown rpc client.'); throw new Exception('Unknown rpc client.');
} }
return $this->consumers[$name]; return static::$consumers[$name];
} }
@@ -97,7 +97,7 @@ class Producer extends Component
public function getService(): array public function getService(): array
{ {
$array = []; $array = [];
foreach (array_keys($this->cods) as $key) { foreach (array_keys(static::$cods) as $key) {
$explode = explode('.', $key); $explode = explode('.', $key);
$prefix = array_shift($explode); $prefix = array_shift($explode);
@@ -119,7 +119,7 @@ class Producer extends Component
*/ */
public function getClient(string $name, string $host = null): Client public function getClient(string $name, string $host = null): Client
{ {
$producer = $this->producers[$name] ?? null; $producer = static::$producers[$name] ?? null;
if ($producer === null) { if ($producer === null) {
throw new Exception('Unknown rpc client config.'); throw new Exception('Unknown rpc client config.');
} }
+33 -33
View File
@@ -20,7 +20,7 @@ abstract class Pool extends Component
{ {
/** @var Channel[] */ /** @var Channel[] */
private array $_items = []; private static array $_items = [];
public int $max = 60; public int $max = 60;
@@ -29,7 +29,7 @@ abstract class Pool extends Component
public int $lastTime = 0; public int $lastTime = 0;
protected array $hasCreate = []; protected static array $hasCreate = [];
/** /**
@@ -37,10 +37,10 @@ abstract class Pool extends Component
*/ */
public function increment(string $name) public function increment(string $name)
{ {
if (!isset($this->hasCreate[$name])) { if (!isset(static::$hasCreate[$name])) {
$this->hasCreate[$name] = 0; static::$hasCreate[$name] = 0;
} }
$this->hasCreate[$name] += 1; static::$hasCreate[$name] += 1;
} }
@@ -49,13 +49,13 @@ abstract class Pool extends Component
*/ */
public function decrement(string $name) public function decrement(string $name)
{ {
if (!isset($this->hasCreate[$name])) { if (!isset(static::$hasCreate[$name])) {
return; return;
} }
if ($this->hasCreate[$name] <= 0) { if (static::$hasCreate[$name] <= 0) {
return; return;
} }
$this->hasCreate[$name] -= 1; static::$hasCreate[$name] -= 1;
} }
@@ -101,7 +101,7 @@ abstract class Pool extends Component
$names[] = $name; $names[] = $name;
$this->pop($channel, $name, $retain_number); $this->pop($channel, $name, $retain_number);
} }
$this->_items = []; static::$_items = [];
if ($retain_number == 0) { if ($retain_number == 0) {
Timer::clear($this->creates); Timer::clear($this->creates);
$this->creates = -1; $this->creates = -1;
@@ -114,10 +114,10 @@ abstract class Pool extends Component
*/ */
protected function clearCreateLog($name): void protected function clearCreateLog($name): void
{ {
if (!isset($this->hasCreate[$name])) { if (!isset(static::$hasCreate[$name])) {
return; return;
} }
$this->hasCreate[$name] = 0; static::$hasCreate[$name] = 0;
} }
@@ -151,13 +151,13 @@ abstract class Pool extends Component
public function initConnections($driver, $name, $isMaster = false, $max = 60) public function initConnections($driver, $name, $isMaster = false, $max = 60)
{ {
$name = $this->name($driver, $name, $isMaster); $name = $this->name($driver, $name, $isMaster);
if (isset($this->_items[$name]) && $this->_items[$name] instanceof Channel) { if (isset(static::$_items[$name]) && static::$_items[$name] instanceof Channel) {
return; return;
} }
if (Coroutine::getCid() === -1) { if (Coroutine::getCid() === -1) {
return; return;
} }
$this->_items[$name] = new Channel((int)$max); static::$_items[$name] = new Channel((int)$max);
$this->max = (int)$max; $this->max = (int)$max;
} }
@@ -173,13 +173,13 @@ abstract class Pool extends Component
if (Coroutine::getCid() === -1) { if (Coroutine::getCid() === -1) {
return $this->createClient($name, $callback); return $this->createClient($name, $callback);
} }
if (!isset($this->_items[$name])) { if (!isset(static::$_items[$name])) {
$this->_items[$name] = new Channel($this->max); static::$_items[$name] = new Channel($this->max);
} }
if ($this->_items[$name]->isEmpty()) { if (static::$_items[$name]->isEmpty()) {
$this->createByCallback($name, $callback); $this->createByCallback($name, $callback);
} }
$connection = $this->_items[$name]->pop(); $connection = static::$_items[$name]->pop();
if (!$this->checkCanUse($name, $connection)) { if (!$this->checkCanUse($name, $connection)) {
return $this->createClient($name, $callback); return $this->createClient($name, $callback);
} else { } else {
@@ -198,7 +198,7 @@ abstract class Pool extends Component
if ($this->creates === -1 && !is_callable($callback)) { if ($this->creates === -1 && !is_callable($callback)) {
$this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']); $this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']);
} }
$this->_items[$name]->push($this->createClient($name, $callback)); static::$_items[$name]->push($this->createClient($name, $callback));
} }
@@ -251,10 +251,10 @@ abstract class Pool extends Component
*/ */
public function canCreate(string $name): bool public function canCreate(string $name): bool
{ {
if (!isset($this->hasCreate[$name])) { if (!isset(static::$hasCreate[$name])) {
$this->hasCreate[$name] = 0; static::$hasCreate[$name] = 0;
} }
return $this->hasCreate[$name] < $this->max; return static::$hasCreate[$name] < $this->max;
} }
@@ -264,8 +264,8 @@ abstract class Pool extends Component
*/ */
public function hasItem(string $name): bool public function hasItem(string $name): bool
{ {
if (isset($this->_items[$name])) { if (isset(static::$_items[$name])) {
return !$this->_items[$name]->isEmpty(); return !static::$_items[$name]->isEmpty();
} }
return false; return false;
} }
@@ -280,10 +280,10 @@ abstract class Pool extends Component
if (Coroutine::getCid() === -1) { if (Coroutine::getCid() === -1) {
return 0; return 0;
} }
if (!isset($this->_items[$name])) { if (!isset(static::$_items[$name])) {
return 0; return 0;
} }
return $this->_items[$name]->length(); return static::$_items[$name]->length();
} }
@@ -296,11 +296,11 @@ abstract class Pool extends Component
if (Coroutine::getCid() === -1) { if (Coroutine::getCid() === -1) {
return; return;
} }
if (!isset($this->_items[$name])) { if (!isset(static::$_items[$name])) {
$this->_items[$name] = new Channel($this->max); static::$_items[$name] = new Channel($this->max);
} }
if (!$this->_items[$name]->isFull()) { if (!static::$_items[$name]->isFull()) {
$this->_items[$name]->push($client); static::$_items[$name]->push($client);
} }
unset($client); unset($client);
} }
@@ -312,15 +312,15 @@ abstract class Pool extends Component
*/ */
public function clean(string $name) public function clean(string $name)
{ {
if (Coroutine::getCid() === -1 || !isset($this->_items[$name])) { if (Coroutine::getCid() === -1 || !isset(static::$_items[$name])) {
return; return;
} }
$channel = $this->_items[$name]; $channel = static::$_items[$name];
$this->pop($channel, $name, 0); $this->pop($channel, $name, 0);
if ($this->creates > -1) { if ($this->creates > -1) {
Timer::clear($this->creates); Timer::clear($this->creates);
} }
$this->_items[$name] = null; static::$_items[$name] = null;
} }
@@ -329,7 +329,7 @@ abstract class Pool extends Component
*/ */
protected function getChannels(): array protected function getChannels(): array
{ {
return $this->_items; return static::$_items;
} }
+7 -7
View File
@@ -20,7 +20,7 @@ class Aop extends Component
{ {
private array $_aop = []; private static array $_aop = [];
/** /**
@@ -31,13 +31,13 @@ class Aop extends Component
{ {
[$class, $method] = $handler; [$class, $method] = $handler;
$alias = $class::class . '::' . $method; $alias = $class::class . '::' . $method;
if (!isset($this->_aop[$alias])) { if (!isset(static::$_aop[$alias])) {
$this->_aop[$alias] = []; static::$_aop[$alias] = [];
} }
if (in_array($aspect, $this->_aop[$alias])) { if (in_array($aspect, static::$_aop[$alias])) {
return; return;
} }
$this->_aop[$alias][] = $aspect; static::$_aop[$alias][] = $aspect;
} }
@@ -55,7 +55,7 @@ class Aop extends Component
return call_user_func($handler, ...$params); return call_user_func($handler, ...$params);
} }
$aopName = $handler[0]::class . '::' . $handler[1]; $aopName = $handler[0]::class . '::' . $handler[1];
if (!isset($this->_aop[$aopName])) { if (!isset(static::$_aop[$aopName])) {
return $this->notFound($handler, $params); return $this->notFound($handler, $params);
} }
return $this->invoke($handler, $params, $aopName); return $this->invoke($handler, $params, $aopName);
@@ -73,7 +73,7 @@ class Aop extends Component
*/ */
private function invoke($handler, $params, $aopName): mixed private function invoke($handler, $params, $aopName): mixed
{ {
$reflect = Snowflake::getDi()->getReflect(current($this->_aop[$aopName])); $reflect = Snowflake::getDi()->getReflect(current(static::$_aop[$aopName]));
if (!$reflect->isInstantiable() || !$reflect->hasMethod('invoke')) { if (!$reflect->isInstantiable() || !$reflect->hasMethod('invoke')) {
throw new Exception(ASPECT_ERROR . IAspect::class); throw new Exception(ASPECT_ERROR . IAspect::class);
} }
+4 -4
View File
@@ -17,7 +17,7 @@ class Async extends Component
{ {
private array $_absences = []; private static array $_absences = [];
/** /**
@@ -26,7 +26,7 @@ class Async extends Component
*/ */
public function addAsync(string $name, Task $handler) public function addAsync(string $name, Task $handler)
{ {
$this->_absences[$name] = $handler::class; static::$_absences[$name] = $handler::class;
} }
@@ -42,12 +42,12 @@ class Async extends Component
return; return;
} }
if (!isset($this->_absences[$name])) { if (!isset(static::$_absences[$name])) {
return; return;
} }
/** @var Task $class */ /** @var Task $class */
$class = new $this->_absences[$name](); $class = Snowflake::createObject(static::$_absences[$name]);
$class->setParams($params); $class->setParams($params);
$randWorkerId = random_int(0, $server->setting['task_worker_num'] - 1); $randWorkerId = random_int(0, $server->setting['task_worker_num'] - 1);
+12 -13
View File
@@ -18,7 +18,7 @@ class Channel extends Component
{ {
private array $_channels = []; private static array $_channels = [];
/** /**
@@ -42,10 +42,10 @@ class Channel extends Component
*/ */
private function channelInit(string $name = ''): bool|SplQueue private function channelInit(string $name = ''): bool|SplQueue
{ {
if (!isset($this->_channels[$name]) || !($this->_channels[$name] instanceof SplQueue)) { if (!isset(static::$_channels[$name]) || !(static::$_channels[$name] instanceof SplQueue)) {
$this->_channels[$name] = new SplQueue(); static::$_channels[$name] = new SplQueue();
} }
return $this->_channels[$name]; return static::$_channels[$name];
} }
@@ -56,7 +56,7 @@ class Channel extends Component
public function cleanAll() public function cleanAll()
{ {
/** @var SplQueue $channel */ /** @var SplQueue $channel */
foreach ($this->_channels as $channel) { foreach (static::$_channels as $channel) {
if (!($channel instanceof SplQueue)) { if (!($channel instanceof SplQueue)) {
continue; continue;
} }
@@ -64,16 +64,15 @@ class Channel extends Component
$channel->dequeue(); $channel->dequeue();
} }
} }
$this->_channels = []; static::$_channels = [];
} }
/**
* @param $timeout /**
* @param Closure $closure * @param string $name
* @param string $name * @param Closure $closure
* @return mixed * @return mixed
* @throws Exception */
*/
public function pop(string $name, Closure $closure): mixed public function pop(string $name, Closure $closure): mixed
{ {
$channel = $this->channelInit($name); $channel = $this->channelInit($name);