Skip to content

Commit

Permalink
feat: 增加waitGroup统一封装
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Sep 29, 2024
1 parent 9942ec8 commit 15d7d48
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/Utils/WaitGroup/Handlers/DefaultWaitGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* @author workbunny/Chaz6chez
* @email [email protected]
*/
declare(strict_types=1);

namespace Workbunny\WebmanCoroutine\Utils\WaitGroup\Handlers;

class DefaultWaitGroup implements WaitGroupInterface
{
/** @var int */
protected int $_count;

/** @inheritdoc */
public function __construct()
{
$this->_count = 0;
}

/** @inheritdoc */
public function __destruct()
{
$this->_count = 0;
}

/** @inheritdoc */
public function add(int $delta = 1): bool
{
$this->_count ++;
return true;
}

/** @inheritdoc */
public function done(): bool
{
$this->_count --;
return true;
}

/** @inheritdoc */
public function wait(int $timeout = -1): void
{
$time = time();
while (1) {
if ($timeout > 0 and time() - $time >= $timeout) {
return;
}
if ($this->_count <= 0) {

return;
}
// todo sleep
}
}
}
44 changes: 44 additions & 0 deletions src/Utils/WaitGroup/Handlers/WaitGroupInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* @author workbunny/Chaz6chez
* @email [email protected]
*/
declare(strict_types=1);

namespace Workbunny\WebmanCoroutine\Utils\WaitGroup\Handlers;

interface WaitGroupInterface
{
/**
* 初始化
*/
public function __construct();

/**
* 销毁
*/
public function __destruct();

/**
* 增加一个计数
*
* @param int $delta
* @return bool
*/
public function add(int $delta = 1): bool;

/**
* 完成一个计数
*
* @return bool
*/
public function done(): bool;

/**
* 阻塞等待
*
* @param int $timeout
* @return void
*/
public function wait(int $timeout = -1): void;
}
69 changes: 69 additions & 0 deletions src/Utils/WaitGroup/WaitGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* @author workbunny/Chaz6chez
* @email [email protected]
*/
declare(strict_types=1);

namespace Workbunny\WebmanCoroutine\Utils\WaitGroup;

use Workbunny\WebmanCoroutine\Factory;
use Workbunny\WebmanCoroutine\Utils\RegisterMethods;
use Workbunny\WebmanCoroutine\Utils\WaitGroup\Handlers\DefaultWaitGroup;
use Workbunny\WebmanCoroutine\Utils\WaitGroup\Handlers\WaitGroupInterface;

class WaitGroup
{
use RegisterMethods;

/**
* @var WaitGroupInterface
*/
protected WaitGroupInterface $_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 => DefaultWaitGroup::class,
];

/**
* 构造方法
*/
public function __construct()
{
$this->_interface = new (self::$_handlers[Factory::getCurrentEventLoop()] ?? DefaultWaitGroup::class)();
}

/** @inheritdoc */
public static function registerVerify(mixed $value): false|string
{
return is_a($value, WaitGroupInterface::class) ? WaitGroupInterface::class : false;
}

/** @inheritdoc */
public static function unregisterExecute(string $key): bool
{
return true;
}

/**
* 代理调用WaitGroupInterface方法
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call(string $name, array $arguments): mixed
{
if (!method_exists($this->_interface, $name)) {
throw new \BadMethodCallException("Method $name not exists. ");
}
return $this->_interface->$name(...$arguments);
}
}

0 comments on commit 15d7d48

Please sign in to comment.