Chris@0
|
1 <?php
|
Chris@0
|
2 namespace GuzzleHttp\Handler;
|
Chris@0
|
3
|
Chris@0
|
4 use GuzzleHttp\Promise as P;
|
Chris@0
|
5 use GuzzleHttp\Promise\Promise;
|
Chris@0
|
6 use GuzzleHttp\Psr7;
|
Chris@0
|
7 use Psr\Http\Message\RequestInterface;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Returns an asynchronous response using curl_multi_* functions.
|
Chris@0
|
11 *
|
Chris@0
|
12 * When using the CurlMultiHandler, custom curl options can be specified as an
|
Chris@0
|
13 * associative array of curl option constants mapping to values in the
|
Chris@0
|
14 * **curl** key of the provided request options.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @property resource $_mh Internal use only. Lazy loaded multi-handle.
|
Chris@0
|
17 */
|
Chris@0
|
18 class CurlMultiHandler
|
Chris@0
|
19 {
|
Chris@0
|
20 /** @var CurlFactoryInterface */
|
Chris@0
|
21 private $factory;
|
Chris@0
|
22 private $selectTimeout;
|
Chris@0
|
23 private $active;
|
Chris@0
|
24 private $handles = [];
|
Chris@0
|
25 private $delays = [];
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * This handler accepts the following options:
|
Chris@0
|
29 *
|
Chris@0
|
30 * - handle_factory: An optional factory used to create curl handles
|
Chris@0
|
31 * - select_timeout: Optional timeout (in seconds) to block before timing
|
Chris@0
|
32 * out while selecting curl handles. Defaults to 1 second.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @param array $options
|
Chris@0
|
35 */
|
Chris@0
|
36 public function __construct(array $options = [])
|
Chris@0
|
37 {
|
Chris@0
|
38 $this->factory = isset($options['handle_factory'])
|
Chris@0
|
39 ? $options['handle_factory'] : new CurlFactory(50);
|
Chris@0
|
40 $this->selectTimeout = isset($options['select_timeout'])
|
Chris@0
|
41 ? $options['select_timeout'] : 1;
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 public function __get($name)
|
Chris@0
|
45 {
|
Chris@0
|
46 if ($name === '_mh') {
|
Chris@0
|
47 return $this->_mh = curl_multi_init();
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 throw new \BadMethodCallException();
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 public function __destruct()
|
Chris@0
|
54 {
|
Chris@0
|
55 if (isset($this->_mh)) {
|
Chris@0
|
56 curl_multi_close($this->_mh);
|
Chris@0
|
57 unset($this->_mh);
|
Chris@0
|
58 }
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 public function __invoke(RequestInterface $request, array $options)
|
Chris@0
|
62 {
|
Chris@0
|
63 $easy = $this->factory->create($request, $options);
|
Chris@0
|
64 $id = (int) $easy->handle;
|
Chris@0
|
65
|
Chris@0
|
66 $promise = new Promise(
|
Chris@0
|
67 [$this, 'execute'],
|
Chris@0
|
68 function () use ($id) { return $this->cancel($id); }
|
Chris@0
|
69 );
|
Chris@0
|
70
|
Chris@0
|
71 $this->addRequest(['easy' => $easy, 'deferred' => $promise]);
|
Chris@0
|
72
|
Chris@0
|
73 return $promise;
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * Ticks the curl event loop.
|
Chris@0
|
78 */
|
Chris@0
|
79 public function tick()
|
Chris@0
|
80 {
|
Chris@0
|
81 // Add any delayed handles if needed.
|
Chris@0
|
82 if ($this->delays) {
|
Chris@0
|
83 $currentTime = microtime(true);
|
Chris@0
|
84 foreach ($this->delays as $id => $delay) {
|
Chris@0
|
85 if ($currentTime >= $delay) {
|
Chris@0
|
86 unset($this->delays[$id]);
|
Chris@0
|
87 curl_multi_add_handle(
|
Chris@0
|
88 $this->_mh,
|
Chris@0
|
89 $this->handles[$id]['easy']->handle
|
Chris@0
|
90 );
|
Chris@0
|
91 }
|
Chris@0
|
92 }
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 // Step through the task queue which may add additional requests.
|
Chris@0
|
96 P\queue()->run();
|
Chris@0
|
97
|
Chris@0
|
98 if ($this->active &&
|
Chris@0
|
99 curl_multi_select($this->_mh, $this->selectTimeout) === -1
|
Chris@0
|
100 ) {
|
Chris@0
|
101 // Perform a usleep if a select returns -1.
|
Chris@0
|
102 // See: https://bugs.php.net/bug.php?id=61141
|
Chris@0
|
103 usleep(250);
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 while (curl_multi_exec($this->_mh, $this->active) === CURLM_CALL_MULTI_PERFORM);
|
Chris@0
|
107
|
Chris@0
|
108 $this->processMessages();
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 /**
|
Chris@0
|
112 * Runs until all outstanding connections have completed.
|
Chris@0
|
113 */
|
Chris@0
|
114 public function execute()
|
Chris@0
|
115 {
|
Chris@0
|
116 $queue = P\queue();
|
Chris@0
|
117
|
Chris@0
|
118 while ($this->handles || !$queue->isEmpty()) {
|
Chris@0
|
119 // If there are no transfers, then sleep for the next delay
|
Chris@0
|
120 if (!$this->active && $this->delays) {
|
Chris@0
|
121 usleep($this->timeToNext());
|
Chris@0
|
122 }
|
Chris@0
|
123 $this->tick();
|
Chris@0
|
124 }
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 private function addRequest(array $entry)
|
Chris@0
|
128 {
|
Chris@0
|
129 $easy = $entry['easy'];
|
Chris@0
|
130 $id = (int) $easy->handle;
|
Chris@0
|
131 $this->handles[$id] = $entry;
|
Chris@0
|
132 if (empty($easy->options['delay'])) {
|
Chris@0
|
133 curl_multi_add_handle($this->_mh, $easy->handle);
|
Chris@0
|
134 } else {
|
Chris@0
|
135 $this->delays[$id] = microtime(true) + ($easy->options['delay'] / 1000);
|
Chris@0
|
136 }
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 /**
|
Chris@0
|
140 * Cancels a handle from sending and removes references to it.
|
Chris@0
|
141 *
|
Chris@0
|
142 * @param int $id Handle ID to cancel and remove.
|
Chris@0
|
143 *
|
Chris@0
|
144 * @return bool True on success, false on failure.
|
Chris@0
|
145 */
|
Chris@0
|
146 private function cancel($id)
|
Chris@0
|
147 {
|
Chris@0
|
148 // Cannot cancel if it has been processed.
|
Chris@0
|
149 if (!isset($this->handles[$id])) {
|
Chris@0
|
150 return false;
|
Chris@0
|
151 }
|
Chris@0
|
152
|
Chris@0
|
153 $handle = $this->handles[$id]['easy']->handle;
|
Chris@0
|
154 unset($this->delays[$id], $this->handles[$id]);
|
Chris@0
|
155 curl_multi_remove_handle($this->_mh, $handle);
|
Chris@0
|
156 curl_close($handle);
|
Chris@0
|
157
|
Chris@0
|
158 return true;
|
Chris@0
|
159 }
|
Chris@0
|
160
|
Chris@0
|
161 private function processMessages()
|
Chris@0
|
162 {
|
Chris@0
|
163 while ($done = curl_multi_info_read($this->_mh)) {
|
Chris@0
|
164 $id = (int) $done['handle'];
|
Chris@0
|
165 curl_multi_remove_handle($this->_mh, $done['handle']);
|
Chris@0
|
166
|
Chris@0
|
167 if (!isset($this->handles[$id])) {
|
Chris@0
|
168 // Probably was cancelled.
|
Chris@0
|
169 continue;
|
Chris@0
|
170 }
|
Chris@0
|
171
|
Chris@0
|
172 $entry = $this->handles[$id];
|
Chris@0
|
173 unset($this->handles[$id], $this->delays[$id]);
|
Chris@0
|
174 $entry['easy']->errno = $done['result'];
|
Chris@0
|
175 $entry['deferred']->resolve(
|
Chris@0
|
176 CurlFactory::finish(
|
Chris@0
|
177 $this,
|
Chris@0
|
178 $entry['easy'],
|
Chris@0
|
179 $this->factory
|
Chris@0
|
180 )
|
Chris@0
|
181 );
|
Chris@0
|
182 }
|
Chris@0
|
183 }
|
Chris@0
|
184
|
Chris@0
|
185 private function timeToNext()
|
Chris@0
|
186 {
|
Chris@0
|
187 $currentTime = microtime(true);
|
Chris@0
|
188 $nextTime = PHP_INT_MAX;
|
Chris@0
|
189 foreach ($this->delays as $time) {
|
Chris@0
|
190 if ($time < $nextTime) {
|
Chris@0
|
191 $nextTime = $time;
|
Chris@0
|
192 }
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 return max(0, $nextTime - $currentTime) * 1000000;
|
Chris@0
|
196 }
|
Chris@0
|
197 }
|