From db99b9b9eccce8fd726d3bcf0b3af4c546023bd9 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Fri, 12 Sep 2025 20:35:12 +0800 Subject: [PATCH 01/21] fix(sentry): optimize HttpClient memory usage and error handling - Replace Closure storage with direct parameter arrays to reduce memory overhead - Improve parameter cleanup in finally block to prevent memory leaks - Remove unused Closure import - Add clearer comments for loop lifecycle management - Streamline channel initialization and cleanup logic --- src/sentry/src/HttpClient/HttpClient.php | 28 ++++++++++++++---------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index c89f2afd3..29ee24de1 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -11,7 +11,6 @@ namespace FriendsOfHyperf\Sentry\HttpClient; -use Closure; use Hyperf\Coordinator\Constants; use Hyperf\Coordinator\CoordinatorManager; use Hyperf\Coroutine\Concurrent; @@ -47,53 +46,57 @@ public function __construct( public function sendRequest(Request $request, Options $options): Response { + // Start the loop if not started yet $this->loop(); - $chan = $this->chan; - $chan->push(fn () => parent::sendRequest($request, $options)); + // Push the request to the channel + $this->chan?->push([$request, $options]); return new Response(202, ['X-Sentry-Request-Status' => ['Queued']], ''); } public function close(): void { - $chan = $this->chan; + $this->chan?->close(); $this->chan = null; - - $chan?->close(); } protected function loop(): void { + // The loop already started if ($this->chan != null) { return; } + // The worker already exited if ($this->workerExited) { return; } + // Initialize the channel and start the loop $this->chan = new Channel($this->channelSize); + // Start the loop Coroutine::create(function () { try { while (true) { while (true) { - /** @var Closure|null $closure */ - $closure = $this->chan?->pop(); - if (! $closure) { + /** @var array{0:Request,1:Options}|null $params */ + $params = $this->chan?->pop(); + if (! $params) { break 2; } try { + $callable = fn () => parent::sendRequest(...$params); if ($this->concurrent) { - $this->concurrent->create($closure); + $this->concurrent->create($callable); } else { - Coroutine::create($closure); + Coroutine::create($callable); } } catch (Throwable) { break; } finally { - $closure = null; + $params = null; } } } @@ -103,6 +106,7 @@ protected function loop(): void } }); + // Wait for the worker exit event $this->waitingWorkerExit ??= Coroutine::create(function () { try { CoordinatorManager::until(Constants::WORKER_EXIT)->yield(); From e1f73f06620c06727a318c01322f295af2b7143d Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 13 Sep 2025 14:05:09 +0800 Subject: [PATCH 02/21] fix(HttpClient): improve channel initialization and request handling logic --- src/sentry/src/HttpClient/HttpClient.php | 50 ++++++++++++------------ 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index 29ee24de1..783cd139b 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -21,6 +21,8 @@ use Sentry\Options; use Throwable; +use function Hyperf\Tappable\tap; + class HttpClient extends \Sentry\HttpClient\HttpClient { protected ?Channel $chan = null; @@ -74,36 +76,34 @@ protected function loop(): void } // Initialize the channel and start the loop - $this->chan = new Channel($this->channelSize); - - // Start the loop - Coroutine::create(function () { - try { - while (true) { + $this->chan ??= tap(new Channel($this->channelSize), function (Channel $chan) { + // Start the loop + Coroutine::create(function () use ($chan) { + try { while (true) { - /** @var array{0:Request,1:Options}|null $params */ - $params = $this->chan?->pop(); - if (! $params) { - break 2; - } - try { - $callable = fn () => parent::sendRequest(...$params); - if ($this->concurrent) { - $this->concurrent->create($callable); - } else { - Coroutine::create($callable); + while (true) { + if ($chan->isClosing() || ! $params = $chan->pop()) { + break 2; + } + try { + $callable = fn () => parent::sendRequest(...$params); + if ($this->concurrent) { + $this->concurrent->create($callable); + } else { + Coroutine::create($callable); + } + } catch (Throwable) { + break; + } finally { + $params = null; } - } catch (Throwable) { - break; - } finally { - $params = null; } } + } catch (Throwable $e) { + } finally { + $this->close(); } - } catch (Throwable $e) { - } finally { - $this->close(); - } + }); }); // Wait for the worker exit event From 0c75decdc7e9124e42645ef5dbd8c16929b148d6 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 13 Sep 2025 14:05:27 +0800 Subject: [PATCH 03/21] fix(HttpClient): prevent redundant loop initialization in channel management --- src/sentry/src/HttpClient/HttpClient.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index 783cd139b..c35a18b96 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -65,11 +65,6 @@ public function close(): void protected function loop(): void { - // The loop already started - if ($this->chan != null) { - return; - } - // The worker already exited if ($this->workerExited) { return; From 6889d4d2c956630e59fec93d7713f3697146242d Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 13 Sep 2025 14:13:50 +0800 Subject: [PATCH 04/21] fix(sentry): increase HTTP concurrent limit to improve request handling --- src/sentry/publish/sentry.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/publish/sentry.php b/src/sentry/publish/sentry.php index c19debaad..8d0c76052 100644 --- a/src/sentry/publish/sentry.php +++ b/src/sentry/publish/sentry.php @@ -130,5 +130,5 @@ 'http_timeout' => (float) env('SENTRY_HTTP_TIMEOUT', 2.0), 'http_chanel_size' => (int) env('SENTRY_HTTP_CHANEL_SIZE', 65535), - 'http_concurrent_limit' => (int) env('SENTRY_HTTP_CONCURRENT_LIMIT', 100), + 'http_concurrent_limit' => (int) env('SENTRY_HTTP_CONCURRENT_LIMIT', 1000), ]; From 1eaaa87c48434fddea48fd53d93190ffe05ff4bc Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 13 Sep 2025 14:14:10 +0800 Subject: [PATCH 05/21] fix(HttpClient): increase default concurrent limit to improve request handling --- src/sentry/src/HttpClient/HttpClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index c35a18b96..a75e3c74a 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -37,7 +37,7 @@ public function __construct( string $sdkIdentifier, string $sdkVersion, protected int $channelSize = 65535, - int $concurrentLimit = 100, + int $concurrentLimit = 1000, ) { parent::__construct($sdkIdentifier, $sdkVersion); From 93a1026dd66630057d3323b0d83bf53a3e885f5e Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 13 Sep 2025 14:15:05 +0800 Subject: [PATCH 06/21] fix(HttpClient): add memory usage and channel size logging in loop initialization --- src/sentry/src/HttpClient/HttpClient.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index a75e3c74a..e4a00a781 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -72,6 +72,15 @@ protected function loop(): void // Initialize the channel and start the loop $this->chan ??= tap(new Channel($this->channelSize), function (Channel $chan) { + // Dump memory usage and channel size + Coroutine::create(function () use ($chan) { + while (! $chan->isClosing()) { + dump('Memory Usage(MB): ' . memory_get_usage(true) / 1024 / 1024); + dump('Channel Size: ' . $chan->getLength()); + sleep(1); + } + }); + // Start the loop Coroutine::create(function () use ($chan) { try { @@ -81,11 +90,11 @@ protected function loop(): void break 2; } try { - $callable = fn () => parent::sendRequest(...$params); + $closure = fn () => parent::sendRequest(...$params); if ($this->concurrent) { - $this->concurrent->create($callable); + $this->concurrent->create($closure); } else { - Coroutine::create($callable); + Coroutine::create($closure); } } catch (Throwable) { break; From fdf031a8044c7dc197914ce8e19604748e61cd62 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 13 Sep 2025 14:17:00 +0800 Subject: [PATCH 07/21] fix(HttpClient): rename variable for clarity in error handling --- src/sentry/src/HttpClient/HttpClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index e4a00a781..785c86d14 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -99,7 +99,7 @@ protected function loop(): void } catch (Throwable) { break; } finally { - $params = null; + $closure = null; } } } From 15cc22a4193b2b35bf8610da74c1507e14539d9f Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 13 Sep 2025 14:21:43 +0800 Subject: [PATCH 08/21] fix(HttpClient): exit loop if channel is closing or pop fails --- src/sentry/src/HttpClient/HttpClient.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index 785c86d14..846b71e1e 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -86,6 +86,7 @@ protected function loop(): void try { while (true) { while (true) { + // If the channel is closing or pop failed, exit the loop if ($chan->isClosing() || ! $params = $chan->pop()) { break 2; } From 9ec9e21b3abe81c83211ffa11cf8d0e06708bc40 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 13 Sep 2025 14:25:15 +0800 Subject: [PATCH 09/21] fix(HttpClient): comment out memory usage and channel size logging in loop initialization --- src/sentry/src/HttpClient/HttpClient.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index 846b71e1e..93235fb4e 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -73,13 +73,13 @@ protected function loop(): void // Initialize the channel and start the loop $this->chan ??= tap(new Channel($this->channelSize), function (Channel $chan) { // Dump memory usage and channel size - Coroutine::create(function () use ($chan) { - while (! $chan->isClosing()) { - dump('Memory Usage(MB): ' . memory_get_usage(true) / 1024 / 1024); - dump('Channel Size: ' . $chan->getLength()); - sleep(1); - } - }); + // Coroutine::create(function () use ($chan) { + // while (! $chan->isClosing()) { + // dump('Memory Usage(MB): ' . memory_get_usage(true) / 1024 / 1024); + // dump('Channel Size: ' . $chan->getLength()); + // sleep(1); + // } + // }); // Start the loop Coroutine::create(function () use ($chan) { From a4a6a671f33f0e187f056fa733cd3488436de852 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 13 Sep 2025 14:56:47 +0800 Subject: [PATCH 10/21] fix(HttpClient): use func_get_args() for pushing requests to the channel --- src/sentry/src/HttpClient/HttpClient.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index 93235fb4e..1b2d50d4b 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -52,7 +52,7 @@ public function sendRequest(Request $request, Options $options): Response $this->loop(); // Push the request to the channel - $this->chan?->push([$request, $options]); + $this->chan?->push(func_get_args()); return new Response(202, ['X-Sentry-Request-Status' => ['Queued']], ''); } @@ -87,11 +87,11 @@ protected function loop(): void while (true) { while (true) { // If the channel is closing or pop failed, exit the loop - if ($chan->isClosing() || ! $params = $chan->pop()) { + if ($chan->isClosing() || ! $args = $chan->pop()) { break 2; } try { - $closure = fn () => parent::sendRequest(...$params); + $closure = fn () => parent::sendRequest(...$args); if ($this->concurrent) { $this->concurrent->create($closure); } else { From 51bd63a0584ab2658df9959da49b58121db2e581 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 13 Sep 2025 15:07:56 +0800 Subject: [PATCH 11/21] fix(sentry): reduce default HTTP concurrent limit from 1000 to 100 --- src/sentry/publish/sentry.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/publish/sentry.php b/src/sentry/publish/sentry.php index 8d0c76052..c19debaad 100644 --- a/src/sentry/publish/sentry.php +++ b/src/sentry/publish/sentry.php @@ -130,5 +130,5 @@ 'http_timeout' => (float) env('SENTRY_HTTP_TIMEOUT', 2.0), 'http_chanel_size' => (int) env('SENTRY_HTTP_CHANEL_SIZE', 65535), - 'http_concurrent_limit' => (int) env('SENTRY_HTTP_CONCURRENT_LIMIT', 1000), + 'http_concurrent_limit' => (int) env('SENTRY_HTTP_CONCURRENT_LIMIT', 100), ]; From 4cc0b5c08ec24fd1f476fe303bccec85e4520a7b Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Sat, 13 Sep 2025 15:17:07 +0800 Subject: [PATCH 12/21] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20HttpClient.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sentry/src/HttpClient/HttpClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index 1b2d50d4b..e21ba29bb 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -37,7 +37,7 @@ public function __construct( string $sdkIdentifier, string $sdkVersion, protected int $channelSize = 65535, - int $concurrentLimit = 1000, + int $concurrentLimit = 100, ) { parent::__construct($sdkIdentifier, $sdkVersion); From 433b9f525e0c98b1883a32f66e1781b7a911e285 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 14 Sep 2025 18:32:27 +0800 Subject: [PATCH 13/21] fix(HttpClient): rename closure variable to callable for clarity --- src/sentry/src/HttpClient/HttpClient.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index e21ba29bb..3aac49431 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -91,16 +91,16 @@ protected function loop(): void break 2; } try { - $closure = fn () => parent::sendRequest(...$args); + $callable = fn () => parent::sendRequest(...$args); if ($this->concurrent) { - $this->concurrent->create($closure); + $this->concurrent->create($callable); } else { - Coroutine::create($closure); + Coroutine::create($callable); } } catch (Throwable) { break; } finally { - $closure = null; + $callable = null; } } } From c30d56095e234bee47c178be4d653905c7a2a095 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 14 Sep 2025 18:34:35 +0800 Subject: [PATCH 14/21] fix(HttpClient): refactor request channel push and restore close method --- src/sentry/src/HttpClient/HttpClient.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index 3aac49431..97360a3b1 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -52,17 +52,12 @@ public function sendRequest(Request $request, Options $options): Response $this->loop(); // Push the request to the channel - $this->chan?->push(func_get_args()); + $chan = $this->chan; + $chan?->push(func_get_args()); return new Response(202, ['X-Sentry-Request-Status' => ['Queued']], ''); } - public function close(): void - { - $this->chan?->close(); - $this->chan = null; - } - protected function loop(): void { // The worker already exited @@ -121,4 +116,14 @@ protected function loop(): void } }); } + + protected function close(): void + { + $chan = $this->chan; + $chan?->close(); + + if ($this->chan === $chan) { + $this->chan = null; + } + } } From 54ad2f2bbeec4ef4ecacf9a223dc5787010fedbe Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 14 Sep 2025 18:42:05 +0800 Subject: [PATCH 15/21] fix(HttpClient): clear callable and args variables after use --- src/sentry/src/HttpClient/HttpClient.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index 97360a3b1..47a5a548a 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -96,6 +96,7 @@ protected function loop(): void break; } finally { $callable = null; + $args = null; } } } From e7e11e35d70881d812b5cf9839283ecb3eab1111 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Mon, 15 Sep 2025 10:41:29 +0800 Subject: [PATCH 16/21] fix(HttpClient): update request pushing to use explicit parameters --- src/sentry/src/HttpClient/HttpClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index 47a5a548a..9aacc293d 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -53,7 +53,7 @@ public function sendRequest(Request $request, Options $options): Response // Push the request to the channel $chan = $this->chan; - $chan?->push(func_get_args()); + $chan?->push([$request, $options]); return new Response(202, ['X-Sentry-Request-Status' => ['Queued']], ''); } From e78deb2e6626603512c1abde8d30c29d70b3d886 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Mon, 15 Sep 2025 11:00:08 +0800 Subject: [PATCH 17/21] fix(HttpClient): ensure channel is closed after request processing --- src/sentry/src/HttpClient/HttpClient.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index 9aacc293d..8a4135f10 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -102,6 +102,7 @@ protected function loop(): void } } catch (Throwable $e) { } finally { + $chan->close(); $this->close(); } }); From ec1d33510d53a8e55dbfb916eec152c7994eb81d Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Mon, 15 Sep 2025 12:03:39 +0800 Subject: [PATCH 18/21] fix(HttpClient): simplify channel initialization and loop handling --- src/sentry/src/HttpClient/HttpClient.php | 67 ++++++++++-------------- 1 file changed, 29 insertions(+), 38 deletions(-) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index 8a4135f10..fb5ef22c9 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -21,8 +21,6 @@ use Sentry\Options; use Throwable; -use function Hyperf\Tappable\tap; - class HttpClient extends \Sentry\HttpClient\HttpClient { protected ?Channel $chan = null; @@ -65,47 +63,40 @@ protected function loop(): void return; } - // Initialize the channel and start the loop - $this->chan ??= tap(new Channel($this->channelSize), function (Channel $chan) { - // Dump memory usage and channel size - // Coroutine::create(function () use ($chan) { - // while (! $chan->isClosing()) { - // dump('Memory Usage(MB): ' . memory_get_usage(true) / 1024 / 1024); - // dump('Channel Size: ' . $chan->getLength()); - // sleep(1); - // } - // }); - - // Start the loop - Coroutine::create(function () use ($chan) { - try { + if ($this->chan !== null) { + return; + } + + $this->chan = new Channel($this->channelSize); + + // Start the loop + Coroutine::create(function () { + try { + while (true) { while (true) { - while (true) { - // If the channel is closing or pop failed, exit the loop - if ($chan->isClosing() || ! $args = $chan->pop()) { - break 2; - } - try { - $callable = fn () => parent::sendRequest(...$args); - if ($this->concurrent) { - $this->concurrent->create($callable); - } else { - Coroutine::create($callable); - } - } catch (Throwable) { - break; - } finally { - $callable = null; - $args = null; + // If the channel is closing or pop failed, exit the loop + if (! $args = $this->chan?->pop()) { + break 2; + } + try { + $callable = fn () => parent::sendRequest(...$args); + if ($this->concurrent) { + $this->concurrent->create($callable); + } else { + Coroutine::create($callable); } + } catch (Throwable) { + break; + } finally { + $callable = null; + $args = null; } } - } catch (Throwable $e) { - } finally { - $chan->close(); - $this->close(); } - }); + } catch (Throwable $e) { + } finally { + $this->close(); + } }); // Wait for the worker exit event From 708c21e65da97fa32987e14141499d242bfa2293 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Mon, 15 Sep 2025 12:08:29 +0800 Subject: [PATCH 19/21] Revert "fix(HttpClient): simplify channel initialization and loop handling" This reverts commit ec1d33510d53a8e55dbfb916eec152c7994eb81d. --- src/sentry/src/HttpClient/HttpClient.php | 67 ++++++++++++++---------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index fb5ef22c9..8a4135f10 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -21,6 +21,8 @@ use Sentry\Options; use Throwable; +use function Hyperf\Tappable\tap; + class HttpClient extends \Sentry\HttpClient\HttpClient { protected ?Channel $chan = null; @@ -63,40 +65,47 @@ protected function loop(): void return; } - if ($this->chan !== null) { - return; - } - - $this->chan = new Channel($this->channelSize); - - // Start the loop - Coroutine::create(function () { - try { - while (true) { + // Initialize the channel and start the loop + $this->chan ??= tap(new Channel($this->channelSize), function (Channel $chan) { + // Dump memory usage and channel size + // Coroutine::create(function () use ($chan) { + // while (! $chan->isClosing()) { + // dump('Memory Usage(MB): ' . memory_get_usage(true) / 1024 / 1024); + // dump('Channel Size: ' . $chan->getLength()); + // sleep(1); + // } + // }); + + // Start the loop + Coroutine::create(function () use ($chan) { + try { while (true) { - // If the channel is closing or pop failed, exit the loop - if (! $args = $this->chan?->pop()) { - break 2; - } - try { - $callable = fn () => parent::sendRequest(...$args); - if ($this->concurrent) { - $this->concurrent->create($callable); - } else { - Coroutine::create($callable); + while (true) { + // If the channel is closing or pop failed, exit the loop + if ($chan->isClosing() || ! $args = $chan->pop()) { + break 2; + } + try { + $callable = fn () => parent::sendRequest(...$args); + if ($this->concurrent) { + $this->concurrent->create($callable); + } else { + Coroutine::create($callable); + } + } catch (Throwable) { + break; + } finally { + $callable = null; + $args = null; } - } catch (Throwable) { - break; - } finally { - $callable = null; - $args = null; } } + } catch (Throwable $e) { + } finally { + $chan->close(); + $this->close(); } - } catch (Throwable $e) { - } finally { - $this->close(); - } + }); }); // Wait for the worker exit event From 52dd80be53f06e825b693200f8d6982a5aa50ecf Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Mon, 15 Sep 2025 12:09:52 +0800 Subject: [PATCH 20/21] fix(HttpClient): refactor channel initialization and loop handling --- src/sentry/src/HttpClient/HttpClient.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index 8a4135f10..3558644a2 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -66,7 +66,7 @@ protected function loop(): void } // Initialize the channel and start the loop - $this->chan ??= tap(new Channel($this->channelSize), function (Channel $chan) { + $this->chan ??= tap(new Channel($this->channelSize), function () { // Dump memory usage and channel size // Coroutine::create(function () use ($chan) { // while (! $chan->isClosing()) { @@ -77,12 +77,12 @@ protected function loop(): void // }); // Start the loop - Coroutine::create(function () use ($chan) { + Coroutine::create(function () { try { while (true) { while (true) { // If the channel is closing or pop failed, exit the loop - if ($chan->isClosing() || ! $args = $chan->pop()) { + if (! $args = $this->chan->pop()) { break 2; } try { @@ -102,7 +102,6 @@ protected function loop(): void } } catch (Throwable $e) { } finally { - $chan->close(); $this->close(); } }); From a80204a05865ed42644d56ee62dba88bca53dd6a Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Mon, 15 Sep 2025 14:05:45 +0800 Subject: [PATCH 21/21] fix(HttpClient): update channel reference in memory usage logging --- src/sentry/src/HttpClient/HttpClient.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sentry/src/HttpClient/HttpClient.php b/src/sentry/src/HttpClient/HttpClient.php index 3558644a2..b97970644 100644 --- a/src/sentry/src/HttpClient/HttpClient.php +++ b/src/sentry/src/HttpClient/HttpClient.php @@ -68,10 +68,10 @@ protected function loop(): void // Initialize the channel and start the loop $this->chan ??= tap(new Channel($this->channelSize), function () { // Dump memory usage and channel size - // Coroutine::create(function () use ($chan) { - // while (! $chan->isClosing()) { + // Coroutine::create(function () { + // while (! $this->chan?->isClosing()) { // dump('Memory Usage(MB): ' . memory_get_usage(true) / 1024 / 1024); - // dump('Channel Size: ' . $chan->getLength()); + // dump('Channel Size: ' . $this->chan?->getLength()); // sleep(1); // } // }); @@ -82,7 +82,7 @@ protected function loop(): void while (true) { while (true) { // If the channel is closing or pop failed, exit the loop - if (! $args = $this->chan->pop()) { + if (! $args = $this->chan?->pop()) { break 2; } try {