Skip to content

Commit 1fb29ba

Browse files
committed
refactor(sentry): improve timer cleanup in metrics listeners (#1046)
* 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 * refactor(metrics): add defer function import in OnBeforeHandle listener --------- Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
1 parent 26a5c22 commit 1fb29ba

6 files changed

Lines changed: 31 additions & 52 deletions

File tree

src/sentry/src/Metrics/Listener/OnBeforeHandle.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616
use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter;
1717
use FriendsOfHyperf\Sentry\SentryContext;
1818
use Hyperf\Command\Event\BeforeHandle;
19-
use Hyperf\Coordinator\CoordinatorManager;
2019
use Hyperf\Coordinator\Timer;
21-
use Hyperf\Engine\Coroutine;
2220
use Hyperf\Event\Contract\ListenerInterface;
2321
use Sentry\Unit;
2422

2523
use function FriendsOfHyperf\Sentry\metrics;
24+
use function Hyperf\Coroutine\defer;
2625

2726
class OnBeforeHandle implements ListenerInterface
2827
{
@@ -89,7 +88,11 @@ public function process(object $event): void
8988
'ru_stime_tv_sec',
9089
];
9190

92-
$timerId = $this->timer->tick($this->feature->getMetricsInterval(), function () use ($metrics) {
91+
$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics) {
92+
if ($isClosing) {
93+
return Timer::STOP;
94+
}
95+
9396
defer(fn () => metrics()->flush());
9497

9598
$this->trySet('gc_', $metrics, gc_status());
@@ -108,10 +111,5 @@ public function process(object $event): void
108111
Unit::megabyte()
109112
);
110113
});
111-
112-
Coroutine::create(function () use ($timerId) {
113-
CoordinatorManager::until(\Hyperf\Coordinator\Constants::WORKER_EXIT)->yield();
114-
$this->timer->clear($timerId);
115-
});
116114
}
117115
}

src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
use FriendsOfHyperf\Sentry\Feature;
1515
use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady;
1616
use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter;
17-
use Hyperf\Coordinator\Constants;
18-
use Hyperf\Coordinator\CoordinatorManager;
1917
use Hyperf\Coordinator\Timer;
20-
use Hyperf\Engine\Coroutine;
2118
use Hyperf\Event\Contract\ListenerInterface;
2219
use Hyperf\Server\Event\MainCoroutineServerStart;
2320
use Psr\Container\ContainerInterface;
@@ -100,7 +97,11 @@ public function process(object $event): void
10097
'ru_stime_tv_sec',
10198
];
10299

103-
$timerId = $this->timer->tick($this->feature->getMetricsInterval(), function () use ($metrics) {
100+
$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics) {
101+
if ($isClosing) {
102+
return Timer::STOP;
103+
}
104+
104105
defer(fn () => metrics()->flush());
105106

106107
$this->trySet('gc_', $metrics, gc_status());
@@ -119,10 +120,5 @@ public function process(object $event): void
119120
Unit::megabyte()
120121
);
121122
});
122-
123-
Coroutine::create(function () use ($timerId) {
124-
CoordinatorManager::until(Constants::WORKER_EXIT)->yield();
125-
$this->timer->clear($timerId);
126-
});
127123
}
128124
}

src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
use FriendsOfHyperf\Sentry\Metrics\CoroutineServerStats;
1717
use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady;
1818
use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter;
19-
use Hyperf\Coordinator\Constants;
20-
use Hyperf\Coordinator\CoordinatorManager;
2119
use Hyperf\Coordinator\Timer;
2220
use Hyperf\Engine\Coroutine;
2321
use Hyperf\Event\Contract\ListenerInterface;
@@ -98,7 +96,11 @@ public function process(object $event): void
9896
}
9997
}
10098

101-
$timerId = $this->timer->tick($this->feature->getMetricsInterval(), function () use ($metrics, $serverStatsFactory, $workerId) {
99+
$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics, $serverStatsFactory, $workerId) {
100+
if ($isClosing) {
101+
return Timer::STOP;
102+
}
103+
102104
defer(fn () => metrics()->flush());
103105

104106
$this->trySet('', $metrics, Coroutine::stats(), $workerId);
@@ -131,10 +133,5 @@ public function process(object $event): void
131133
Unit::megabyte()
132134
);
133135
});
134-
135-
Coroutine::create(function () use ($timerId) {
136-
CoordinatorManager::until(Constants::WORKER_EXIT)->yield();
137-
$this->timer->clear($timerId);
138-
});
139136
}
140137
}

