改名
This commit is contained in:
@@ -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,6 +443,18 @@ abstract class BaseApplication extends Service
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Channel
|
||||
* @throws ComponentException
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function getChannel(): Channel
|
||||
{
|
||||
return $this->get('channel');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
@@ -465,6 +478,7 @@ abstract class BaseApplication extends Service
|
||||
'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],
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user