This commit is contained in:
Javier Casares 2026-06-02 18:49:45 +00:00
commit 1dd5809016

View file

@ -6,6 +6,7 @@ use Closure;
use GuzzleHttp\Promise as P;
use GuzzleHttp\Promise\Promise;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\TransportSharing;
use GuzzleHttp\Utils;
use Psr\Http\Message\RequestInterface;
@ -25,6 +26,11 @@ class CurlMultiHandler
*/
private $factory;
/**
* @var CurlShareHandleState|null
*/
private $shareHandleState;
/**
* @var int
*/
@ -57,10 +63,21 @@ class CurlMultiHandler
/** @var resource|\CurlMultiHandle */
private $_mh;
/**
* @var bool
*/
private $executingMulti = false;
/**
* @var array<int, EasyHandle>
*/
private $deferredCancels = [];
/**
* This handler accepts the following options:
*
* - handle_factory: An optional factory used to create curl handles
* - transport_sharing: Optional transport sharing mode.
* - select_timeout: Optional timeout (in seconds) to block before timing
* out while selecting curl handles. Defaults to 1 second.
* - options: An associative array of CURLMOPT_* options and
@ -68,12 +85,27 @@ class CurlMultiHandler
*/
public function __construct(array $options = [])
{
$this->factory = $options['handle_factory'] ?? new CurlFactory(50);
CurlShareHandleState::assertNoRequiredSharingCustomFactoryConflict($options, 'CurlMultiHandler');
$transportSharing = $options['transport_sharing'] ?? null;
$sharingMode = CurlShareHandleState::normalizeMode($transportSharing, 'transport_sharing');
if (\array_key_exists('handle_factory', $options) && $options['handle_factory'] !== null) {
$this->shareHandleState = null;
$this->factory = $options['handle_factory'];
} else {
$this->shareHandleState = $sharingMode !== TransportSharing::NONE
? CurlShareHandleState::fromOption($transportSharing)
: null;
$this->factory = $this->shareHandleState !== null
? new CurlFactory(50, $this->shareHandleState->mode, $this->shareHandleState->handle)
: new CurlFactory(50);
}
if (isset($options['select_timeout'])) {
$this->selectTimeout = $options['select_timeout'];
} elseif ($selectTimeout = Utils::getenv('GUZZLE_CURL_SELECT_TIMEOUT')) {
@trigger_error('Since guzzlehttp/guzzle 7.2.0: Using environment variable GUZZLE_CURL_SELECT_TIMEOUT is deprecated. Use option "select_timeout" instead.', \E_USER_DEPRECATED);
\trigger_deprecation('guzzlehttp/guzzle', '7.2', 'The GUZZLE_CURL_SELECT_TIMEOUT environment variable is deprecated; use the "select_timeout" option instead.');
$this->selectTimeout = (int) $selectTimeout;
} else {
$this->selectTimeout = 1;
@ -119,8 +151,13 @@ class CurlMultiHandler
public function __destruct()
{
if (isset($this->_mh)) {
\curl_multi_close($this->_mh);
unset($this->_mh);
try {
\curl_multi_close($this->_mh);
} catch (\Throwable $e) {
// Destructors must not throw.
} finally {
unset($this->_mh);
}
}
}
@ -172,10 +209,21 @@ class CurlMultiHandler
\usleep(250);
}
while (\curl_multi_exec($this->_mh, $this->active) === \CURLM_CALL_MULTI_PERFORM) {
do {
$this->executingMulti = true;
try {
$exec = \curl_multi_exec($this->_mh, $this->active);
} finally {
$this->executingMulti = false;
$this->cleanupDeferredCancels();
}
// Prevent busy looping for slow HTTP requests.
\curl_multi_select($this->_mh, $this->selectTimeout);
}
if ($exec === \CURLM_CALL_MULTI_PERFORM) {
\curl_multi_select($this->_mh, $this->selectTimeout);
}
} while ($exec === \CURLM_CALL_MULTI_PERFORM);
$this->processMessages();
}
@ -185,7 +233,16 @@ class CurlMultiHandler
*/
private function tickInQueue(): void
{
if (\curl_multi_exec($this->_mh, $this->active) === \CURLM_CALL_MULTI_PERFORM) {
$this->executingMulti = true;
try {
$exec = \curl_multi_exec($this->_mh, $this->active);
} finally {
$this->executingMulti = false;
$this->cleanupDeferredCancels();
}
if ($exec === \CURLM_CALL_MULTI_PERFORM) {
\curl_multi_select($this->_mh, 0);
P\Utils::queue()->add(Closure::fromCallable([$this, 'tickInQueue']));
}
@ -229,7 +286,7 @@ class CurlMultiHandler
private function cancel($id): bool
{
if (!is_int($id)) {
trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an integer to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__);
\trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__);
}
// Cannot cancel if it has been processed.
@ -237,15 +294,42 @@ class CurlMultiHandler
return false;
}
$handle = $this->handles[$id]['easy']->handle;
$easy = $this->handles[$id]['easy'];
unset($this->delays[$id], $this->handles[$id]);
if ($this->executingMulti) {
$this->deferredCancels[$id] = $easy;
return true;
}
$this->cleanupCancelledHandle($easy);
return true;
}
private function cleanupDeferredCancels(): void
{
if ($this->deferredCancels === []) {
return;
}
$entries = $this->deferredCancels;
$this->deferredCancels = [];
foreach ($entries as $easy) {
$this->cleanupCancelledHandle($easy);
}
}
private function cleanupCancelledHandle(EasyHandle $easy): void
{
$handle = $easy->handle;
\curl_multi_remove_handle($this->_mh, $handle);
if (PHP_VERSION_ID < 80000) {
\curl_close($handle);
}
return true;
}
private function processMessages(): void
@ -255,6 +339,12 @@ class CurlMultiHandler
// if it's not done, then it would be premature to remove the handle. ref https://github.com/guzzle/guzzle/pull/2892#issuecomment-945150216
continue;
}
if (!isset($done['handle'])) {
// Work around a PHP issue where cancelled transfers may omit the handle.
// Remove this once we no longer support PHP versions before the fix in
// https://github.com/php/php-src/pull/16302.
continue;
}
$id = (int) $done['handle'];
\curl_multi_remove_handle($this->_mh, $done['handle']);
@ -266,9 +356,16 @@ class CurlMultiHandler
$entry = $this->handles[$id];
unset($this->handles[$id], $this->delays[$id]);
$entry['easy']->errno = $done['result'];
$entry['deferred']->resolve(
CurlFactory::finish($this, $entry['easy'], $this->factory)
);
try {
$result = CurlFactory::finish($this, $entry['easy'], $this->factory);
} catch (\Throwable $e) {
$entry['deferred']->reject($e);
continue;
}
$entry['deferred']->resolve($result);
}
}