Open Journal Systems  3.3.0
CurlMultiProxy.php
1 <?php
2 
3 namespace Guzzle\Http\Curl;
4 
7 
13 {
14  protected $handles = array();
15  protected $groups = array();
16  protected $queued = array();
17  protected $maxHandles;
18  protected $selectTimeout;
19 
24  public function __construct($maxHandles = 3, $selectTimeout = 1.0)
25  {
26  $this->maxHandles = $maxHandles;
27  $this->selectTimeout = $selectTimeout;
28  // You can get some weird "Too many open files" errors when sending a large amount of requests in parallel.
29  // These two statements autoload classes before a system runs out of file descriptors so that you can get back
30  // valuable error messages if you run out.
31  class_exists('Guzzle\Http\Message\Response');
32  class_exists('Guzzle\Http\Exception\CurlException');
33  }
34 
35  public function add(RequestInterface $request)
36  {
37  $this->queued[] = $request;
38 
39  return $this;
40  }
41 
42  public function all()
43  {
44  $requests = $this->queued;
45  foreach ($this->handles as $handle) {
46  $requests = array_merge($requests, $handle->all());
47  }
48 
49  return $requests;
50  }
51 
52  public function remove(RequestInterface $request)
53  {
54  foreach ($this->queued as $i => $r) {
55  if ($request === $r) {
56  unset($this->queued[$i]);
57  return true;
58  }
59  }
60 
61  foreach ($this->handles as $handle) {
62  if ($handle->remove($request)) {
63  return true;
64  }
65  }
66 
67  return false;
68  }
69 
70  public function reset($hard = false)
71  {
72  $this->queued = array();
73  $this->groups = array();
74  foreach ($this->handles as $handle) {
75  $handle->reset();
76  }
77  if ($hard) {
78  $this->handles = array();
79  }
80 
81  return $this;
82  }
83 
84  public function send()
85  {
86  if ($this->queued) {
87  $group = $this->getAvailableHandle();
88  // Add this handle to a list of handles than is claimed
89  $this->groups[] = $group;
90  while ($request = array_shift($this->queued)) {
91  $group->add($request);
92  }
93  try {
94  $group->send();
95  array_pop($this->groups);
96  $this->cleanupHandles();
97  } catch (\Exception $e) {
98  // Remove the group and cleanup if an exception was encountered and no more requests in group
99  if (!$group->count()) {
100  array_pop($this->groups);
101  $this->cleanupHandles();
102  }
103  throw $e;
104  }
105  }
106  }
107 
108  public function count()
109  {
110  return count($this->all());
111  }
112 
118  protected function getAvailableHandle()
119  {
120  // Grab a handle that is not claimed
121  foreach ($this->handles as $h) {
122  if (!in_array($h, $this->groups, true)) {
123  return $h;
124  }
125  }
126 
127  // All are claimed, so create one
128  $handle = new CurlMulti($this->selectTimeout);
129  $handle->setEventDispatcher($this->getEventDispatcher());
130  $this->handles[] = $handle;
131 
132  return $handle;
133  }
134 
138  protected function cleanupHandles()
139  {
140  if ($diff = max(0, count($this->handles) - $this->maxHandles)) {
141  for ($i = count($this->handles) - 1; $i > 0 && $diff > 0; $i--) {
142  if (!count($this->handles[$i])) {
143  unset($this->handles[$i]);
144  $diff--;
145  }
146  }
147  $this->handles = array_values($this->handles);
148  }
149  }
150 }
Guzzle\Http\Curl\CurlMultiProxy\$handles
$handles
Definition: CurlMultiProxy.php:14
Guzzle\Http\Curl\CurlMultiProxy\all
all()
Definition: CurlMultiProxy.php:42
Guzzle\Http\Message\RequestInterface
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestInterface.php:16
Guzzle\Http\Curl\CurlMultiProxy\$maxHandles
$maxHandles
Definition: CurlMultiProxy.php:17
Guzzle\Http\Curl
Definition: CurlHandle.php:3
Guzzle\Http\Curl\CurlMultiProxy\__construct
__construct($maxHandles=3, $selectTimeout=1.0)
Definition: CurlMultiProxy.php:24
Guzzle\Http\Curl\CurlMultiProxy\$selectTimeout
$selectTimeout
Definition: CurlMultiProxy.php:18
Guzzle\Http\Curl\CurlMultiProxy\getAvailableHandle
getAvailableHandle()
Definition: CurlMultiProxy.php:118
Guzzle\Http\Curl\CurlMultiProxy
Definition: CurlMultiProxy.php:12
Guzzle\Http\Curl\CurlMultiProxy\cleanupHandles
cleanupHandles()
Definition: CurlMultiProxy.php:138
Guzzle\Http\Curl\CurlMultiProxy\count
count()
Definition: CurlMultiProxy.php:108
Guzzle\Http\Curl\CurlMultiProxy\reset
reset($hard=false)
Definition: CurlMultiProxy.php:70
Guzzle\Http\Curl\CurlMultiProxy\add
add(RequestInterface $request)
Definition: CurlMultiProxy.php:35
Guzzle\Http\Curl\CurlMultiProxy\send
send()
Definition: CurlMultiProxy.php:84
Guzzle\Http\Curl\CurlMultiProxy\$groups
$groups
Definition: CurlMultiProxy.php:15
Guzzle\Common\AbstractHasDispatcher\getEventDispatcher
getEventDispatcher()
Definition: AbstractHasDispatcher.php:32
Guzzle\Http\Curl\CurlMultiProxy\$queued
$queued
Definition: CurlMultiProxy.php:16
Guzzle\Http\Curl\CurlMultiInterface
Definition: CurlMultiInterface.php:12
Guzzle\Http\Curl\CurlMulti
Definition: CurlMulti.php:16
Guzzle\Common\AbstractHasDispatcher
Definition: AbstractHasDispatcher.php:12