From 59b9b12e52aaabc2d92de8bcc6c12f7a1c49c9a6 Mon Sep 17 00:00:00 2001 From: chadicus Date: Mon, 1 Dec 2025 17:15:29 -0500 Subject: [PATCH] Allow ability to limit concurrency when making requests --- src/GuzzleAdapter.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/GuzzleAdapter.php b/src/GuzzleAdapter.php index 9178b80..37bf5e9 100644 --- a/src/GuzzleAdapter.php +++ b/src/GuzzleAdapter.php @@ -16,6 +16,11 @@ */ final class GuzzleAdapter implements AdapterInterface { + /** + * @var int + */ + const DEFAULT_CONCURRENCY_LIMIT = PHP_INT_MAX; + /** * Collection of Promise\PromiseInterface instances with keys matching what was given from start(). * @@ -37,13 +42,20 @@ final class GuzzleAdapter implements AdapterInterface */ private $exceptions; + /** + * @var int + */ + private $concurrencyLimit; + /** * @var GuzzleClientInterface */ private $client; - public function __construct(GuzzleClientInterface $client = null) - { + public function __construct( + GuzzleClientInterface $client = null, + int $concurrencyLimit = self::DEFAULT_CONCURRENCY_LIMIT + ) { $this->exceptions = new ArrayObject(); $this->client = $client ?? new GuzzleClient( [ @@ -51,6 +63,7 @@ public function __construct(GuzzleClientInterface $client = null) 'http_errors' => false, //only for 400/500 error codes, actual exceptions can still happen ] ); + $this->concurrencyLimit = $concurrencyLimit; } /** @@ -122,8 +135,9 @@ private function fulfillPromises(array $promises, ArrayObject $exceptions) : arr } $results = new ArrayObject(); - Promise\each( + Promise\Each::ofLimit( $this->promises, + $this->concurrencyLimit, function (ResponseInterface $response, $index) use ($results) { $results[$index] = $response; },