Pool.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace GuzzleHttp;
  3. use GuzzleHttp\Promise\PromisorInterface;
  4. use Psr\Http\Message\RequestInterface;
  5. use GuzzleHttp\Promise\EachPromise;
  6. /**
  7. * Sends and iterator of requests concurrently using a capped pool size.
  8. *
  9. * The pool will read from an iterator until it is cancelled or until the
  10. * iterator is consumed. When a request is yielded, the request is sent after
  11. * applying the "request_options" request options (if provided in the ctor).
  12. *
  13. * When a function is yielded by the iterator, the function is provided the
  14. * "request_options" array that should be merged on top of any existing
  15. * options, and the function MUST then return a wait-able promise.
  16. */
  17. class Pool implements PromisorInterface
  18. {
  19. /** @var EachPromise */
  20. private $each;
  21. /**
  22. * @param ClientInterface $client Client used to send the requests.
  23. * @param array|\Iterator $requests Requests or functions that return
  24. * requests to send concurrently.
  25. * @param array $config Associative array of options
  26. * - concurrency: (int) Maximum number of requests to send concurrently
  27. * - options: Array of request options to apply to each request.
  28. * - fulfilled: (callable) Function to invoke when a request completes.
  29. * - rejected: (callable) Function to invoke when a request is rejected.
  30. */
  31. public function __construct(
  32. ClientInterface $client,
  33. $requests,
  34. array $config = []
  35. ) {
  36. // Backwards compatibility.
  37. if (isset($config['pool_size'])) {
  38. $config['concurrency'] = $config['pool_size'];
  39. } elseif (!isset($config['concurrency'])) {
  40. $config['concurrency'] = 25;
  41. }
  42. if (isset($config['options'])) {
  43. $opts = $config['options'];
  44. unset($config['options']);
  45. } else {
  46. $opts = [];
  47. }
  48. $iterable = \GuzzleHttp\Promise\iter_for($requests);
  49. $requests = function () use ($iterable, $client, $opts) {
  50. foreach ($iterable as $key => $rfn) {
  51. if ($rfn instanceof RequestInterface) {
  52. yield $key => $client->sendAsync($rfn, $opts);
  53. } elseif (is_callable($rfn)) {
  54. yield $key => $rfn($opts);
  55. } else {
  56. throw new \InvalidArgumentException('Each value yielded by '
  57. . 'the iterator must be a Psr7\Http\Message\RequestInterface '
  58. . 'or a callable that returns a promise that fulfills '
  59. . 'with a Psr7\Message\Http\ResponseInterface object.');
  60. }
  61. }
  62. };
  63. $this->each = new EachPromise($requests(), $config);
  64. }
  65. public function promise()
  66. {
  67. return $this->each->promise();
  68. }
  69. /**
  70. * Sends multiple requests concurrently and returns an array of responses
  71. * and exceptions that uses the same ordering as the provided requests.
  72. *
  73. * IMPORTANT: This method keeps every request and response in memory, and
  74. * as such, is NOT recommended when sending a large number or an
  75. * indeterminate number of requests concurrently.
  76. *
  77. * @param ClientInterface $client Client used to send the requests
  78. * @param array|\Iterator $requests Requests to send concurrently.
  79. * @param array $options Passes through the options available in
  80. * {@see GuzzleHttp\Pool::__construct}
  81. *
  82. * @return array Returns an array containing the response or an exception
  83. * in the same order that the requests were sent.
  84. * @throws \InvalidArgumentException if the event format is incorrect.
  85. */
  86. public static function batch(
  87. ClientInterface $client,
  88. $requests,
  89. array $options = []
  90. ) {
  91. $res = [];
  92. self::cmpCallback($options, 'fulfilled', $res);
  93. self::cmpCallback($options, 'rejected', $res);
  94. $pool = new static($client, $requests, $options);
  95. $pool->promise()->wait();
  96. ksort($res);
  97. return $res;
  98. }
  99. private static function cmpCallback(array &$options, $name, array &$results)
  100. {
  101. if (!isset($options[$name])) {
  102. $options[$name] = function ($v, $k) use (&$results) {
  103. $results[$k] = $v;
  104. };
  105. } else {
  106. $currentFn = $options[$name];
  107. $options[$name] = function ($v, $k) use (&$results, $currentFn) {
  108. $currentFn($v, $k);
  109. $results[$k] = $v;
  110. };
  111. }
  112. }
  113. }