src/sentry/src/Metrics/Listener/OnWorkerStart.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
use FriendsOfHyperf\Sentry\Feature;
1515
use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady;
1616
use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter;
17-
use Hyperf\Coordinator\Constants;
18-
use Hyperf\Coordinator\CoordinatorManager;
1917
use Hyperf\Coordinator\Timer;
20-
use Hyperf\Engine\Coroutine;
2118
use Hyperf\Event\Contract\ListenerInterface;
2219
use Hyperf\Framework\Event\BeforeWorkerStart;
2320
use Psr\Container\ContainerInterface;
@@ -90,7 +87,11 @@ public function process(object $event): void
9087
'ru_stime_tv_sec',
9188
];
9289

93-
$timerId = $this->timer->tick($this->feature->getMetricsInterval(), function () use ($metrics, $event) {
90+
$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics, $event) {
91+
if ($isClosing) {
92+
return Timer::STOP;
93+
}
94+
9495
defer(fn () => metrics()->flush());
9596

9697
$server = $this->container->get(Server::class);
@@ -121,10 +122,5 @@ public function process(object $event): void
121122
Unit::megabyte()
122123
);
123124
});
124-
125-
Coroutine::create(function () use ($timerId) {
126-
CoordinatorManager::until(Constants::WORKER_EXIT)->yield();
127-
$this->timer->clear($timerId);
128-
});
129125
}
130126
}

src/sentry/src/Metrics/Listener/PoolWatcher.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
namespace FriendsOfHyperf\Sentry\Metrics\Listener;
1313

1414
use FriendsOfHyperf\Sentry\Feature;
15-
use Hyperf\Coordinator\Constants;
16-
use Hyperf\Coordinator\CoordinatorManager;
1715
use Hyperf\Coordinator\Timer;
18-
use Hyperf\Engine\Coroutine;
1916
use Hyperf\Event\Contract\ListenerInterface;
2017
use Hyperf\Framework\Event\BeforeWorkerStart;
2118
use Hyperf\Pool\Pool;
@@ -65,11 +62,15 @@ public function watch(Pool $pool, string $poolName, int $workerId): void
6562
return;
6663
}
6764

68-
$timerId = $this->timer->tick($this->feature->getMetricsInterval(), function () use (
65+
$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use (
6966
$pool,
7067
$workerId,
7168
$poolName
7269
) {
70+
if ($isClosing) {
71+
return Timer::STOP;
72+
}
73+
7374
defer(fn () => metrics()->flush());
7475

7576
metrics()->gauge(
@@ -97,10 +98,5 @@ public function watch(Pool $pool, string $poolName, int $workerId): void
9798
]
9899
);
99100
});
100-
101-
Coroutine::create(function () use ($timerId) {
102-
CoordinatorManager::until(Constants::WORKER_EXIT)->yield();
103-
$this->timer->clear($timerId);
104-
});
105101
}
106102
}

src/sentry/src/Metrics/Listener/QueueWatcher.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady;
1616
use Hyperf\AsyncQueue\Driver\DriverFactory;
1717
use Hyperf\Contract\ConfigInterface;
18-
use Hyperf\Coordinator\Constants;
19-
use Hyperf\Coordinator\CoordinatorManager;
2018
use Hyperf\Coordinator\Timer;
21-
use Hyperf\Engine\Coroutine;
2219
use Hyperf\Event\Contract\ListenerInterface;
2320
use Psr\Container\ContainerInterface;
2421

@@ -55,7 +52,11 @@ public function process(object $event): void
5552
return;
5653
}
5754

58-
$timerId = $this->timer->tick($this->feature->getMetricsInterval(), function () {
55+
$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) {
56+
if ($isClosing) {
57+
return Timer::STOP;
58+
}
59+
5960
defer(fn () => metrics()->flush());
6061

6162
$config = $this->container->get(ConfigInterface::class);
@@ -87,10 +88,5 @@ public function process(object $event): void
8788
);
8889
}
8990
});
90-
91-
Coroutine::create(function () use ($timerId) {
92-
CoordinatorManager::until(Constants::WORKER_EXIT)->yield();
93-
$this->timer->clear($timerId);
94-
});
9591
}
9692
}

0 commit comments

Comments
 (0)