From 21dd7c09011da0b05fb75b5b1b2f1d0cb9169a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 26 Mar 2021 15:59:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- System/Abstracts/BaseApplication.php | 54 ++++++++++------ System/Abstracts/TraitApplication.php | 2 + System/Application.php | 4 -- System/Channel.php | 88 +++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 24 deletions(-) create mode 100644 System/Channel.php diff --git a/System/Abstracts/BaseApplication.php b/System/Abstracts/BaseApplication.php index 91b7f9b8..36042264 100644 --- a/System/Abstracts/BaseApplication.php +++ b/System/Abstracts/BaseApplication.php @@ -28,6 +28,7 @@ use Annotation\Annotation as SAnnotation; use ReflectionException; use Snowflake\Async; use Snowflake\Cache\Redis; +use Snowflake\Channel; use Snowflake\Di\Service; use Snowflake\Error\ErrorHandler; use Snowflake\Error\Logger; @@ -442,32 +443,45 @@ abstract class BaseApplication extends Service } + /** + * @return Channel + * @throws ComponentException + * @throws NotFindClassException + * @throws ReflectionException + */ + public function getChannel(): Channel + { + return $this->get('channel'); + } + + /** * @throws Exception */ protected function moreComponents(): void { $this->setComponents([ - 'error' => ['class' => ErrorHandler::class], - 'event' => ['class' => Event::class], - 'connections' => ['class' => Connection::class], - 'redis_connections' => ['class' => SRedis::class], - 'pool' => ['class' => SPool::class], - 'response' => ['class' => Response::class], - 'request' => ['class' => Request::class], - 'config' => ['class' => Config::class], - 'logger' => ['class' => Logger::class], - 'attributes' => ['class' => SAnnotation::class], - 'router' => ['class' => Router::class], - 'redis' => ['class' => Redis::class], - 'jwt' => ['class' => Jwt::class], - 'async' => ['class' => Async::class], - 'filter' => ['class' => HttpFilter::class], - 'object' => ['class' => ObjectPool::class], - 'goto' => ['class' => BaseGoto::class], - 'rpc' => ['class' => \Rpc\Producer::class], - 'rpc-service' => ['class' => \Rpc\Service::class], - 'http2' => ['class' => Http2::class], + 'error' => ['class' => ErrorHandler::class], + 'event' => ['class' => Event::class], + 'connections' => ['class' => Connection::class], + 'redis_connections' => ['class' => SRedis::class], + 'pool' => ['class' => SPool::class], + 'response' => ['class' => Response::class], + 'request' => ['class' => Request::class], + 'config' => ['class' => Config::class], + 'logger' => ['class' => Logger::class], + 'attributes' => ['class' => SAnnotation::class], + 'router' => ['class' => Router::class], + 'redis' => ['class' => Redis::class], + 'jwt' => ['class' => Jwt::class], + 'async' => ['class' => Async::class], + 'filter' => ['class' => HttpFilter::class], + 'object' => ['class' => ObjectPool::class], + 'goto' => ['class' => BaseGoto::class], + 'channel' => ['class' => Channel::class], + 'rpc' => ['class' => \Rpc\Producer::class], + 'rpc-service' => ['class' => \Rpc\Service::class], + 'http2' => ['class' => Http2::class], ]); } } diff --git a/System/Abstracts/TraitApplication.php b/System/Abstracts/TraitApplication.php index 58505de4..7c0ffb46 100644 --- a/System/Abstracts/TraitApplication.php +++ b/System/Abstracts/TraitApplication.php @@ -17,6 +17,7 @@ use HttpServer\Server; use Kafka\Producer; use Snowflake\Async; use Snowflake\Cache\Redis; +use Snowflake\Channel; use Snowflake\Error\Logger; use Snowflake\Event; use Snowflake\Jwt\Jwt; @@ -48,6 +49,7 @@ use Rpc\Producer as RPCProducer; * @property \Snowflake\Crontab\Producer $crontab * @property HttpFilter $filter * @property RPCProducer $rpc + * @property Channel $channel */ trait TraitApplication { diff --git a/System/Application.php b/System/Application.php index df3bcd3f..00f88079 100644 --- a/System/Application.php +++ b/System/Application.php @@ -15,18 +15,14 @@ use Console\ConsoleProviders; use Database\DatabasesProviders; use Exception; use HttpServer\ServerProviders; -use ReflectionException; use Snowflake\Abstracts\BaseApplication; use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Input; use Snowflake\Abstracts\Kernel; use Snowflake\Crontab\CrontabProviders; -use Snowflake\Exception\ComponentException; use Snowflake\Exception\NotFindClassException; -use Snowflake\Exception\NotFindPropertyException; use stdClass; use Swoole\Timer; -use function Co\run; /** * Class Init diff --git a/System/Channel.php b/System/Channel.php new file mode 100644 index 00000000..ce69e034 --- /dev/null +++ b/System/Channel.php @@ -0,0 +1,88 @@ +channelInit($length, $name); + if ($channel->isFull()) { + return $this->addError('Channel is full.'); + } + return $channel->push($value); + } + + + /** + * @param int $length + * @param string $name + * @return bool|CChannel + */ + private function channelInit(int $length, string $name = ''): bool|CChannel + { + if ($length < 1) { + return false; + } + if (empty($name)) { + if (!($this->_channel instanceof CChannel) + || $this->_channel->close()) { + $this->_channel = new CChannel($length); + } + return $this->_channel; + } else { + if (!isset($this->_channels[$name]) || !($this->_channels[$name] instanceof CChannel)) { + $this->_channels[$name] = new CChannel($length); + } else if ($this->_channels[$name]->close()) { + $this->_channels[$name] = new CChannel($length); + } + return $this->_channels[$name]; + } + } + + + /** + * @param int $timeout + * @param string $name + * @return mixed + * @throws Exception + */ + public function pop($timeout = 1, string $name = ''): mixed + { + if ($channel = $this->channelInit(0, $name)) { + return $this->addError('Channel is full.'); + } + if (!$channel->isEmpty()) { + return $channel->pop($timeout); + } + return null; + } + + +}