From 8e94cea27e16170ada521ce6ab0b73cd710b9950 Mon Sep 17 00:00:00 2001 From: yaozm Date: Tue, 9 Jul 2024 18:15:53 +0800 Subject: [PATCH] perf(src): improve performance of Client pool method - Refactored pool method to use a generator for sending async requests - Updated method to use Utils::unwrap with generator for better performance --- src/Foundation/Client.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Foundation/Client.php b/src/Foundation/Client.php index ba6af28..05a2344 100644 --- a/src/Foundation/Client.php +++ b/src/Foundation/Client.php @@ -76,6 +76,7 @@ public function sendAsync(Message $message): PromiseInterface /** * @see https://docs.guzzlephp.org/en/stable/quickstart.html#concurrent-requests + * @see \GuzzleHttp\Pool * * @param iterable $messages * @@ -86,13 +87,14 @@ public function sendAsync(Message $message): PromiseInterface */ public function pool(iterable $messages): array { - $promises = []; - - foreach ($messages as $key => $message) { - $promises[$key] = $this->sendAsync($message); - } - // return Utils::settle($promises)->wait(); - return Utils::unwrap($promises); + /** @noinspection PhpParamsInspection */ + return Utils::unwrap( + (function (iterable $messages): \Generator { + foreach ($messages as $key => $message) { + yield $key => $this->sendAsync($message); + } + })($messages) + ); } }