From 5528231021f0bd27346aefaafa1d56295503665c Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Thu, 18 Dec 2025 11:11:34 +0800 Subject: [PATCH 1/2] refactor(sentry): improve timer cleanup in metrics listeners Replace manual timer cleanup with Timer's built-in closure parameter support. This removes the need for separate coroutines to monitor WORKER_EXIT events and simplifies the timer handling logic across all metrics listeners. Changes: - Use Timer's closure parameter to handle cleanup gracefully - Remove manual coroutine creation and CoordinatorManager usage - Improve overall resource management and reduce complexity Affected listeners: - OnBeforeHandle - OnCoroutineServerStart - OnMetricFactoryReady - OnWorkerStart - PoolWatcher - QueueWatcher --- src/sentry/src/Metrics/Listener/OnBeforeHandle.php | 13 +++++-------- .../Metrics/Listener/OnCoroutineServerStart.php | 14 +++++--------- .../src/Metrics/Listener/OnMetricFactoryReady.php | 13 +++++-------- src/sentry/src/Metrics/Listener/OnWorkerStart.php | 14 +++++--------- src/sentry/src/Metrics/Listener/PoolWatcher.php | 14 +++++--------- src/sentry/src/Metrics/Listener/QueueWatcher.php | 14 +++++--------- 6 files changed, 30 insertions(+), 52 deletions(-) diff --git a/src/sentry/src/Metrics/Listener/OnBeforeHandle.php b/src/sentry/src/Metrics/Listener/OnBeforeHandle.php index df2047341..aa3ec7752 100644 --- a/src/sentry/src/Metrics/Listener/OnBeforeHandle.php +++ b/src/sentry/src/Metrics/Listener/OnBeforeHandle.php @@ -16,9 +16,7 @@ use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter; use FriendsOfHyperf\Sentry\SentryContext; use Hyperf\Command\Event\BeforeHandle; -use Hyperf\Coordinator\CoordinatorManager; use Hyperf\Coordinator\Timer; -use Hyperf\Engine\Coroutine; use Hyperf\Event\Contract\ListenerInterface; use Sentry\Unit; @@ -89,7 +87,11 @@ public function process(object $event): void 'ru_stime_tv_sec', ]; - $timerId = $this->timer->tick($this->feature->getMetricsInterval(), function () use ($metrics) { + $this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics) { + if ($isClosing) { + return Timer::STOP; + } + defer(fn () => metrics()->flush()); $this->trySet('gc_', $metrics, gc_status()); @@ -108,10 +110,5 @@ public function process(object $event): void Unit::megabyte() ); }); - - Coroutine::create(function () use ($timerId) { - CoordinatorManager::until(\Hyperf\Coordinator\Constants::WORKER_EXIT)->yield(); - $this->timer->clear($timerId); - }); } } diff --git a/src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php b/src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php index 2be7c9653..cf371a4aa 100644 --- a/src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php +++ b/src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php @@ -14,10 +14,7 @@ use FriendsOfHyperf\Sentry\Feature; use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady; use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter; -use Hyperf\Coordinator\Constants; -use Hyperf\Coordinator\CoordinatorManager; use Hyperf\Coordinator\Timer; -use Hyperf\Engine\Coroutine; use Hyperf\Event\Contract\ListenerInterface; use Hyperf\Server\Event\MainCoroutineServerStart; use Psr\Container\ContainerInterface; @@ -100,7 +97,11 @@ public function process(object $event): void 'ru_stime_tv_sec', ]; - $timerId = $this->timer->tick($this->feature->getMetricsInterval(), function () use ($metrics) { + $this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics) { + if ($isClosing) { + return Timer::STOP; + } + defer(fn () => metrics()->flush()); $this->trySet('gc_', $metrics, gc_status()); @@ -119,10 +120,5 @@ public function process(object $event): void Unit::megabyte() ); }); - - Coroutine::create(function () use ($timerId) { - CoordinatorManager::until(Constants::WORKER_EXIT)->yield(); - $this->timer->clear($timerId); - }); } } diff --git a/src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php b/src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php index 1e93bb454..ada52adbb 100644 --- a/src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php +++ b/src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php @@ -16,8 +16,6 @@ use FriendsOfHyperf\Sentry\Metrics\CoroutineServerStats; use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady; use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter; -use Hyperf\Coordinator\Constants; -use Hyperf\Coordinator\CoordinatorManager; use Hyperf\Coordinator\Timer; use Hyperf\Engine\Coroutine; use Hyperf\Event\Contract\ListenerInterface; @@ -98,7 +96,11 @@ public function process(object $event): void } } - $timerId = $this->timer->tick($this->feature->getMetricsInterval(), function () use ($metrics, $serverStatsFactory, $workerId) { + $this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics, $serverStatsFactory, $workerId) { + if ($isClosing) { + return Timer::STOP; + } + defer(fn () => metrics()->flush()); $this->trySet('', $metrics, Coroutine::stats(), $workerId); @@ -131,10 +133,5 @@ public function process(object $event): void Unit::megabyte() ); }); - - Coroutine::create(function () use ($timerId) { - CoordinatorManager::until(Constants::WORKER_EXIT)->yield(); - $this->timer->clear($timerId); - }); } } diff --git a/src/sentry/src/Metrics/Listener/OnWorkerStart.php b/src/sentry/src/Metrics/Listener/OnWorkerStart.php index 3c6f10d61..046d33798 100644 --- a/src/sentry/src/Metrics/Listener/OnWorkerStart.php +++ b/src/sentry/src/Metrics/Listener/OnWorkerStart.php @@ -14,10 +14,7 @@ use FriendsOfHyperf\Sentry\Feature; use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady; use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter; -use Hyperf\Coordinator\Constants; -use Hyperf\Coordinator\CoordinatorManager; use Hyperf\Coordinator\Timer; -use Hyperf\Engine\Coroutine; use Hyperf\Event\Contract\ListenerInterface; use Hyperf\Framework\Event\BeforeWorkerStart; use Psr\Container\ContainerInterface; @@ -90,7 +87,11 @@ public function process(object $event): void 'ru_stime_tv_sec', ]; - $timerId = $this->timer->tick($this->feature->getMetricsInterval(), function () use ($metrics, $event) { + $this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics, $event) { + if ($isClosing) { + return Timer::STOP; + } + defer(fn () => metrics()->flush()); $server = $this->container->get(Server::class); @@ -121,10 +122,5 @@ public function process(object $event): void Unit::megabyte() ); }); - - Coroutine::create(function () use ($timerId) { - CoordinatorManager::until(Constants::WORKER_EXIT)->yield(); - $this->timer->clear($timerId); - }); } } diff --git a/src/sentry/src/Metrics/Listener/PoolWatcher.php b/src/sentry/src/Metrics/Listener/PoolWatcher.php index 565525596..d39d3d6c9 100644 --- a/src/sentry/src/Metrics/Listener/PoolWatcher.php +++ b/src/sentry/src/Metrics/Listener/PoolWatcher.php @@ -12,10 +12,7 @@ namespace FriendsOfHyperf\Sentry\Metrics\Listener; use FriendsOfHyperf\Sentry\Feature; -use Hyperf\Coordinator\Constants; -use Hyperf\Coordinator\CoordinatorManager; use Hyperf\Coordinator\Timer; -use Hyperf\Engine\Coroutine; use Hyperf\Event\Contract\ListenerInterface; use Hyperf\Framework\Event\BeforeWorkerStart; use Hyperf\Pool\Pool; @@ -65,11 +62,15 @@ public function watch(Pool $pool, string $poolName, int $workerId): void return; } - $timerId = $this->timer->tick($this->feature->getMetricsInterval(), function () use ( + $this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ( $pool, $workerId, $poolName ) { + if ($isClosing) { + return Timer::STOP; + } + defer(fn () => metrics()->flush()); metrics()->gauge( @@ -97,10 +98,5 @@ public function watch(Pool $pool, string $poolName, int $workerId): void ] ); }); - - Coroutine::create(function () use ($timerId) { - CoordinatorManager::until(Constants::WORKER_EXIT)->yield(); - $this->timer->clear($timerId); - }); } } diff --git a/src/sentry/src/Metrics/Listener/QueueWatcher.php b/src/sentry/src/Metrics/Listener/QueueWatcher.php index 96f620619..d666d6b6a 100644 --- a/src/sentry/src/Metrics/Listener/QueueWatcher.php +++ b/src/sentry/src/Metrics/Listener/QueueWatcher.php @@ -15,10 +15,7 @@ use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady; use Hyperf\AsyncQueue\Driver\DriverFactory; use Hyperf\Contract\ConfigInterface; -use Hyperf\Coordinator\Constants; -use Hyperf\Coordinator\CoordinatorManager; use Hyperf\Coordinator\Timer; -use Hyperf\Engine\Coroutine; use Hyperf\Event\Contract\ListenerInterface; use Psr\Container\ContainerInterface; @@ -55,7 +52,11 @@ public function process(object $event): void return; } - $timerId = $this->timer->tick($this->feature->getMetricsInterval(), function () { + $this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) { + if ($isClosing) { + return Timer::STOP; + } + defer(fn () => metrics()->flush()); $config = $this->container->get(ConfigInterface::class); @@ -87,10 +88,5 @@ public function process(object $event): void ); } }); - - Coroutine::create(function () use ($timerId) { - CoordinatorManager::until(Constants::WORKER_EXIT)->yield(); - $this->timer->clear($timerId); - }); } } From 749c75d9ba0f6bb1dc19d0f93bb4d6143848aab7 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Thu, 18 Dec 2025 11:22:11 +0800 Subject: [PATCH 2/2] refactor(metrics): add defer function import in OnBeforeHandle listener --- src/sentry/src/Metrics/Listener/OnBeforeHandle.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sentry/src/Metrics/Listener/OnBeforeHandle.php b/src/sentry/src/Metrics/Listener/OnBeforeHandle.php index aa3ec7752..de9691bb1 100644 --- a/src/sentry/src/Metrics/Listener/OnBeforeHandle.php +++ b/src/sentry/src/Metrics/Listener/OnBeforeHandle.php @@ -21,6 +21,7 @@ use Sentry\Unit; use function FriendsOfHyperf\Sentry\metrics; +use function Hyperf\Coroutine\defer; class OnBeforeHandle implements ListenerInterface {