Skip to content

Commit

Permalink
perf(src): improve performance of Client pool method
Browse files Browse the repository at this point in the history
- Refactored pool method to use a generator for sending async requests
- Updated method to use Utils::unwrap with generator for better performance
  • Loading branch information
guanguans committed Jul 9, 2024
1 parent e139eab commit 8e94cea
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/Foundation/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array-key, Message> $messages
*
Expand All @@ -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)
);
}
}

0 comments on commit 8e94cea

Please sign in to comment.