Skip to content

Commit

Permalink
feat: channel优化
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Sep 29, 2024
1 parent 15d7d48 commit af7b805
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
14 changes: 13 additions & 1 deletion src/Utils/Channel/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,28 @@
use Workbunny\WebmanCoroutine\Factory;
use Workbunny\WebmanCoroutine\Utils\Channel\Handlers\ChannelInterface;
use Workbunny\WebmanCoroutine\Utils\Channel\Handlers\DefaultChannel;
use Workbunny\WebmanCoroutine\Utils\Channel\Handlers\SwooleChannel;
use Workbunny\WebmanCoroutine\Utils\Channel\Handlers\SwowChannel;
use Workbunny\WebmanCoroutine\Utils\RegisterMethods;

class Channel
{
use RegisterMethods;

/**
* @var ChannelInterface
*/
protected ChannelInterface $_interface;

/**
* @var string[]
*/
protected static array $_handlers = [

Factory::WORKERMAN_SWOW => SwowChannel::class,
Factory::WORKBUNNY_SWOW => SwowChannel::class,
Factory::WORKERMAN_SWOOLE => SwooleChannel::class,
Factory::WORKBUNNY_SWOOLE => SwooleChannel::class,
Factory::RIPPLE_FIBER => DefaultChannel::class,
];

/**
Expand Down
11 changes: 11 additions & 0 deletions src/Utils/Channel/Handlers/ChannelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
interface ChannelInterface
{

/**
* 初始化通道
*
* @param int $capacity 初始容量
*/
public function __construct(int $capacity = -1);

/**
* 摧毁通道
*/
public function __destruct();
/**
* 获取一个数据
*
Expand Down
2 changes: 2 additions & 0 deletions src/Utils/Channel/Handlers/DefaultChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ class DefaultChannel implements ChannelInterface
/** @var int */
protected int $_capacity;

/** @inheritdoc */
public function __construct(int $capacity = -1)
{
$this->_queue = new \SplQueue();
$this->_capacity = $capacity;
}

/** @inheritdoc */
public function __destruct()
{
$this->close();
Expand Down
2 changes: 2 additions & 0 deletions src/Utils/Channel/Handlers/SwooleChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ class SwooleChannel implements ChannelInterface
/** @var Channel|null */
protected ?Channel $_channel;

/** @inheritdoc */
public function __construct(int $capacity = -1)
{
$this->_channel = new Channel($capacity);
}

/** @inheritdoc */
public function __destruct()
{
$this->close();
Expand Down
2 changes: 2 additions & 0 deletions src/Utils/Channel/Handlers/SwowChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ class SwowChannel implements ChannelInterface
/** @var Channel|null */
protected ?Channel $_channel;

/** @inheritdoc */
public function __construct(int $capacity = -1)
{
$this->_channel = new Channel($capacity);
}

/** @inheritdoc */
public function __destruct()
{
$this->close();
Expand Down
4 changes: 0 additions & 4 deletions src/Utils/RegisterMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@

trait RegisterMethods
{
/**
* @var array
*/
protected static array $_handlers = [];

/**
* 注册
Expand Down

0 comments on commit af7b805

Please sign in to comment.