2 namespace GuzzleHttp\Handler;
4 use GuzzleHttp\Promise as P;
5 use GuzzleHttp\Promise\Promise;
7 use Psr\Http\Message\RequestInterface;
10 * Returns an asynchronous response using curl_multi_* functions.
12 * When using the CurlMultiHandler, custom curl options can be specified as an
13 * associative array of curl option constants mapping to values in the
14 * **curl** key of the provided request options.
16 * @property resource $_mh Internal use only. Lazy loaded multi-handle.
18 class CurlMultiHandler
20 /** @var CurlFactoryInterface */
22 private $selectTimeout;
24 private $handles = [];
28 * This handler accepts the following options:
30 * - handle_factory: An optional factory used to create curl handles
31 * - select_timeout: Optional timeout (in seconds) to block before timing
32 * out while selecting curl handles. Defaults to 1 second.
34 * @param array $options
36 public function __construct(array $options = [])
38 $this->factory = isset($options['handle_factory'])
39 ? $options['handle_factory'] : new CurlFactory(50);
40 $this->selectTimeout = isset($options['select_timeout'])
41 ? $options['select_timeout'] : 1;
44 public function __get($name)
46 if ($name === '_mh') {
47 return $this->_mh = curl_multi_init();
50 throw new \BadMethodCallException();
53 public function __destruct()
55 if (isset($this->_mh)) {
56 curl_multi_close($this->_mh);
61 public function __invoke(RequestInterface $request, array $options)
63 $easy = $this->factory->create($request, $options);
64 $id = (int) $easy->handle;
66 $promise = new Promise(
68 function () use ($id) {
69 return $this->cancel($id);
73 $this->addRequest(['easy' => $easy, 'deferred' => $promise]);
79 * Ticks the curl event loop.
81 public function tick()
83 // Add any delayed handles if needed.
85 $currentTime = microtime(true);
86 foreach ($this->delays as $id => $delay) {
87 if ($currentTime >= $delay) {
88 unset($this->delays[$id]);
89 curl_multi_add_handle(
91 $this->handles[$id]['easy']->handle
97 // Step through the task queue which may add additional requests.
101 curl_multi_select($this->_mh, $this->selectTimeout) === -1
103 // Perform a usleep if a select returns -1.
104 // See: https://bugs.php.net/bug.php?id=61141
108 while (curl_multi_exec($this->_mh, $this->active) === CURLM_CALL_MULTI_PERFORM);
110 $this->processMessages();
114 * Runs until all outstanding connections have completed.
116 public function execute()
120 while ($this->handles || !$queue->isEmpty()) {
121 // If there are no transfers, then sleep for the next delay
122 if (!$this->active && $this->delays) {
123 usleep($this->timeToNext());
129 private function addRequest(array $entry)
131 $easy = $entry['easy'];
132 $id = (int) $easy->handle;
133 $this->handles[$id] = $entry;
134 if (empty($easy->options['delay'])) {
135 curl_multi_add_handle($this->_mh, $easy->handle);
137 $this->delays[$id] = microtime(true) + ($easy->options['delay'] / 1000);
142 * Cancels a handle from sending and removes references to it.
144 * @param int $id Handle ID to cancel and remove.
146 * @return bool True on success, false on failure.
148 private function cancel($id)
150 // Cannot cancel if it has been processed.
151 if (!isset($this->handles[$id])) {
155 $handle = $this->handles[$id]['easy']->handle;
156 unset($this->delays[$id], $this->handles[$id]);
157 curl_multi_remove_handle($this->_mh, $handle);
163 private function processMessages()
165 while ($done = curl_multi_info_read($this->_mh)) {
166 $id = (int) $done['handle'];
167 curl_multi_remove_handle($this->_mh, $done['handle']);
169 if (!isset($this->handles[$id])) {
170 // Probably was cancelled.
174 $entry = $this->handles[$id];
175 unset($this->handles[$id], $this->delays[$id]);
176 $entry['easy']->errno = $done['result'];
177 $entry['deferred']->resolve(
187 private function timeToNext()
189 $currentTime = microtime(true);
190 $nextTime = PHP_INT_MAX;
191 foreach ($this->delays as $time) {
192 if ($time < $nextTime) {
197 return max(0, $nextTime - $currentTime) * 1000000;