1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace ClickHouseDB\Transport; |
4
|
|
|
|
5
|
|
|
use ClickHouseDB\Exception\TransportException; |
6
|
|
|
|
7
|
|
|
class CurlerRolling |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var int |
11
|
|
|
* |
12
|
|
|
* Max number of simultaneous requests. |
13
|
|
|
*/ |
14
|
|
|
private $simultaneousLimit = 10; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
|
|
|
|
18
|
|
|
* |
19
|
|
|
* Requests currently being processed by curl |
20
|
|
|
*/ |
21
|
|
|
private $activeRequests = []; |
22
|
|
|
|
23
|
|
|
/** |
|
|
|
|
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
private $runningRequests = 0; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var CurlerRequest[] |
30
|
|
|
* |
31
|
|
|
* Requests queued to be processed |
32
|
|
|
*/ |
33
|
|
|
private $pendingRequests = []; |
34
|
|
|
|
35
|
|
|
/** |
|
|
|
|
36
|
|
|
* @return int |
37
|
|
|
*/ |
38
|
|
|
private $completedRequestCount = 0; |
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
|
|
|
|
41
|
|
|
* @var null|resource |
42
|
|
|
*/ |
43
|
|
|
private $_pool_master = null; |
44
|
|
|
|
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
private $waitRequests = 0; |
49
|
|
|
|
50
|
|
|
/** |
|
|
|
|
51
|
|
|
* @var array |
|
|
|
|
52
|
|
|
*/ |
53
|
|
|
private $handleMapTasks = []; |
54
|
|
|
|
55
|
|
|
/** |
|
|
|
|
56
|
|
|
* |
57
|
|
|
*/ |
58
|
|
|
public function __destructor() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$this->close(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return resource |
66
|
|
|
*/ |
67
|
10 |
|
private function handlerMulti() |
68
|
|
|
{ |
69
|
10 |
|
if (!$this->_pool_master) { |
|
|
|
|
70
|
10 |
|
$this->_pool_master = curl_multi_init(); |
|
|
|
|
71
|
|
|
|
72
|
10 |
|
if (function_exists('curl_multi_setopt')) { |
|
|
|
|
73
|
10 |
|
curl_multi_setopt($this->_pool_master, CURLMOPT_MAXCONNECTS, $this->simultaneousLimit); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
10 |
|
return $this->_pool_master; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
|
|
|
|
81
|
|
|
* |
82
|
|
|
*/ |
83
|
|
|
public function close() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
if ($this->_pool_master) { |
|
|
|
|
86
|
|
|
curl_multi_close($this->handlerMulti()); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param CurlerRequest $req |
|
|
|
|
93
|
|
|
* @param bool $checkMultiAdd |
|
|
|
|
94
|
|
|
* @param bool $force |
|
|
|
|
95
|
|
|
* @return bool |
96
|
|
|
* @throws TransportException |
97
|
|
|
*/ |
98
|
13 |
|
public function addQueLoop(CurlerRequest $req, $checkMultiAdd = true, $force = false) |
|
|
|
|
99
|
|
|
{ |
100
|
13 |
|
$id = $req->getId(); |
101
|
|
|
|
102
|
13 |
|
if (!$id) { |
|
|
|
|
103
|
13 |
|
$id = $req->getUniqHash($this->completedRequestCount); |
104
|
|
|
} |
105
|
|
|
|
106
|
13 |
|
if (!$force && isset($this->pendingRequests[$id])) { |
|
|
|
|
107
|
|
|
if (!$checkMultiAdd) { |
|
|
|
|
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
throw new TransportException("Cant add exists que - cant overwrite : $id!\n"); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
13 |
|
$this->pendingRequests[$id] = $req; |
115
|
13 |
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param resource $oneHandle |
120
|
|
|
* @return CurlerResponse |
121
|
|
|
*/ |
122
|
47 |
|
private function makeResponse($oneHandle) |
|
|
|
|
123
|
|
|
{ |
124
|
47 |
|
$response = curl_multi_getcontent($oneHandle); |
|
|
|
|
125
|
47 |
|
$header_size = curl_getinfo($oneHandle, CURLINFO_HEADER_SIZE); |
|
|
|
|
126
|
47 |
|
$header = substr($response, 0, $header_size); |
|
|
|
|
127
|
47 |
|
$body = substr($response, $header_size); |
|
|
|
|
128
|
|
|
|
129
|
47 |
|
$n = new CurlerResponse(); |
|
|
|
|
130
|
47 |
|
$n->_headers = $this->parse_headers_from_curl_response($header); |
131
|
47 |
|
$n->_body = $body; |
|
|
|
|
132
|
47 |
|
$n->_info = curl_getinfo($oneHandle); |
|
|
|
|
133
|
47 |
|
$n->_error = curl_error($oneHandle); |
|
|
|
|
134
|
47 |
|
$n->_errorNo = curl_errno($oneHandle); |
|
|
|
|
135
|
47 |
|
$n->_useTime = 0; |
136
|
|
|
|
137
|
47 |
|
return $n; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return bool |
142
|
|
|
* @throws TransportException |
143
|
|
|
*/ |
144
|
10 |
|
public function execLoopWait() |
|
|
|
|
145
|
|
|
{ |
146
|
10 |
|
$c = 0; |
|
|
|
|
147
|
10 |
|
$count=0; |
|
|
|
|
148
|
|
|
// add all tasks |
149
|
|
|
do { |
150
|
10 |
|
$this->exec(); |
151
|
|
|
|
152
|
10 |
|
$loop = $this->countActive(); |
153
|
10 |
|
$pend = $this->countPending(); |
154
|
|
|
|
155
|
10 |
|
$count=$loop+$pend; |
|
|
|
|
156
|
10 |
|
$c++; |
157
|
|
|
|
158
|
10 |
|
if ($c > 20000) { |
159
|
|
|
break; |
160
|
|
|
} |
161
|
10 |
|
usleep(500); |
|
|
|
|
162
|
10 |
|
} while ($count); |
163
|
|
|
|
164
|
10 |
|
return true; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $response |
169
|
|
|
* @return array |
|
|
|
|
170
|
|
|
*/ |
171
|
47 |
|
private function parse_headers_from_curl_response($response) |
|
|
|
|
172
|
|
|
{ |
173
|
47 |
|
$headers = []; |
|
|
|
|
174
|
47 |
|
$header_text = $response; |
175
|
|
|
|
176
|
47 |
|
foreach (explode("\r\n", $header_text) as $i => $line) { |
|
|
|
|
177
|
47 |
|
if ($i === 0) { |
178
|
47 |
|
$headers['http_code'] = $line; |
179
|
|
|
} else { |
180
|
46 |
|
$r = explode(': ', $line); |
|
|
|
|
181
|
46 |
|
if (sizeof($r) == 2) { |
|
|
|
|
182
|
47 |
|
$headers[$r[0]] = $r[1]; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
47 |
|
return $headers; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return int |
192
|
|
|
*/ |
193
|
14 |
|
public function countPending() |
|
|
|
|
194
|
|
|
{ |
195
|
14 |
|
return sizeof($this->pendingRequests); |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return int |
200
|
|
|
*/ |
201
|
10 |
|
public function countActive() |
|
|
|
|
202
|
|
|
{ |
203
|
10 |
|
return count($this->activeRequests); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return int |
208
|
|
|
*/ |
209
|
|
|
public function countCompleted() |
|
|
|
|
210
|
|
|
{ |
211
|
|
|
return $this->completedRequestCount; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Set the limit for how many cURL requests will be execute simultaneously. |
216
|
|
|
* |
217
|
|
|
* Please be mindful that if you set this too high, requests are likely to fail |
218
|
|
|
* more frequently or automated software may perceive you as a DOS attack and |
219
|
|
|
* automatically block further requests. |
220
|
|
|
* |
221
|
|
|
* @param int $count |
222
|
|
|
* @throws \InvalidArgumentException |
223
|
|
|
* @return $this |
224
|
|
|
*/ |
225
|
|
|
public function setSimultaneousLimit($count) |
|
|
|
|
226
|
|
|
{ |
227
|
|
|
if (!is_int($count) || $count < 2) { |
|
|
|
|
228
|
|
|
throw new \InvalidArgumentException("setSimultaneousLimit count must be an int >= 2"); |
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$this->simultaneousLimit = $count; |
232
|
|
|
return $this; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @return int |
237
|
|
|
*/ |
238
|
10 |
|
public function getSimultaneousLimit() |
|
|
|
|
239
|
|
|
{ |
240
|
10 |
|
return $this->simultaneousLimit; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @return int |
245
|
|
|
*/ |
246
|
|
|
public function getRunningRequests() |
|
|
|
|
247
|
|
|
{ |
248
|
|
|
return $this->runningRequests; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param CurlerRequest $request |
|
|
|
|
253
|
|
|
* @param bool $auto_close |
|
|
|
|
254
|
|
|
* @return mixed |
255
|
|
|
* @throws TransportException |
256
|
|
|
*/ |
257
|
47 |
|
public function execOne(CurlerRequest $request, $auto_close = false) |
|
|
|
|
258
|
|
|
{ |
259
|
47 |
|
$h = $request->handle(); |
260
|
47 |
|
curl_exec($h); |
|
|
|
|
261
|
|
|
|
262
|
47 |
|
$request->setResponse($this->makeResponse($h)); |
263
|
|
|
|
264
|
47 |
|
if ($auto_close) { |
265
|
4 |
|
$request->close(); |
266
|
|
|
} |
267
|
|
|
|
268
|
47 |
|
return $request->response()->http_code(); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @return string |
273
|
|
|
*/ |
274
|
|
|
public function getInfo() |
|
|
|
|
275
|
|
|
{ |
276
|
|
|
return "runningRequests = {$this->runningRequests} , pending=" . sizeof($this->pendingRequests) . " "; |
|
|
|
|
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @throws TransportException |
281
|
|
|
*/ |
282
|
10 |
|
public function exec() |
|
|
|
|
283
|
|
|
{ |
284
|
10 |
|
$this->makePendingRequestsQue(); |
285
|
|
|
|
286
|
|
|
// ensure we're running |
287
|
|
|
// a request was just completed -- find out which one |
288
|
|
|
|
289
|
10 |
|
while (($execrun = curl_multi_exec($this->handlerMulti(), $running)) == CURLM_CALL_MULTI_PERFORM); |
|
|
|
|
290
|
|
|
|
291
|
10 |
|
if ($execrun != CURLM_OK) { |
|
|
|
|
292
|
|
|
throw new TransportException("[ NOT CURLM_OK ]"); |
|
|
|
|
293
|
|
|
} |
294
|
|
|
|
295
|
10 |
|
$this->runningRequests = $running; |
296
|
|
|
|
297
|
10 |
|
while ($done = curl_multi_info_read($this->handlerMulti())) { |
|
|
|
|
298
|
10 |
|
$response = $this->makeResponse($done['handle']); |
299
|
|
|
|
300
|
|
|
// send the return values to the callback function. |
301
|
|
|
|
302
|
10 |
|
$key = (string) $done['handle']; |
|
|
|
|
303
|
10 |
|
$task_id = $this->handleMapTasks[$key]; |
304
|
10 |
|
$request = $this->pendingRequests[$this->handleMapTasks[$key]]; |
305
|
|
|
|
306
|
10 |
|
unset($this->handleMapTasks[$key]); |
307
|
10 |
|
unset($this->activeRequests[$task_id]); |
308
|
|
|
|
309
|
10 |
|
$this->pendingRequests[$task_id]->setResponse($response); |
310
|
10 |
|
$this->pendingRequests[$task_id]->onCallback(); |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
313
|
10 |
|
if (!$request->isPersistent()) { |
|
|
|
|
314
|
10 |
|
unset($this->pendingRequests[$task_id]); |
315
|
|
|
} |
316
|
|
|
|
317
|
10 |
|
$this->completedRequestCount++; |
318
|
|
|
|
319
|
|
|
// remove the curl handle that just completed |
320
|
10 |
|
curl_multi_remove_handle($this->handlerMulti(), $done['handle']); |
|
|
|
|
321
|
|
|
|
322
|
|
|
// if something was requeued, this will get it running/update our loop check values |
323
|
10 |
|
$status = curl_multi_exec($this->handlerMulti(), $active); |
|
|
|
|
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
// see if there is anything to read |
327
|
10 |
|
curl_multi_select($this->handlerMulti(), 0.01); |
|
|
|
|
328
|
10 |
|
return $this->countActive(); |
329
|
|
|
} |
330
|
|
|
|
331
|
10 |
|
public function makePendingRequestsQue() |
|
|
|
|
332
|
|
|
{ |
|
|
|
|
333
|
|
|
|
334
|
10 |
|
$max = $this->getSimultaneousLimit(); |
|
|
|
|
335
|
10 |
|
$active = $this->countActive(); |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
338
|
10 |
|
if ($active < $max) { |
|
|
|
|
339
|
|
|
|
340
|
10 |
|
$canAdd = $max - $active; |
341
|
|
|
// $pending = sizeof($this->pendingRequests); |
342
|
|
|
|
343
|
10 |
|
$add = []; |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
346
|
10 |
|
foreach ($this->pendingRequests as $task_id => $params) { |
347
|
10 |
|
if (empty($this->activeRequests[$task_id])) { |
|
|
|
|
348
|
10 |
|
$add[$task_id] = $task_id; |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
353
|
10 |
|
if (sizeof($add)) { |
|
|
|
|
354
|
10 |
|
if ($canAdd >= sizeof($add)) { |
|
|
|
|
355
|
10 |
|
$ll = $add; |
356
|
|
|
} else { |
357
|
1 |
|
$ll = array_rand($add, $canAdd); |
|
|
|
|
358
|
1 |
|
if (!is_array($ll)) { |
|
|
|
|
359
|
1 |
|
$ll = array($ll => $ll); |
|
|
|
|
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
363
|
10 |
|
foreach ($ll as $task_id) { |
364
|
10 |
|
$this->_prepareLoopQue($task_id); |
365
|
|
|
} |
366
|
|
|
|
|
|
|
|
367
|
|
|
}// if add |
368
|
|
|
}// if can add |
369
|
10 |
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* @param string $task_id |
373
|
|
|
*/ |
374
|
10 |
|
private function _prepareLoopQue($task_id) |
|
|
|
|
375
|
|
|
{ |
376
|
10 |
|
$this->activeRequests[$task_id] = 1; |
377
|
10 |
|
$this->waitRequests++; |
378
|
|
|
|
379
|
10 |
|
$h = $this->pendingRequests[$task_id]->handle(); |
380
|
|
|
|
381
|
|
|
// pool |
382
|
10 |
|
curl_multi_add_handle($this->handlerMulti(), $h); |
|
|
|
|
383
|
|
|
|
384
|
10 |
|
$key = (string) $h; |
|
|
|
|
385
|
10 |
|
$this->handleMapTasks[$key] = $task_id; |
386
|
10 |
|
} |
387
|
|
|
} |
388
|
|
|
|