From c6d815ffcb22ea8665c0c718956c9dfef8d62b60 Mon Sep 17 00:00:00 2001 From: chaz6chez Date: Tue, 1 Oct 2024 02:24:30 +0800 Subject: [PATCH] update function-apis doc --- ...ny-WebmanCoroutine-CoroutineWebServer.html | 251 +++++++++++++----- ...ny-WebmanCoroutine-Events-SwooleEvent.html | 44 +-- ...unny-WebmanCoroutine-Events-SwowEvent.html | 44 +-- .../Workbunny-WebmanCoroutine-Factory.html | 132 +++++++-- docs/files/src/CoroutineWebServer.php.txt | 118 +++++++- docs/files/src/Events/SwooleEvent.php.txt | 5 +- docs/files/src/Events/SwowEvent.php.txt | 5 +- docs/files/src/Factory.php.txt | 48 ++-- docs/files/src/Handlers/SwooleHandler.php.txt | 1 + docs/files/src/Handlers/SwowHandler.php.txt | 3 +- docs/js/searchIndex.js | 30 +++ test.php | 64 +++++ 12 files changed, 605 insertions(+), 140 deletions(-) create mode 100644 test.php diff --git a/docs/classes/Workbunny-WebmanCoroutine-CoroutineWebServer.html b/docs/classes/Workbunny-WebmanCoroutine-CoroutineWebServer.html index e874d0e..bfaf977 100644 --- a/docs/classes/Workbunny-WebmanCoroutine-CoroutineWebServer.html +++ b/docs/classes/Workbunny-WebmanCoroutine-CoroutineWebServer.html @@ -110,13 +110,7 @@

- - implements - CoroutineServerInterface - - uses - CoroutineServerMethods

@@ -127,9 +121,9 @@

协程化web服务进程

@@ -150,13 +144,6 @@

-

- Interfaces - - -

-
-
CoroutineServerInterface
自定义服务代理协程化接口
@@ -171,11 +158,24 @@

- onMessage() + onClose() + +  : void +
+
连接关闭
+ +
+ onConnect() + +  : void +
+
连接建立
+ +
+ onMessage() -  : mixed|null +  : null
-
重写onMessage方法
onWorkerStart() @@ -184,11 +184,11 @@

- parentOnMessage() + onWorkerStop() -  : mixed +  : void
-
父类onMessage代理
+
停止服务
@@ -210,40 +210,160 @@

-public " > -

- onMessage() - +

+ onClose() +

-

重写onMessage方法

+

连接关闭

public - onMessage(mixed $connection, mixed $request) : mixed|null + onClose(ConnectionInterface|mixed $connection, mixed ...$params) : void
+
    +
  • 不用返回值和参数标定是为了兼容
  • +
+
+ +
Parameters
+
+
+ $connection + : ConnectionInterface|mixed +
+
+ +
+
+ $params + : mixed +
+
+ +
+
+ + + + + + +
+

+ onConnect() + + +

+ + +

连接建立

+ + + public + onConnect(ConnectionInterface $connection, mixed ...$params) : void + +
+
+ +
    +
  • 不用返回值和参数标定是为了兼容
  • +
+
+
Parameters
$connection + : ConnectionInterface +
+
+ +
+
+ $params : mixed
+
+
+ + + + + + +
+
+

+ onMessage() + + +

+ + + + + public + onMessage(ConnectionInterface|mixed $connection, Request|mixed $request, mixed ...$params) : null + +
+
+ + +
Parameters
+
+
+ $connection + : ConnectionInterface|mixed +
+
+
$request + : Request|mixed +
+
+ +
+
+ $params : mixed
@@ -254,7 +374,7 @@
Parameters
Tags - +
@@ -262,7 +382,14 @@
link
- CoroutineServerInterface::onMessage() + parent::onMessage() + +
+
+ link +
+
+ parent::onMessage()
@@ -271,7 +398,7 @@
Return values
- mixed|null + null
@@ -289,9 +416,9 @@

@@ -340,40 +467,44 @@

-public " > -

- parentOnMessage() - +

+ onWorkerStop() +

-

父类onMessage代理

+

停止服务

public - parentOnMessage(mixed $connection, mixed $request) : mixed + onWorkerStop(Worker|mixed $worker, mixed ...$params) : void
- +
    +
  • 不用返回值和参数标定是为了兼容
  • +
+
+
Parameters
- $connection - : mixed + $worker + : Worker|mixed
- $request + $params : mixed
@@ -382,28 +513,6 @@
Parameters
-
- Tags - - -
-
-
- link -
-
- CoroutineServerInterface::parentOnMessage() - -
-
- link -
-
-

