CommandPool.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Aws;
  3. use GuzzleHttp\Promise\PromisorInterface;
  4. use GuzzleHttp\Promise\EachPromise;
  5. /**
  6. * Sends and iterator of commands concurrently using a capped pool size.
  7. *
  8. * The pool will read command objects from an iterator until it is cancelled or
  9. * until the iterator is consumed.
  10. */
  11. class CommandPool implements PromisorInterface
  12. {
  13. /** @var EachPromise */
  14. private $each;
  15. /**
  16. * The CommandPool constructor accepts a hash of configuration options:
  17. *
  18. * - concurrency: (callable|int) Maximum number of commands to execute
  19. * concurrently. Provide a function to resize the pool dynamically. The
  20. * function will be provided the current number of pending requests and
  21. * is expected to return an integer representing the new pool size limit.
  22. * - before: (callable) function to invoke before sending each command. The
  23. * before function accepts the command and the key of the iterator of the
  24. * command. You can mutate the command as needed in the before function
  25. * before sending the command.
  26. * - fulfilled: (callable) Function to invoke when a promise is fulfilled.
  27. * The function is provided the result object, id of the iterator that the
  28. * result came from, and the aggregate promise that can be resolved/rejected
  29. * if you need to short-circuit the pool.
  30. * - rejected: (callable) Function to invoke when a promise is rejected.
  31. * The function is provided an AwsException object, id of the iterator that
  32. * the exception came from, and the aggregate promise that can be
  33. * resolved/rejected if you need to short-circuit the pool.
  34. * - preserve_iterator_keys: (bool) Retain the iterator key when generating
  35. * the commands.
  36. *
  37. * @param AwsClientInterface $client Client used to execute commands.
  38. * @param array|\Iterator $commands Iterable that yields commands.
  39. * @param array $config Associative array of options.
  40. */
  41. public function __construct(
  42. AwsClientInterface $client,
  43. $commands,
  44. array $config = []
  45. ) {
  46. if (!isset($config['concurrency'])) {
  47. $config['concurrency'] = 25;
  48. }
  49. $before = $this->getBefore($config);
  50. $mapFn = function ($commands) use ($client, $before, $config) {
  51. foreach ($commands as $key => $command) {
  52. if (!($command instanceof CommandInterface)) {
  53. throw new \InvalidArgumentException('Each value yielded by '
  54. . 'the iterator must be an Aws\CommandInterface.');
  55. }
  56. if ($before) {
  57. $before($command, $key);
  58. }
  59. if (!empty($config['preserve_iterator_keys'])) {
  60. yield $key => $client->executeAsync($command);
  61. } else {
  62. yield $client->executeAsync($command);
  63. }
  64. }
  65. };
  66. $this->each = new EachPromise($mapFn($commands), $config);
  67. }
  68. /**
  69. * @return \GuzzleHttp\Promise\PromiseInterface
  70. */
  71. public function promise()
  72. {
  73. return $this->each->promise();
  74. }
  75. /**
  76. * Executes a pool synchronously and aggregates the results of the pool
  77. * into an indexed array in the same order as the passed in array.
  78. *
  79. * @param AwsClientInterface $client Client used to execute commands.
  80. * @param mixed $commands Iterable that yields commands.
  81. * @param array $config Configuration options.
  82. *
  83. * @return array
  84. * @see \Aws\CommandPool::__construct for available configuration options.
  85. */
  86. public static function batch(
  87. AwsClientInterface $client,
  88. $commands,
  89. array $config = []
  90. ) {
  91. $results = [];
  92. self::cmpCallback($config, 'fulfilled', $results);
  93. self::cmpCallback($config, 'rejected', $results);
  94. return (new self($client, $commands, $config))
  95. ->promise()
  96. ->then(static function () use (&$results) {
  97. ksort($results);
  98. return $results;
  99. })
  100. ->wait();
  101. }
  102. /**
  103. * @return callable
  104. */
  105. private function getBefore(array $config)
  106. {
  107. if (!isset($config['before'])) {
  108. return null;
  109. }
  110. if (is_callable($config['before'])) {
  111. return $config['before'];
  112. }
  113. throw new \InvalidArgumentException('before must be callable');
  114. }
  115. /**
  116. * Adds an onFulfilled or onRejected callback that aggregates results into
  117. * an array. If a callback is already present, it is replaced with the
  118. * composed function.
  119. *
  120. * @param array $config
  121. * @param $name
  122. * @param array $results
  123. */
  124. private static function cmpCallback(array &$config, $name, array &$results)
  125. {
  126. if (!isset($config[$name])) {
  127. $config[$name] = function ($v, $k) use (&$results) {
  128. $results[$k] = $v;
  129. };
  130. } else {
  131. $currentFn = $config[$name];
  132. $config[$name] = function ($v, $k) use (&$results, $currentFn) {
  133. $currentFn($v, $k);
  134. $results[$k] = $v;
  135. };
  136. }
  137. }
  138. }