From 27b20e2a5b7c27ebc6ddc32af98da8365e157c27 Mon Sep 17 00:00:00 2001 From: Reasno Date: Mon, 3 Feb 2020 00:11:20 +0800 Subject: [PATCH 1/9] Add setTimeout & Cancel API --- src/utils/src/Context.php | 49 ++++++++++++++++ src/utils/tests/ContextTest.php | 101 ++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) diff --git a/src/utils/src/Context.php b/src/utils/src/Context.php index 6cec5b605..99a14fba5 100644 --- a/src/utils/src/Context.php +++ b/src/utils/src/Context.php @@ -12,10 +12,13 @@ declare(strict_types=1); namespace Hyperf\Utils; +use Carbon\Carbon; use Swoole\Coroutine as SwCoroutine; class Context { + public const DONE = 'hyperf.context.done'; + protected static $nonCoContext = []; public static function set(string $id, $value) @@ -108,4 +111,50 @@ class Context return static::$nonCoContext; } + + public static function done(): bool + { + $holder = self::getOrSet(static::DONE, new \stdClass()); + return $holder->done ?? false; + } + + /** + * @param float|int $millisecond + */ + public static function setTimeout($millisecond) + { + $holder = self::getOrSet(static::DONE, new \stdClass()); + Coroutine::create(function () use ($holder, $millisecond) { + usleep((int) $millisecond * 1000); + $holder->done = true; + }); + } + + public static function setDeadline(\DateTime $deadline) + { + if (! ($deadline instanceof Carbon)) { + $deadline = Carbon::instance($deadline); + } + + $timeout = $deadline->getPreciseTimestamp(3) - microtime(true) * 1000; + $timeout = $timeout > 0 ? $timeout : 0; + static::setTimeout($timeout); + } + + public static function cancel(): void + { + $holder = self::getOrSet(static::DONE, new \stdClass()); + $holder->done = true; + } + + public static function go(callable $callable): int + { + if (! self::has(static::DONE)) { + self::set(static::DONE, new \stdClass()); + } + return Coroutine::create(function () use ($callable) { + static::copy(coroutine::parentId()); + $callable(); + }); + } } diff --git a/src/utils/tests/ContextTest.php b/src/utils/tests/ContextTest.php index c5206868f..576846927 100644 --- a/src/utils/tests/ContextTest.php +++ b/src/utils/tests/ContextTest.php @@ -12,8 +12,12 @@ declare(strict_types=1); namespace HyperfTest\Utils; +use Carbon\Carbon; use Hyperf\Utils\Context; use PHPUnit\Framework\TestCase; +use Swoole\Coroutine\Channel; +use Swoole\Coroutine\System; +use Swoole\Runtime; /** * @internal @@ -45,4 +49,101 @@ class ContextTest extends TestCase Context::set('test.store.id', null); $this->assertSame(1, Context::getOrSet('test.store.id', 1)); } + + public function testCancel() + { + Context::set(Context::DONE, null); + $chan = new Channel(1); + Context::go(function () use ($chan) { + if (Context::done()) { + $chan->push(1); + return; + } + $chan->push(2); + }); + $this->assertEquals(2, $chan->pop()); + Context::cancel(); + Context::go(function () use ($chan) { + if (Context::done()) { + $chan->push(1); + return; + } + $chan->push(2); + }); + $this->assertEquals(1, $chan->pop()); + } + + public function testNestedCancel() + { + Context::set(Context::DONE, null); + $chan = new Channel(1); + Context::go(function () use ($chan) { + if (Context::done()) { + $chan->push(1); + return; + } + usleep(20000); + System::sleep(1); + Context::go(function () use ($chan) { + if (Context::done()) { + $chan->push(2); + return; + } + $chan->push(3); + }); + }); + usleep(10000); + Context::cancel(); + $this->assertEquals(2, $chan->pop()); + } + + public function testTimeout() + { + Context::set(Context::DONE, null); + Runtime::enableCoroutine(); + Context::setTimeout(5); + $chan = new Channel(1); + Context::go(function () use ($chan) { + if (Context::done()) { + $chan->push(1); + return; + } + $chan->push(2); + }); + $this->assertEquals(2, $chan->pop()); + Context::go(function () use ($chan) { + usleep(10000); + if (Context::done()) { + $chan->push(1); + return; + } + $chan->push(2); + }); + $this->assertEquals(1, $chan->pop()); + } + + public function testDeadline() + { + Context::set(Context::DONE, null); + $deadline = Carbon::now()->addMillisecond(5); + Context::setDeadline($deadline); + $chan = new Channel(1); + Context::go(function () use ($chan) { + if (Context::done()) { + $chan->push(1); + return; + } + $chan->push(2); + }); + $this->assertEquals(2, $chan->pop()); + usleep(10000); + Context::go(function () use ($chan) { + if (Context::done()) { + $chan->push(1); + return; + } + $chan->push(2); + }); + $this->assertEquals(1, $chan->pop()); + } } From 47bdf2ccb97bf4c5ace1df267688773c1ae6ce4d Mon Sep 17 00:00:00 2001 From: reasno Date: Sat, 14 Mar 2020 17:13:51 +0800 Subject: [PATCH 2/9] Allow config fetcher to start in a coroutine instead of standalone process. --- src/config-aliyun-acm/publish/aliyun_acm.php | 1 + .../src/Listener/BootProcessListener.php | 30 +++++++++++++--- .../src/Process/ConfigFetcherProcess.php | 3 +- src/config-apollo/publish/apollo.php | 1 + .../src/Listener/BootProcessListener.php | 13 +++++++ .../src/Process/ConfigFetcherProcess.php | 3 +- src/config-etcd/publish/config_etcd.php | 1 + .../src/Listener/BootProcessListener.php | 35 +++++++++++++++---- .../src/Process/ConfigFetcherProcess.php | 3 +- 9 files changed, 75 insertions(+), 15 deletions(-) diff --git a/src/config-aliyun-acm/publish/aliyun_acm.php b/src/config-aliyun-acm/publish/aliyun_acm.php index e0f25c952..3f2cbe51c 100644 --- a/src/config-aliyun-acm/publish/aliyun_acm.php +++ b/src/config-aliyun-acm/publish/aliyun_acm.php @@ -12,6 +12,7 @@ declare(strict_types=1); return [ 'enable' => false, + 'use_standalone_process' => true, 'interval' => 5, 'endpoint' => env('ALIYUN_ACM_ENDPOINT', 'acm.aliyun.com'), 'namespace' => env('ALIYUN_ACM_NAMESPACE', ''), diff --git a/src/config-aliyun-acm/src/Listener/BootProcessListener.php b/src/config-aliyun-acm/src/Listener/BootProcessListener.php index ba1155932..27032075d 100644 --- a/src/config-aliyun-acm/src/Listener/BootProcessListener.php +++ b/src/config-aliyun-acm/src/Listener/BootProcessListener.php @@ -18,6 +18,7 @@ use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Event\Contract\ListenerInterface; use Hyperf\Framework\Event\BeforeWorkerStart; use Hyperf\Process\Event\BeforeProcessHandle; +use Hyperf\Utils\Coroutine; use Psr\Container\ContainerInterface; class BootProcessListener implements ListenerInterface @@ -59,11 +60,30 @@ class BootProcessListener implements ListenerInterface } if ($config = $this->client->pull()) { - foreach ($config as $key => $value) { - if (is_string($key)) { - $this->config->set($key, $value); - $this->logger->debug(sprintf('Config [%s] is updated', $key)); - } + $this->updateConfig($config); + } + + if (! $this->config->get('aliyun_acm.use_standalone_process', true)) { + Coroutine::create(function () { + $interval = $this->config->get('aliyun_acm.interval', 5); + retry(INF, function () use ($interval) { + while (true) { + sleep($interval); + $config = $this->client->pull(); + if ($config !== $this->config) { + $this->updateConfig($config); + } + } + }, $interval * 1000); + }); + } + } + + protected function updateConfig(array $config){ + foreach ($config as $key => $value) { + if (is_string($key)) { + $this->config->set($key, $value); + $this->logger->debug(sprintf('Config [%s] is updated', $key)); } } } diff --git a/src/config-aliyun-acm/src/Process/ConfigFetcherProcess.php b/src/config-aliyun-acm/src/Process/ConfigFetcherProcess.php index cd6b5129a..b57880078 100644 --- a/src/config-aliyun-acm/src/Process/ConfigFetcherProcess.php +++ b/src/config-aliyun-acm/src/Process/ConfigFetcherProcess.php @@ -59,7 +59,8 @@ class ConfigFetcherProcess extends AbstractProcess public function isEnable(): bool { - return $this->config->get('aliyun_acm.enable', false); + return $this->config->get('aliyun_acm.enable', false) + && $this->config->get('aliyun_acm.use_standalone_process', true); } public function handle(): void diff --git a/src/config-apollo/publish/apollo.php b/src/config-apollo/publish/apollo.php index a92e08686..cdae92e37 100644 --- a/src/config-apollo/publish/apollo.php +++ b/src/config-apollo/publish/apollo.php @@ -12,6 +12,7 @@ declare(strict_types=1); return [ 'enable' => false, + 'use_standalone_process' => true, 'server' => 'http://localhost:8080', 'appid' => 'test', 'cluster' => 'default', diff --git a/src/config-apollo/src/Listener/BootProcessListener.php b/src/config-apollo/src/Listener/BootProcessListener.php index 9c002aa90..e79b6f5f7 100644 --- a/src/config-apollo/src/Listener/BootProcessListener.php +++ b/src/config-apollo/src/Listener/BootProcessListener.php @@ -17,6 +17,7 @@ use Hyperf\ConfigApollo\PipeMessage; use Hyperf\ConfigApollo\ReleaseKey; use Hyperf\Framework\Event\BeforeWorkerStart; use Hyperf\Process\Event\BeforeProcessHandle; +use Hyperf\Utils\Coroutine; class BootProcessListener extends OnPipeMessageListener { @@ -62,5 +63,17 @@ class BootProcessListener extends OnPipeMessageListener $callbacks[$namespace] = $ipcCallback; } $this->client->pull($namespaces, $callbacks); + + if (! $this->config->get('apollo.use_standalone_process', true)) { + Coroutine::create(function () use ($namespaces, $callbacks) { + $interval = $this->config->get('apollo.interval', 5); + retry(INF, function () use ($namespaces, $callbacks, $interval) { + while (true) { + sleep($interval); + $this->client->pull($namespaces, $callbacks); + } + }, $interval * 1000); + }); + } } } diff --git a/src/config-apollo/src/Process/ConfigFetcherProcess.php b/src/config-apollo/src/Process/ConfigFetcherProcess.php index 77de23b2a..ffa39696b 100644 --- a/src/config-apollo/src/Process/ConfigFetcherProcess.php +++ b/src/config-apollo/src/Process/ConfigFetcherProcess.php @@ -54,7 +54,8 @@ class ConfigFetcherProcess extends AbstractProcess public function isEnable(): bool { - return $this->config->get('apollo.enable', false); + return $this->config->get('apollo.enable', false) + && $this->config->get('apollo.use_standalone_process', true); } public function handle(): void diff --git a/src/config-etcd/publish/config_etcd.php b/src/config-etcd/publish/config_etcd.php index 4b1bbe5d9..a9f8b7749 100644 --- a/src/config-etcd/publish/config_etcd.php +++ b/src/config-etcd/publish/config_etcd.php @@ -13,6 +13,7 @@ declare(strict_types=1); return [ 'enable' => false, 'packer' => Hyperf\Utils\Packer\JsonPacker::class, + 'use_standalone_process' => true, 'namespaces' => [ 'application', ], diff --git a/src/config-etcd/src/Listener/BootProcessListener.php b/src/config-etcd/src/Listener/BootProcessListener.php index f0f3b71e1..e7fd5709e 100644 --- a/src/config-etcd/src/Listener/BootProcessListener.php +++ b/src/config-etcd/src/Listener/BootProcessListener.php @@ -20,6 +20,7 @@ use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Event\Contract\ListenerInterface; use Hyperf\Framework\Event\BeforeWorkerStart; use Hyperf\Process\Event\BeforeProcessHandle; +use Hyperf\Utils\Coroutine; use Hyperf\Utils\Packer\JsonPacker; use Psr\Container\ContainerInterface; @@ -75,13 +76,33 @@ class BootProcessListener implements ListenerInterface } if ($config = $this->client->pull()) { - $configurations = $this->format($config); - foreach ($configurations as $kv) { - $key = $this->mapping[$kv->key] ?? null; - if (is_string($key)) { - $this->config->set($key, $this->packer->unpack($kv->value)); - $this->logger->debug(sprintf('Config [%s] is updated', $key)); - } + $this->updateConfig($config); + } + + if (! $this->config->get('config_etcd.use_standalone_process', true)) { + Coroutine::create(function () { + $interval = $this->config->get('config_etcd.interval', 5); + retry(INF, function () use ($interval) { + while (true) { + sleep($interval); + $config = $this->client->pull(); + if ($config !== $this->config) { + $this->updateConfig($config); + } + } + }, $interval * 1000); + }); + } + } + + protected function updateConfig(array $config) + { + $configurations = $this->format($config); + foreach ($configurations as $kv) { + $key = $this->mapping[$kv->key] ?? null; + if (is_string($key)) { + $this->config->set($key, $this->packer->unpack($kv->value)); + $this->logger->debug(sprintf('Config [%s] is updated', $key)); } } } diff --git a/src/config-etcd/src/Process/ConfigFetcherProcess.php b/src/config-etcd/src/Process/ConfigFetcherProcess.php index 3e68e0d0e..95c500083 100644 --- a/src/config-etcd/src/Process/ConfigFetcherProcess.php +++ b/src/config-etcd/src/Process/ConfigFetcherProcess.php @@ -60,7 +60,8 @@ class ConfigFetcherProcess extends AbstractProcess public function isEnable(): bool { - return $this->config->get('config_etcd.enable', false); + return $this->config->get('config_etcd.enable', false) + && $this->config->get('config_etcd.use_standalone_process', true); } public function handle(): void From 496872fb72a9d3107526da7a29c3a3e59154b21c Mon Sep 17 00:00:00 2001 From: reasno Date: Sat, 14 Mar 2020 17:45:20 +0800 Subject: [PATCH 3/9] apply use_standalone_process to zookeeper --- .../src/Listener/BootProcessListener.php | 3 +- src/config-zookeeper/publish/zookeeper.php | 1 + src/config-zookeeper/src/ConfigProvider.php | 2 + .../src/Listener/BootProcessListener.php | 75 +++++++++++++++++++ .../src/Process/ConfigFetcherProcess.php | 3 +- 5 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 src/config-zookeeper/src/Listener/BootProcessListener.php diff --git a/src/config-aliyun-acm/src/Listener/BootProcessListener.php b/src/config-aliyun-acm/src/Listener/BootProcessListener.php index 27032075d..84f027e50 100644 --- a/src/config-aliyun-acm/src/Listener/BootProcessListener.php +++ b/src/config-aliyun-acm/src/Listener/BootProcessListener.php @@ -79,7 +79,8 @@ class BootProcessListener implements ListenerInterface } } - protected function updateConfig(array $config){ + protected function updateConfig(array $config) + { foreach ($config as $key => $value) { if (is_string($key)) { $this->config->set($key, $value); diff --git a/src/config-zookeeper/publish/zookeeper.php b/src/config-zookeeper/publish/zookeeper.php index 8c3d32510..0f3aecf74 100644 --- a/src/config-zookeeper/publish/zookeeper.php +++ b/src/config-zookeeper/publish/zookeeper.php @@ -12,6 +12,7 @@ declare(strict_types=1); return [ 'enable' => false, + 'use_standalone_process' => true, 'interval' => 5, 'server' => env('ZOOKEEPER_SERVER', '127.0.0.1:2181'), 'path' => env('ZOOKEEPER_CONFIG_PATH', '/conf'), diff --git a/src/config-zookeeper/src/ConfigProvider.php b/src/config-zookeeper/src/ConfigProvider.php index 25c33bca7..15bec46b9 100644 --- a/src/config-zookeeper/src/ConfigProvider.php +++ b/src/config-zookeeper/src/ConfigProvider.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Hyperf\ConfigZookeeper; +use Hyperf\ConfigZookeeper\Listener\BootProcessListener; use Hyperf\ConfigZookeeper\Listener\OnPipeMessageListener; use Hyperf\ConfigZookeeper\Process\ConfigFetcherProcess; @@ -28,6 +29,7 @@ class ConfigProvider ], 'listeners' => [ OnPipeMessageListener::class, + BootProcessListener::class, ], 'annotations' => [ 'scan' => [ diff --git a/src/config-zookeeper/src/Listener/BootProcessListener.php b/src/config-zookeeper/src/Listener/BootProcessListener.php new file mode 100644 index 000000000..21571a0d4 --- /dev/null +++ b/src/config-zookeeper/src/Listener/BootProcessListener.php @@ -0,0 +1,75 @@ +config = $config; + $this->logger = $logger; + $this->client = $client; + } + + public function listen(): array + { + return [ + BeforeWorkerStart::class, + BeforeProcessHandle::class, + ]; + } + + public function process(object $event) + { + if (! $this->config->get('zookeeper.use_standalone_process', true)) { + Coroutine::create(function () { + $interval = $this->config->get('zookeeper.interval', 5); + retry(INF, function () use ($interval) { + while (true) { + $config = $this->client->pull(); + if ($config !== $this->config) { + foreach ($config ?? [] as $key => $value) { + $this->config->set($key, $value); + $this->logger->debug(sprintf('Config [%s] is updated', $key)); + } + } + sleep($interval); + } + }, $interval * 1000); + }); + } + } +} diff --git a/src/config-zookeeper/src/Process/ConfigFetcherProcess.php b/src/config-zookeeper/src/Process/ConfigFetcherProcess.php index 1f684fd59..eef9f5e00 100644 --- a/src/config-zookeeper/src/Process/ConfigFetcherProcess.php +++ b/src/config-zookeeper/src/Process/ConfigFetcherProcess.php @@ -63,7 +63,8 @@ class ConfigFetcherProcess extends AbstractProcess public function isEnable(): bool { - return $this->config->get('zookeeper.enable', false); + return $this->config->get('zookeeper.enable', false) + && $this->config->get('zookeeper.use_standalone_process', true); } public function handle(): void From d866567016792e0d479849bf061e3715941d9a29 Mon Sep 17 00:00:00 2001 From: reasno Date: Sat, 14 Mar 2020 17:55:53 +0800 Subject: [PATCH 4/9] guard zookeeper.enable --- src/config-zookeeper/src/Listener/BootProcessListener.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/config-zookeeper/src/Listener/BootProcessListener.php b/src/config-zookeeper/src/Listener/BootProcessListener.php index 21571a0d4..9c1078095 100644 --- a/src/config-zookeeper/src/Listener/BootProcessListener.php +++ b/src/config-zookeeper/src/Listener/BootProcessListener.php @@ -54,6 +54,10 @@ class BootProcessListener implements ListenerInterface public function process(object $event) { + if (! $this->config->get('zookeeper.enable', false)) { + return; + } + if (! $this->config->get('zookeeper.use_standalone_process', true)) { Coroutine::create(function () { $interval = $this->config->get('zookeeper.interval', 5); From 8f7891b9d747c01b5a6ecc05831221816633e75a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Mon, 16 Mar 2020 15:30:03 +0800 Subject: [PATCH 5/9] Fixed bug that update config everytime. --- .../src/Listener/BootProcessListener.php | 4 +++- .../src/Listener/BootProcessListener.php | 4 +++- .../src/Listener/BootProcessListener.php | 23 +++++++++++++------ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/config-aliyun-acm/src/Listener/BootProcessListener.php b/src/config-aliyun-acm/src/Listener/BootProcessListener.php index 84f027e50..40bfe8dbc 100644 --- a/src/config-aliyun-acm/src/Listener/BootProcessListener.php +++ b/src/config-aliyun-acm/src/Listener/BootProcessListener.php @@ -67,12 +67,14 @@ class BootProcessListener implements ListenerInterface Coroutine::create(function () { $interval = $this->config->get('aliyun_acm.interval', 5); retry(INF, function () use ($interval) { + $prevConfig = []; while (true) { sleep($interval); $config = $this->client->pull(); - if ($config !== $this->config) { + if ($config !== $prevConfig) { $this->updateConfig($config); } + $prevConfig = $config; } }, $interval * 1000); }); diff --git a/src/config-etcd/src/Listener/BootProcessListener.php b/src/config-etcd/src/Listener/BootProcessListener.php index e7fd5709e..eb27112ea 100644 --- a/src/config-etcd/src/Listener/BootProcessListener.php +++ b/src/config-etcd/src/Listener/BootProcessListener.php @@ -83,12 +83,14 @@ class BootProcessListener implements ListenerInterface Coroutine::create(function () { $interval = $this->config->get('config_etcd.interval', 5); retry(INF, function () use ($interval) { + $prevConfig = []; while (true) { sleep($interval); $config = $this->client->pull(); - if ($config !== $this->config) { + if ($config !== $prevConfig) { $this->updateConfig($config); } + $prevConfig = $config; } }, $interval * 1000); }); diff --git a/src/config-zookeeper/src/Listener/BootProcessListener.php b/src/config-zookeeper/src/Listener/BootProcessListener.php index 9c1078095..7cb8d4f60 100644 --- a/src/config-zookeeper/src/Listener/BootProcessListener.php +++ b/src/config-zookeeper/src/Listener/BootProcessListener.php @@ -62,18 +62,27 @@ class BootProcessListener implements ListenerInterface Coroutine::create(function () { $interval = $this->config->get('zookeeper.interval', 5); retry(INF, function () use ($interval) { + $prevConfig = []; while (true) { - $config = $this->client->pull(); - if ($config !== $this->config) { - foreach ($config ?? [] as $key => $value) { - $this->config->set($key, $value); - $this->logger->debug(sprintf('Config [%s] is updated', $key)); - } - } sleep($interval); + $config = $this->client->pull(); + if ($config !== $prevConfig) { + $this->updateConfig($config); + } + $prevConfig = $config; } }, $interval * 1000); }); } } + + protected function updateConfig(array $config) + { + foreach ($config as $key => $value) { + if (is_string($key)) { + $this->config->set($key, $value); + $this->logger->debug(sprintf('Config [%s] is updated', $key)); + } + } + } } From ce24419456dd41df9e935b20ef436e95c4564416 Mon Sep 17 00:00:00 2001 From: Reasno Date: Tue, 17 Mar 2020 12:34:37 +0800 Subject: [PATCH 6/9] Add Doc. --- doc/zh-cn/config-center.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/zh-cn/config-center.md b/doc/zh-cn/config-center.md index 367be9a9e..7b90958a2 100644 --- a/doc/zh-cn/config-center.md +++ b/doc/zh-cn/config-center.md @@ -40,6 +40,8 @@ composer require hyperf/config-aliyun-acm return [ // 是否开启配置中心的接入流程,为 true 时会自动启动一个 ConfigFetcherProcess 进程用于更新配置 'enable' => true, + // 是否使用独立进程来拉取config,如果否则将在worker内以协程方式拉取 + 'use_standalone_process' => true, // Apollo Server 'server' => 'http://127.0.0.1:8080', // 您的 AppId @@ -74,6 +76,8 @@ return [ return [ // 是否开启配置中心的接入流程,为 true 时会自动启动一个 ConfigFetcherProcess 进程用于更新配置 'enable' => true, + // 是否使用独立进程来拉取config,如果否则将在worker内以协程方式拉取 + 'use_standalone_process' => true, // 配置更新间隔(秒) 'interval' => 5, // 阿里云 ACM 断点地址,取决于您的可用区 @@ -135,6 +139,7 @@ composer require hyperf/config-etcd true, + 'use_standalone_process' => true, 'namespaces' => [ '/test', ], From 42a505ace628d8d3c7dde49293c408a01785f14a Mon Sep 17 00:00:00 2001 From: Reasno Date: Tue, 17 Mar 2020 12:36:47 +0800 Subject: [PATCH 7/9] Add CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7db9f4cc4..3c399258a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Added +- [#1419](https://github.com/hyperf/hyperf/pull/1419) Allow config fetcher to start in a coroutine instead of a process. - [#1424](https://github.com/hyperf/hyperf/pull/1424) Allow user modify the session_name by configuration file. # v1.1.20 - 2020-03-12 From 63921dda48e08ec1d6636e09fa790d2ddf4f706e Mon Sep 17 00:00:00 2001 From: Reasno Date: Tue, 17 Mar 2020 12:38:55 +0800 Subject: [PATCH 8/9] Revert "Add setTimeout & Cancel API" This reverts commit 27b20e2a --- src/utils/src/Context.php | 49 ---------------- src/utils/tests/ContextTest.php | 101 -------------------------------- 2 files changed, 150 deletions(-) diff --git a/src/utils/src/Context.php b/src/utils/src/Context.php index 99a14fba5..6cec5b605 100644 --- a/src/utils/src/Context.php +++ b/src/utils/src/Context.php @@ -12,13 +12,10 @@ declare(strict_types=1); namespace Hyperf\Utils; -use Carbon\Carbon; use Swoole\Coroutine as SwCoroutine; class Context { - public const DONE = 'hyperf.context.done'; - protected static $nonCoContext = []; public static function set(string $id, $value) @@ -111,50 +108,4 @@ class Context return static::$nonCoContext; } - - public static function done(): bool - { - $holder = self::getOrSet(static::DONE, new \stdClass()); - return $holder->done ?? false; - } - - /** - * @param float|int $millisecond - */ - public static function setTimeout($millisecond) - { - $holder = self::getOrSet(static::DONE, new \stdClass()); - Coroutine::create(function () use ($holder, $millisecond) { - usleep((int) $millisecond * 1000); - $holder->done = true; - }); - } - - public static function setDeadline(\DateTime $deadline) - { - if (! ($deadline instanceof Carbon)) { - $deadline = Carbon::instance($deadline); - } - - $timeout = $deadline->getPreciseTimestamp(3) - microtime(true) * 1000; - $timeout = $timeout > 0 ? $timeout : 0; - static::setTimeout($timeout); - } - - public static function cancel(): void - { - $holder = self::getOrSet(static::DONE, new \stdClass()); - $holder->done = true; - } - - public static function go(callable $callable): int - { - if (! self::has(static::DONE)) { - self::set(static::DONE, new \stdClass()); - } - return Coroutine::create(function () use ($callable) { - static::copy(coroutine::parentId()); - $callable(); - }); - } } diff --git a/src/utils/tests/ContextTest.php b/src/utils/tests/ContextTest.php index 576846927..c5206868f 100644 --- a/src/utils/tests/ContextTest.php +++ b/src/utils/tests/ContextTest.php @@ -12,12 +12,8 @@ declare(strict_types=1); namespace HyperfTest\Utils; -use Carbon\Carbon; use Hyperf\Utils\Context; use PHPUnit\Framework\TestCase; -use Swoole\Coroutine\Channel; -use Swoole\Coroutine\System; -use Swoole\Runtime; /** * @internal @@ -49,101 +45,4 @@ class ContextTest extends TestCase Context::set('test.store.id', null); $this->assertSame(1, Context::getOrSet('test.store.id', 1)); } - - public function testCancel() - { - Context::set(Context::DONE, null); - $chan = new Channel(1); - Context::go(function () use ($chan) { - if (Context::done()) { - $chan->push(1); - return; - } - $chan->push(2); - }); - $this->assertEquals(2, $chan->pop()); - Context::cancel(); - Context::go(function () use ($chan) { - if (Context::done()) { - $chan->push(1); - return; - } - $chan->push(2); - }); - $this->assertEquals(1, $chan->pop()); - } - - public function testNestedCancel() - { - Context::set(Context::DONE, null); - $chan = new Channel(1); - Context::go(function () use ($chan) { - if (Context::done()) { - $chan->push(1); - return; - } - usleep(20000); - System::sleep(1); - Context::go(function () use ($chan) { - if (Context::done()) { - $chan->push(2); - return; - } - $chan->push(3); - }); - }); - usleep(10000); - Context::cancel(); - $this->assertEquals(2, $chan->pop()); - } - - public function testTimeout() - { - Context::set(Context::DONE, null); - Runtime::enableCoroutine(); - Context::setTimeout(5); - $chan = new Channel(1); - Context::go(function () use ($chan) { - if (Context::done()) { - $chan->push(1); - return; - } - $chan->push(2); - }); - $this->assertEquals(2, $chan->pop()); - Context::go(function () use ($chan) { - usleep(10000); - if (Context::done()) { - $chan->push(1); - return; - } - $chan->push(2); - }); - $this->assertEquals(1, $chan->pop()); - } - - public function testDeadline() - { - Context::set(Context::DONE, null); - $deadline = Carbon::now()->addMillisecond(5); - Context::setDeadline($deadline); - $chan = new Channel(1); - Context::go(function () use ($chan) { - if (Context::done()) { - $chan->push(1); - return; - } - $chan->push(2); - }); - $this->assertEquals(2, $chan->pop()); - usleep(10000); - Context::go(function () use ($chan) { - if (Context::done()) { - $chan->push(1); - return; - } - $chan->push(2); - }); - $this->assertEquals(1, $chan->pop()); - } } From 33bb34c9a754c84a637a4dc4ef4db1c7b8080b30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Tue, 17 Mar 2020 14:50:14 +0800 Subject: [PATCH 9/9] Fixed command does not works in config center. --- src/config-aliyun-acm/src/Listener/BootProcessListener.php | 2 ++ src/config-apollo/src/Listener/BootProcessListener.php | 2 ++ src/config-etcd/src/Listener/BootProcessListener.php | 2 ++ src/config-zookeeper/src/Listener/BootProcessListener.php | 2 ++ 4 files changed, 8 insertions(+) diff --git a/src/config-aliyun-acm/src/Listener/BootProcessListener.php b/src/config-aliyun-acm/src/Listener/BootProcessListener.php index 40bfe8dbc..c04c49899 100644 --- a/src/config-aliyun-acm/src/Listener/BootProcessListener.php +++ b/src/config-aliyun-acm/src/Listener/BootProcessListener.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Hyperf\ConfigAliyunAcm\Listener; +use Hyperf\Command\Event\BeforeHandle; use Hyperf\ConfigAliyunAcm\ClientInterface; use Hyperf\Contract\ConfigInterface; use Hyperf\Contract\StdoutLoggerInterface; @@ -50,6 +51,7 @@ class BootProcessListener implements ListenerInterface return [ BeforeWorkerStart::class, BeforeProcessHandle::class, + BeforeHandle::class, ]; } diff --git a/src/config-apollo/src/Listener/BootProcessListener.php b/src/config-apollo/src/Listener/BootProcessListener.php index e79b6f5f7..287ca4554 100644 --- a/src/config-apollo/src/Listener/BootProcessListener.php +++ b/src/config-apollo/src/Listener/BootProcessListener.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Hyperf\ConfigApollo\Listener; +use Hyperf\Command\Event\BeforeHandle; use Hyperf\ConfigApollo\Option; use Hyperf\ConfigApollo\PipeMessage; use Hyperf\ConfigApollo\ReleaseKey; @@ -26,6 +27,7 @@ class BootProcessListener extends OnPipeMessageListener return [ BeforeWorkerStart::class, BeforeProcessHandle::class, + BeforeHandle::class, ]; } diff --git a/src/config-etcd/src/Listener/BootProcessListener.php b/src/config-etcd/src/Listener/BootProcessListener.php index eb27112ea..c6995507b 100644 --- a/src/config-etcd/src/Listener/BootProcessListener.php +++ b/src/config-etcd/src/Listener/BootProcessListener.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Hyperf\ConfigEtcd\Listener; +use Hyperf\Command\Event\BeforeHandle; use Hyperf\ConfigEtcd\ClientInterface; use Hyperf\ConfigEtcd\KV; use Hyperf\Contract\ConfigInterface; @@ -66,6 +67,7 @@ class BootProcessListener implements ListenerInterface return [ BeforeWorkerStart::class, BeforeProcessHandle::class, + BeforeHandle::class, ]; } diff --git a/src/config-zookeeper/src/Listener/BootProcessListener.php b/src/config-zookeeper/src/Listener/BootProcessListener.php index 7cb8d4f60..1300a7a47 100644 --- a/src/config-zookeeper/src/Listener/BootProcessListener.php +++ b/src/config-zookeeper/src/Listener/BootProcessListener.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Hyperf\ConfigZookeeper\Listener; +use Hyperf\Command\Event\BeforeHandle; use Hyperf\ConfigZookeeper\ClientInterface; use Hyperf\Contract\ConfigInterface; use Hyperf\Contract\StdoutLoggerInterface; @@ -49,6 +50,7 @@ class BootProcessListener implements ListenerInterface return [ BeforeWorkerStart::class, BeforeProcessHandle::class, + BeforeHandle::class, ]; }