Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/sentry/src/Metrics/Listener/OnBeforeHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
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;

use function FriendsOfHyperf\Sentry\metrics;
use function Hyperf\Coroutine\defer;

class OnBeforeHandle implements ListenerInterface
{
Expand Down Expand Up @@ -89,7 +88,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());
Expand All @@ -108,10 +111,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);
});
}
}
14 changes: 5 additions & 9 deletions src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Comment on lines +100 to +103
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refactoring assumes that Timer::tick() passes an $isClosing parameter to the callback during shutdown, but this doesn't appear to be how Hyperf's Coordinator\Timer works. The Timer::tick() method in Hyperf doesn't automatically invoke callbacks with a closing signal.

The original pattern using CoordinatorManager::until(Constants::WORKER_EXIT) to wait for worker exit and then calling timer->clear() is the established Hyperf pattern for cleanup. Without this, the timers may not be properly cleaned up during shutdown, potentially causing memory leaks or preventing graceful shutdowns.

Copilot uses AI. Check for mistakes.

defer(fn () => metrics()->flush());

$this->trySet('gc_', $metrics, gc_status());
Expand All @@ -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);
});
}
}
13 changes: 5 additions & 8 deletions src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Comment on lines +99 to +102
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refactoring assumes that Timer::tick() passes an $isClosing parameter to the callback during shutdown, but this doesn't appear to be how Hyperf's Coordinator\Timer works. The Timer::tick() method in Hyperf doesn't automatically invoke callbacks with a closing signal.

The original pattern using CoordinatorManager::until(Constants::WORKER_EXIT) to wait for worker exit and then calling timer->clear() is the established Hyperf pattern for cleanup. Without this, the timers may not be properly cleaned up during shutdown, potentially causing memory leaks or preventing graceful shutdowns.

Copilot uses AI. Check for mistakes.

defer(fn () => metrics()->flush());

$this->trySet('', $metrics, Coroutine::stats(), $workerId);
Expand Down Expand Up @@ -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);
});
}
}
14 changes: 5 additions & 9 deletions src/sentry/src/Metrics/Listener/OnWorkerStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Comment on lines +90 to +93

This comment was marked as outdated.

Comment on lines +90 to +93
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refactoring assumes that Timer::tick() passes an $isClosing parameter to the callback during shutdown, but this doesn't appear to be how Hyperf's Coordinator\Timer works. The Timer::tick() method in Hyperf doesn't automatically invoke callbacks with a closing signal.

The original pattern using CoordinatorManager::until(Constants::WORKER_EXIT) to wait for worker exit and then calling timer->clear() is the established Hyperf pattern for cleanup. Without this, the timers may not be properly cleaned up during shutdown, potentially causing memory leaks or preventing graceful shutdowns.

Copilot uses AI. Check for mistakes.

defer(fn () => metrics()->flush());

$server = $this->container->get(Server::class);
Expand Down Expand Up @@ -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);
});
}
}
14 changes: 5 additions & 9 deletions src/sentry/src/Metrics/Listener/PoolWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Comment on lines +65 to +72
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refactoring assumes that Timer::tick() passes an $isClosing parameter to the callback during shutdown, but this doesn't appear to be how Hyperf's Coordinator\Timer works. The Timer::tick() method in Hyperf doesn't automatically invoke callbacks with a closing signal.

The original pattern using CoordinatorManager::until(Constants::WORKER_EXIT) to wait for worker exit and then calling timer->clear() is the established Hyperf pattern for cleanup. Without this, the timers may not be properly cleaned up during shutdown, potentially causing memory leaks or preventing graceful shutdowns.

Copilot uses AI. Check for mistakes.

defer(fn () => metrics()->flush());

metrics()->gauge(
Expand Down Expand Up @@ -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);
});
}
}
14 changes: 5 additions & 9 deletions src/sentry/src/Metrics/Listener/QueueWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Comment on lines +55 to +58
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refactoring assumes that Timer::tick() passes an $isClosing parameter to the callback during shutdown, but this doesn't appear to be how Hyperf's Coordinator\Timer works. The Timer::tick() method in Hyperf doesn't automatically invoke callbacks with a closing signal.

Looking at other usages in this codebase (trigger component's HealthMonitor, RedisServerMutex), they return Timer::STOP based on runtime conditions they check themselves, not on a parameter passed by the Timer.

The original pattern using CoordinatorManager::until(Constants::WORKER_EXIT) to wait for worker exit and then calling timer->clear() is the established Hyperf pattern for cleanup. Without this, the timers may not be properly cleaned up during shutdown, potentially causing memory leaks or preventing graceful shutdowns.

Copilot uses AI. Check for mistakes.

defer(fn () => metrics()->flush());

$config = $this->container->get(ConfigInterface::class);
Expand Down Expand Up @@ -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);
});
}
}