例:web服务的父类方法

-
- -
-
@@ -522,9 +631,11 @@
  • Methods
  • diff --git a/docs/classes/Workbunny-WebmanCoroutine-Events-SwooleEvent.html b/docs/classes/Workbunny-WebmanCoroutine-Events-SwooleEvent.html index 2da193a..9088990 100644 --- a/docs/classes/Workbunny-WebmanCoroutine-Events-SwooleEvent.html +++ b/docs/classes/Workbunny-WebmanCoroutine-Events-SwooleEvent.html @@ -234,21 +234,33 @@

    public - __construct() : mixed + __construct([bool $debug = false ]) : mixed
    - +

    Parameters
    +
    +
    + $debug + : bool + = false
    +
    +

    测试用

    +
    + +
    +
    +
    Tags @@ -286,9 +298,9 @@

    @@ -366,9 +378,9 @@

    @@ -415,9 +427,9 @@

    @@ -481,9 +493,9 @@

    @@ -530,9 +542,9 @@

    @@ -579,9 +591,9 @@

    diff --git a/docs/classes/Workbunny-WebmanCoroutine-Events-SwowEvent.html b/docs/classes/Workbunny-WebmanCoroutine-Events-SwowEvent.html index 703256b..8d1218c 100644 --- a/docs/classes/Workbunny-WebmanCoroutine-Events-SwowEvent.html +++ b/docs/classes/Workbunny-WebmanCoroutine-Events-SwowEvent.html @@ -234,21 +234,33 @@

    public - __construct() : mixed + __construct([bool $debug = false ]) : mixed
    - +

    Parameters
    +
    +
    + $debug + : bool + = false
    +
    +

    测试用

    +
    + +
    +
    +
    Tags @@ -286,9 +298,9 @@

    @@ -366,9 +378,9 @@

    @@ -415,9 +427,9 @@

    @@ -481,9 +493,9 @@

    @@ -530,9 +542,9 @@

    @@ -579,9 +591,9 @@

    diff --git a/docs/classes/Workbunny-WebmanCoroutine-Factory.html b/docs/classes/Workbunny-WebmanCoroutine-Factory.html index 8c6453e..42b576e 100644 --- a/docs/classes/Workbunny-WebmanCoroutine-Factory.html +++ b/docs/classes/Workbunny-WebmanCoroutine-Factory.html @@ -218,6 +218,12 @@

    获取所有事件处理器
    +
    + getCurrentEventLoop() + +  : string|null +
    +
    getCurrentHandler() @@ -225,6 +231,13 @@

    获取当前使用的处理器类名
    +
    + init() + +  : void +
    +
    初始化
    +
    register() @@ -480,9 +493,9 @@

    根据当前环境获取可用的处理器

    @@ -536,9 +549,9 @@

    根据事件循环类获取对应处理器

    @@ -606,9 +619,9 @@

    获取所有事件处理器

    @@ -631,6 +644,45 @@

    Return values
    array<string|int, string> + +
    +

    + getCurrentEventLoop() + + +

    + + + + + public + static getCurrentEventLoop() : string|null + +
    +
    + + + + + + + +
    +
    Return values
    + string|null +
    +

    获取当前使用的处理器类名

    @@ -671,6 +723,52 @@
    Return values
    string|null +
    +
    +

    + init() + + +

    + + +

    初始化

    + + + public + static init(string|null $eventLoopClass) : void + +
    +
    + + +
    Parameters
    +
    +
    + $eventLoopClass + : string|null +
    +
    + +
    +
    + + + + + +

    注册事件处理器

    @@ -751,9 +849,9 @@

    根据当前环境运行处理器

    @@ -826,9 +924,9 @@

    根据当前环境运行处理器

    @@ -892,9 +990,9 @@

    注销事件处理器

    @@ -1062,7 +1160,9 @@

    Return values
  • find()
  • get()
  • getAll()
  • +
  • getCurrentEventLoop()
  • getCurrentHandler()
  • +
  • init()
  • register()
  • run()
  • start()
  • diff --git a/docs/files/src/CoroutineWebServer.php.txt b/docs/files/src/CoroutineWebServer.php.txt index 7bb1299..adc16c2 100644 --- a/docs/files/src/CoroutineWebServer.php.txt +++ b/docs/files/src/CoroutineWebServer.php.txt @@ -8,13 +8,28 @@ declare(strict_types=1); namespace Workbunny\WebmanCoroutine; use Webman\App; +use Webman\Http\Request; +use Workbunny\WebmanCoroutine\Utils\Channel\Channel; +use Workbunny\WebmanCoroutine\Utils\Coroutine\Coroutine; +use Workbunny\WebmanCoroutine\Utils\WaitGroup\WaitGroup; +use Workerman\Connection\ConnectionInterface; +use Workerman\Worker; /** * 协程化web服务进程 */ -class CoroutineWebServer extends App implements CoroutineServerInterface +class CoroutineWebServer extends App { - use CoroutineServerMethods; + + /** + * @var Channel[] + */ + protected array $_connectionChannels = []; + + /** + * @var Coroutine|null + */ + protected ?Coroutine $_coroutine = null; /** @inheritdoc */ public function onWorkerStart($worker) @@ -22,6 +37,105 @@ class CoroutineWebServer extends App implements CoroutineServerInterface if (!\config('plugin.workbunny.webman-coroutine.app.enable', false)) { return; } + $this->_coroutine = new Coroutine(); parent::onWorkerStart($worker); } + + /** + * 停止服务 + * + * - 不用返回值和参数标定是为了兼容 + * + * @param Worker|mixed $worker + * @return void + */ + public function onWorkerStop($worker, ...$params) + { + if (is_callable($call = [parent::class, 'onWorkerStop'])) { + call_user_func($call, $worker, ...$params); + } + } + + /** + * 连接建立 + * + * - 不用返回值和参数标定是为了兼容 + * + * @param ConnectionInterface $connection + * @param mixed ...$params + * @return void + */ + public function onConnect($connection, ...$params): void + { + if (!is_object($connection)) { + return; + } + if (is_callable($call = [parent::class, 'onConnect'])) { + call_user_func($call, $connection, ...$params); + } + if (!$this->_connectionChannels[$id = spl_object_hash($connection)] ?? null) { + $this->_connectionChannels[$id] = new Channel(\config('plugin.workbunny.webman-coroutine.app.channel_size', 1)); + } + } + + /** + * 连接关闭 + * + * - 不用返回值和参数标定是为了兼容 + * + * @param ConnectionInterface|mixed $connection + * @param ...$params + * @return void + */ + public function onClose($connection, ...$params) + { + if (!is_object($connection)) { + return; + } + if (is_callable($call = [parent::class, 'onClose'])) { + call_user_func($call, $connection, ...$params); + } + unset($this->_connectionChannels[spl_object_hash($connection)]); + } + + /** + * @link parent::onMessage() + * @param ConnectionInterface|mixed $connection + * @param Request|mixed $request + * @param ...$params + * @return null + * @link parent::onMessage() + */ + public function onMessage($connection, $request, ...$params) + { + if (!is_object($connection)) { + return null; + } + // 为每一个连接创建一个通道 + $channel = $this->_connectionChannels[spl_object_hash($connection)]; + // 请求生产 + $channel->push(func_get_args()); + $waitGroup = new WaitGroup(); + // 根据request consumer数量创建协程 + $consumerCount = config('plugin.workbunny.webman-coroutine.app.consumer_count', 1); + foreach (range(1, $consumerCount) as $ignored) { + $waitGroup->add(); + // 请求消费协程 + $this->_coroutine->create(function () use ($channel, $waitGroup) { + while (true) { + // 通道为空或者关闭时退出协程 + if ( + $channel->isEmpty() or + !$data = $channel->pop() + ) { + break; + } + parent::onMessage(...$data); + } + $waitGroup->done(); + }); + } + $waitGroup->wait(); + return null; + } } diff --git a/docs/files/src/Events/SwooleEvent.php.txt b/docs/files/src/Events/SwooleEvent.php.txt index 74f644c..146576b 100644 --- a/docs/files/src/Events/SwooleEvent.php.txt +++ b/docs/files/src/Events/SwooleEvent.php.txt @@ -32,11 +32,12 @@ class SwooleEvent implements EventInterface protected int $_timerId = 0; /** + * @param bool $debug 测试用 * @throws EventLoopException 如果没有启用拓展 */ - public function __construct() + public function __construct(bool $debug = false) { - if (!extension_loaded('swoole')) { + if (!$debug and !extension_loaded('swoole')) { throw new EventLoopException('Not support ext-swoole. '); } } diff --git a/docs/files/src/Events/SwowEvent.php.txt b/docs/files/src/Events/SwowEvent.php.txt index ca571ea..c42f2b4 100644 --- a/docs/files/src/Events/SwowEvent.php.txt +++ b/docs/files/src/Events/SwowEvent.php.txt @@ -35,11 +35,12 @@ class SwowEvent implements EventInterface protected null|WaitGroup $_waitGroup = null; /** + * @param bool $debug 测试用 * @throws EventLoopException 如果没有启用拓展 */ - public function __construct() + public function __construct(bool $debug = false) { - if (!extension_loaded('swow')) { + if (!$debug and !extension_loaded('swow')) { throw new EventLoopException('Not support ext-swow. '); } } diff --git a/docs/files/src/Factory.php.txt b/docs/files/src/Factory.php.txt index d98de29..ee05131 100644 --- a/docs/files/src/Factory.php.txt +++ b/docs/files/src/Factory.php.txt @@ -46,11 +46,19 @@ class Factory ]; /** - * 当前的处理器 + * 当前的事件循环 * * @var string|null */ - protected static ?string $_currentHandler = null; + protected static ?string $_currentEventLoop = null; + + /** + * @return string|null + */ + public static function getCurrentEventLoop(): ?string + { + return self::$_currentEventLoop; + } /** * 获取当前使用的处理器类名 @@ -59,7 +67,8 @@ class Factory */ public static function getCurrentHandler(): ?string { - return self::$_currentHandler; + return self::$_handlers[self::getCurrentEventLoop()] ?? + (self::getCurrentEventLoop() === null ? DefaultHandler::class : null); } /** @@ -156,6 +165,23 @@ class Factory return $returnEventLoopClass ? self::WORKERMAN_DEFAULT : DefaultHandler::class; } + /** + * 初始化 + * + * @param string|null $eventLoopClass + * @return void + */ + public static function init(?string $eventLoopClass): void + { + if (!self::$_currentEventLoop) { + // 赋值,避免重复获取 + self::$_currentEventLoop = ( + // 如果没有就自动获取 + $eventLoopClass ? self::get($eventLoopClass, true, true) : self::find(true) + ); + } + } + /** * 根据当前环境运行处理器 * @@ -167,14 +193,10 @@ class Factory */ public static function run(CoroutineServerInterface $app, mixed $connection, mixed $request, ?string $eventLoopClass = null): mixed { + self::init($eventLoopClass); // 获取当前处理器 /** @var HandlerInterface $handlerClass */ - $handlerClass = self::getCurrentHandler() ?: - // 赋值,避免重复获取 - self::$_currentHandler = ( - // 如果没有就自动获取 - $eventLoopClass ? self::get($eventLoopClass, true) : self::find() - ); + $handlerClass = self::getCurrentHandler(); return $handlerClass::onMessage($app, $connection, $request); } @@ -189,14 +211,10 @@ class Factory */ public static function start(CoroutineWorkerInterface $app, mixed $worker = null, ?string $eventLoopClass = null): mixed { + self::init($eventLoopClass); // 获取当前处理器 /** @var HandlerInterface $handlerClass */ - $handlerClass = self::getCurrentHandler() ?: - // 赋值,避免重复获取 - self::$_currentHandler = ( - // 如果没有就自动获取 - $eventLoopClass ? self::get($eventLoopClass, true) : self::find() - ); + $handlerClass = self::getCurrentHandler(); return $handlerClass::onWorkerStart($app, $worker); } diff --git a/docs/files/src/Handlers/SwooleHandler.php.txt b/docs/files/src/Handlers/SwooleHandler.php.txt index 7b2f6b3..2540125 100644 --- a/docs/files/src/Handlers/SwooleHandler.php.txt +++ b/docs/files/src/Handlers/SwooleHandler.php.txt @@ -195,5 +195,6 @@ class SwooleHandler implements HandlerInterface throw new HandlerException("WaitGroup $waitGroupId not found [coroutine create]. "); } $waitGroup->wait($timeout); + unset(self::$_waitGroups[$waitGroupId]); } } diff --git a/docs/files/src/Handlers/SwowHandler.php.txt b/docs/files/src/Handlers/SwowHandler.php.txt index 61a79b8..e721259 100644 --- a/docs/files/src/Handlers/SwowHandler.php.txt +++ b/docs/files/src/Handlers/SwowHandler.php.txt @@ -157,7 +157,7 @@ class SwowHandler implements HandlerInterface public static function waitGroupCreate(): string { self::$_waitGroups[ - $id = spl_object_hash($waitGroup = new WaitGroup()) + $id = spl_object_hash($waitGroup = new WaitGroup()) ] = $waitGroup; return $id; @@ -175,5 +175,6 @@ class SwowHandler implements HandlerInterface throw new HandlerException("WaitGroup $waitGroupId not found [coroutine create]. "); } $waitGroup->wait($timeout); + unset(self::$_waitGroups[$waitGroupId]); } } diff --git a/docs/js/searchIndex.js b/docs/js/searchIndex.js index 40abe43..ba48af6 100644 --- a/docs/js/searchIndex.js +++ b/docs/js/searchIndex.js @@ -40,6 +40,26 @@ Search.appendIndex( "name": "onWorkerStart", "summary": "", "url": "classes/Workbunny-WebmanCoroutine-CoroutineWebServer.html#method_onWorkerStart" + }, { + "fqsen": "\\Workbunny\\WebmanCoroutine\\CoroutineWebServer\u003A\u003AonWorkerStop\u0028\u0029", + "name": "onWorkerStop", + "summary": "\u505C\u6B62\u670D\u52A1", + "url": "classes/Workbunny-WebmanCoroutine-CoroutineWebServer.html#method_onWorkerStop" + }, { + "fqsen": "\\Workbunny\\WebmanCoroutine\\CoroutineWebServer\u003A\u003AonConnect\u0028\u0029", + "name": "onConnect", + "summary": "\u8FDE\u63A5\u5EFA\u7ACB", + "url": "classes/Workbunny-WebmanCoroutine-CoroutineWebServer.html#method_onConnect" + }, { + "fqsen": "\\Workbunny\\WebmanCoroutine\\CoroutineWebServer\u003A\u003AonClose\u0028\u0029", + "name": "onClose", + "summary": "\u8FDE\u63A5\u5173\u95ED", + "url": "classes/Workbunny-WebmanCoroutine-CoroutineWebServer.html#method_onClose" + }, { + "fqsen": "\\Workbunny\\WebmanCoroutine\\CoroutineWebServer\u003A\u003AonMessage\u0028\u0029", + "name": "onMessage", + "summary": "", + "url": "classes/Workbunny-WebmanCoroutine-CoroutineWebServer.html#method_onMessage" }, { "fqsen": "\\Workbunny\\WebmanCoroutine\\CoroutineWorkerInterface", "name": "CoroutineWorkerInterface", @@ -175,6 +195,11 @@ Search.appendIndex( "name": "Factory", "summary": "\u5DE5\u5382\u5316\u542F\u52A8\u5668", "url": "classes/Workbunny-WebmanCoroutine-Factory.html" + }, { + "fqsen": "\\Workbunny\\WebmanCoroutine\\Factory\u003A\u003AgetCurrentEventLoop\u0028\u0029", + "name": "getCurrentEventLoop", + "summary": "", + "url": "classes/Workbunny-WebmanCoroutine-Factory.html#method_getCurrentEventLoop" }, { "fqsen": "\\Workbunny\\WebmanCoroutine\\Factory\u003A\u003AgetCurrentHandler\u0028\u0029", "name": "getCurrentHandler", @@ -205,6 +230,11 @@ Search.appendIndex( "name": "find", "summary": "\u6839\u636E\u5F53\u524D\u73AF\u5883\u83B7\u53D6\u53EF\u7528\u7684\u5904\u7406\u5668", "url": "classes/Workbunny-WebmanCoroutine-Factory.html#method_find" + }, { + "fqsen": "\\Workbunny\\WebmanCoroutine\\Factory\u003A\u003Ainit\u0028\u0029", + "name": "init", + "summary": "\u521D\u59CB\u5316", + "url": "classes/Workbunny-WebmanCoroutine-Factory.html#method_init" }, { "fqsen": "\\Workbunny\\WebmanCoroutine\\Factory\u003A\u003Arun\u0028\u0029", "name": "run", diff --git a/test.php b/test.php new file mode 100644 index 0000000..b42343e --- /dev/null +++ b/test.php @@ -0,0 +1,64 @@ +add(3); +$timeOne = microtime(true); +\Swow\Coroutine::run(function () use ($wg) { + task1(); + $wg->done(); +}); +\Swow\Coroutine::run(function () use ($wg) { + task2(); + $wg->done(); +}); +\Swow\Coroutine::run(function () use ($wg) { + task3(); + $wg->done(); +}); +// 等待协程完毕 +$wg->wait(); +$timeTwo = microtime(true); + +echo '[x] [运行时间] ' . ($timeTwo - $timeOne) . PHP_EOL; \ No newline at end of file