This commit is contained in:
2021-03-26 15:59:39 +08:00
parent 3f2ea33535
commit 21dd7c0901
4 changed files with 124 additions and 24 deletions
+34 -20
View File
@@ -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],
]);
}
}
+2
View File
@@ -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
{
-4
View File
@@ -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
+88
View File
@@ -0,0 +1,88 @@
<?php
namespace Snowflake;
use Exception;
use Snowflake\Abstracts\Component;
use Swoole\Coroutine\Channel as CChannel;
/**
* Class Channel
* @package Snowflake
*/
class Channel extends Component
{
private ?CChannel $_channel = null;
private array $_channels = [];
/**
* @param mixed $value
* @param string $name
* @param int $length
* @return mixed
* @throws Exception
*/
public function push(mixed $value, string $name = '', $length = 999): mixed
{
$channel = $this->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;
}
}