CurlMultiHandler.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace GuzzleHttp\Handler;
  3. use Closure;
  4. use GuzzleHttp\Promise as P;
  5. use GuzzleHttp\Promise\Promise;
  6. use GuzzleHttp\Promise\PromiseInterface;
  7. use GuzzleHttp\Utils;
  8. use Psr\Http\Message\RequestInterface;
  9. /**
  10. * Returns an asynchronous response using curl_multi_* functions.
  11. *
  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.
  15. *
  16. * @final
  17. */
  18. class CurlMultiHandler
  19. {
  20. /**
  21. * @var CurlFactoryInterface
  22. */
  23. private $factory;
  24. /**
  25. * @var int
  26. */
  27. private $selectTimeout;
  28. /**
  29. * @var int Will be higher than 0 when `curl_multi_exec` is still running.
  30. */
  31. private $active = 0;
  32. /**
  33. * @var array Request entry handles, indexed by handle id in `addRequest`.
  34. *
  35. * @see CurlMultiHandler::addRequest
  36. */
  37. private $handles = [];
  38. /**
  39. * @var array<int, float> An array of delay times, indexed by handle id in `addRequest`.
  40. *
  41. * @see CurlMultiHandler::addRequest
  42. */
  43. private $delays = [];
  44. /**
  45. * @var array<mixed> An associative array of CURLMOPT_* options and corresponding values for curl_multi_setopt()
  46. */
  47. private $options = [];
  48. /** @var resource|\CurlMultiHandle */
  49. private $_mh;
  50. /**
  51. * This handler accepts the following options:
  52. *
  53. * - handle_factory: An optional factory used to create curl handles
  54. * - select_timeout: Optional timeout (in seconds) to block before timing
  55. * out while selecting curl handles. Defaults to 1 second.
  56. * - options: An associative array of CURLMOPT_* options and
  57. * corresponding values for curl_multi_setopt()
  58. */
  59. public function __construct(array $options = [])
  60. {
  61. $this->factory = $options['handle_factory'] ?? new CurlFactory(50);
  62. if (isset($options['select_timeout'])) {
  63. $this->selectTimeout = $options['select_timeout'];
  64. } elseif ($selectTimeout = Utils::getenv('GUZZLE_CURL_SELECT_TIMEOUT')) {
  65. @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);
  66. $this->selectTimeout = (int) $selectTimeout;
  67. } else {
  68. $this->selectTimeout = 1;
  69. }
  70. $this->options = $options['options'] ?? [];
  71. // unsetting the property forces the first access to go through
  72. // __get().
  73. unset($this->_mh);
  74. }
  75. /**
  76. * @param string $name
  77. *
  78. * @return resource|\CurlMultiHandle
  79. *
  80. * @throws \BadMethodCallException when another field as `_mh` will be gotten
  81. * @throws \RuntimeException when curl can not initialize a multi handle
  82. */
  83. public function __get($name)
  84. {
  85. if ($name !== '_mh') {
  86. throw new \BadMethodCallException("Can not get other property as '_mh'.");
  87. }
  88. $multiHandle = \curl_multi_init();
  89. if (false === $multiHandle) {
  90. throw new \RuntimeException('Can not initialize curl multi handle.');
  91. }
  92. $this->_mh = $multiHandle;
  93. foreach ($this->options as $option => $value) {
  94. // A warning is raised in case of a wrong option.
  95. curl_multi_setopt($this->_mh, $option, $value);
  96. }
  97. return $this->_mh;
  98. }
  99. public function __destruct()
  100. {
  101. if (isset($this->_mh)) {
  102. \curl_multi_close($this->_mh);
  103. unset($this->_mh);
  104. }
  105. }
  106. public function __invoke(RequestInterface $request, array $options): PromiseInterface
  107. {
  108. $easy = $this->factory->create($request, $options);
  109. $id = (int) $easy->handle;
  110. $promise = new Promise(
  111. [$this, 'execute'],
  112. function () use ($id) {
  113. return $this->cancel($id);
  114. }
  115. );
  116. $this->addRequest(['easy' => $easy, 'deferred' => $promise]);
  117. return $promise;
  118. }
  119. /**
  120. * Ticks the curl event loop.
  121. */
  122. public function tick(): void
  123. {
  124. // Add any delayed handles if needed.
  125. if ($this->delays) {
  126. $currentTime = Utils::currentTime();
  127. foreach ($this->delays as $id => $delay) {
  128. if ($currentTime >= $delay) {
  129. unset($this->delays[$id]);
  130. \curl_multi_add_handle(
  131. $this->_mh,
  132. $this->handles[$id]['easy']->handle
  133. );
  134. }
  135. }
  136. }
  137. // Run curl_multi_exec in the queue to enable other async tasks to run
  138. P\Utils::queue()->add(Closure::fromCallable([$this, 'tickInQueue']));
  139. // Step through the task queue which may add additional requests.
  140. P\Utils::queue()->run();
  141. if ($this->active && \curl_multi_select($this->_mh, $this->selectTimeout) === -1) {
  142. // Perform a usleep if a select returns -1.
  143. // See: https://bugs.php.net/bug.php?id=61141
  144. \usleep(250);
  145. }
  146. while (\curl_multi_exec($this->_mh, $this->active) === \CURLM_CALL_MULTI_PERFORM) {
  147. // Prevent busy looping for slow HTTP requests.
  148. \curl_multi_select($this->_mh, $this->selectTimeout);
  149. }
  150. $this->processMessages();
  151. }
  152. /**
  153. * Runs \curl_multi_exec() inside the event loop, to prevent busy looping
  154. */
  155. private function tickInQueue(): void
  156. {
  157. if (\curl_multi_exec($this->_mh, $this->active) === \CURLM_CALL_MULTI_PERFORM) {
  158. \curl_multi_select($this->_mh, 0);
  159. P\Utils::queue()->add(Closure::fromCallable([$this, 'tickInQueue']));
  160. }
  161. }
  162. /**
  163. * Runs until all outstanding connections have completed.
  164. */
  165. public function execute(): void
  166. {
  167. $queue = P\Utils::queue();
  168. while ($this->handles || !$queue->isEmpty()) {
  169. // If there are no transfers, then sleep for the next delay
  170. if (!$this->active && $this->delays) {
  171. \usleep($this->timeToNext());
  172. }
  173. $this->tick();
  174. }
  175. }
  176. private function addRequest(array $entry): void
  177. {
  178. $easy = $entry['easy'];
  179. $id = (int) $easy->handle;
  180. $this->handles[$id] = $entry;
  181. if (empty($easy->options['delay'])) {
  182. \curl_multi_add_handle($this->_mh, $easy->handle);
  183. } else {
  184. $this->delays[$id] = Utils::currentTime() + ($easy->options['delay'] / 1000);
  185. }
  186. }
  187. /**
  188. * Cancels a handle from sending and removes references to it.
  189. *
  190. * @param int $id Handle ID to cancel and remove.
  191. *
  192. * @return bool True on success, false on failure.
  193. */
  194. private function cancel($id): bool
  195. {
  196. if (!is_int($id)) {
  197. 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__);
  198. }
  199. // Cannot cancel if it has been processed.
  200. if (!isset($this->handles[$id])) {
  201. return false;
  202. }
  203. $handle = $this->handles[$id]['easy']->handle;
  204. unset($this->delays[$id], $this->handles[$id]);
  205. \curl_multi_remove_handle($this->_mh, $handle);
  206. \curl_close($handle);
  207. return true;
  208. }
  209. private function processMessages(): void
  210. {
  211. while ($done = \curl_multi_info_read($this->_mh)) {
  212. if ($done['msg'] !== \CURLMSG_DONE) {
  213. // if it's not done, then it would be premature to remove the handle. ref https://github.com/guzzle/guzzle/pull/2892#issuecomment-945150216
  214. continue;
  215. }
  216. $id = (int) $done['handle'];
  217. \curl_multi_remove_handle($this->_mh, $done['handle']);
  218. if (!isset($this->handles[$id])) {
  219. // Probably was cancelled.
  220. continue;
  221. }
  222. $entry = $this->handles[$id];
  223. unset($this->handles[$id], $this->delays[$id]);
  224. $entry['easy']->errno = $done['result'];
  225. $entry['deferred']->resolve(
  226. CurlFactory::finish($this, $entry['easy'], $this->factory)
  227. );
  228. }
  229. }
  230. private function timeToNext(): int
  231. {
  232. $currentTime = Utils::currentTime();
  233. $nextTime = \PHP_INT_MAX;
  234. foreach ($this->delays as $time) {
  235. if ($time < $nextTime) {
  236. $nextTime = $time;
  237. }
  238. }
  239. return ((int) \max(0, $nextTime - $currentTime)) * 1000000;
  240. }
  241. }