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
|
48 |
|
private function makeResponse($oneHandle) |
|
|
|
|
123
|
|
|
{ |
124
|
48 |
|
$response = curl_multi_getcontent($oneHandle); |
|
|
|
|
125
|
48 |
|
$header_size = curl_getinfo($oneHandle, CURLINFO_HEADER_SIZE); |
|
|
|
|
126
|
48 |
|
$header = substr($response, 0, $header_size); |
|
|
|
|
127
|
48 |
|
$body = substr($response, $header_size); |
|
|
|
|
128
|
|
|
|
129
|
48 |
|
$n = new CurlerResponse(); |
|
|
|
|
130
|
48 |
|
$n->_headers = $this->parse_headers_from_curl_response($header); |
131
|
48 |
|
$n->_body = $body; |
|
|
|
|
132
|
48 |
|
$n->_info = curl_getinfo($oneHandle); |
|
|
|
|
133
|
48 |
|
$n->_error = curl_error($oneHandle); |
|
|
|
|
134
|
48 |
|
$n->_errorNo = curl_errno($oneHandle); |
|
|
|
|
135
|
48 |
|
$n->_useTime = 0; |
136
|
|
|
|
137
|
48 |
|
return $n; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return bool |
|
|
|
|
142
|
|
|
* @throws TransportException |
143
|
|
|
*/ |
144
|
10 |
|
public function execLoopWait() |
|
|
|
|
145
|
|
|
{ |
146
|
10 |
|
$c = 0; |
|
|
|
|
147
|
10 |
|
$timeStart=time(); |
|
|
|
|
148
|
10 |
|
$uSleep=1000; // Timer: check response from server, and add new Task/Que to loop |
|
|
|
|
149
|
10 |
|
$PendingAllConnections=$this->countPending(); |
|
|
|
|
150
|
|
|
|
151
|
|
|
// Max loop check |
152
|
|
|
|
153
|
10 |
|
$count=0; |
|
|
|
|
154
|
|
|
// add all tasks |
155
|
|
|
do { |
156
|
10 |
|
$this->exec(); |
157
|
10 |
|
$timeWork=time()-$timeStart; |
|
|
|
|
158
|
|
|
// |
|
|
|
|
159
|
10 |
|
$ActiveNowConnections = $this->countActive(); |
|
|
|
|
160
|
10 |
|
$PendingNowConnections = $this->countPending(); |
161
|
|
|
|
162
|
10 |
|
$count=$ActiveNowConnections+$PendingNowConnections; |
|
|
|
|
163
|
10 |
|
$c++; |
164
|
|
|
|
165
|
10 |
|
if ($c > 20000) { |
166
|
|
|
break; |
167
|
|
|
} |
168
|
|
|
|
169
|
10 |
|
usleep($uSleep); |
|
|
|
|
170
|
|
|
// usleep(2000000) == 2 seconds |
171
|
10 |
|
} while ($count); |
172
|
|
|
|
173
|
10 |
|
return true; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $response |
|
|
|
|
178
|
|
|
* @return array |
|
|
|
|
179
|
|
|
*/ |
180
|
48 |
|
private function parse_headers_from_curl_response($response) |
|
|
|
|
181
|
|
|
{ |
182
|
48 |
|
$headers = []; |
|
|
|
|
183
|
48 |
|
$header_text = $response; |
184
|
|
|
|
185
|
48 |
|
foreach (explode("\r\n", $header_text) as $i => $line) { |
|
|
|
|
186
|
48 |
|
if ($i === 0) { |
187
|
48 |
|
$headers['http_code'] = $line; |
188
|
|
|
} else { |
189
|
47 |
|
$r = explode(': ', $line); |
|
|
|
|
190
|
47 |
|
if (sizeof($r) == 2) { |
|
|
|
|
191
|
48 |
|
$headers[$r[0]] = $r[1]; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
48 |
|
return $headers; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return int |
201
|
|
|
*/ |
202
|
14 |
|
public function countPending() |
|
|
|
|
203
|
|
|
{ |
204
|
14 |
|
return sizeof($this->pendingRequests); |
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return int |
209
|
|
|
*/ |
210
|
10 |
|
public function countActive() |
|
|
|
|
211
|
|
|
{ |
212
|
10 |
|
return count($this->activeRequests); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return int |
217
|
|
|
*/ |
218
|
|
|
public function countCompleted() |
|
|
|
|
219
|
|
|
{ |
220
|
|
|
return $this->completedRequestCount; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Set the limit for how many cURL requests will be execute simultaneously. |
225
|
|
|
* |
226
|
|
|
* Please be mindful that if you set this too high, requests are likely to fail |
227
|
|
|
* more frequently or automated software may perceive you as a DOS attack and |
228
|
|
|
* automatically block further requests. |
229
|
|
|
* |
230
|
|
|
* @param int $count |
|
|
|
|
231
|
|
|
* @throws \InvalidArgumentException |
|
|
|
|
232
|
|
|
* @return $this |
233
|
|
|
*/ |
234
|
|
|
public function setSimultaneousLimit($count) |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
if (!is_int($count) || $count < 2) { |
|
|
|
|
237
|
|
|
throw new \InvalidArgumentException("setSimultaneousLimit count must be an int >= 2"); |
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
$this->simultaneousLimit = $count; |
241
|
|
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return int |
246
|
|
|
*/ |
247
|
10 |
|
public function getSimultaneousLimit() |
|
|
|
|
248
|
|
|
{ |
249
|
10 |
|
return $this->simultaneousLimit; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return int |
254
|
|
|
*/ |
255
|
|
|
public function getRunningRequests() |
|
|
|
|
256
|
|
|
{ |
257
|
|
|
return $this->runningRequests; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param CurlerRequest $request |
|
|
|
|
262
|
|
|
* @param bool $auto_close |
|
|
|
|
263
|
|
|
* @return mixed |
264
|
|
|
* @throws TransportException |
265
|
|
|
*/ |
266
|
48 |
|
public function execOne(CurlerRequest $request, $auto_close = false) |
|
|
|
|
267
|
|
|
{ |
268
|
48 |
|
$h = $request->handle(); |
269
|
48 |
|
curl_exec($h); |
|
|
|
|
270
|
|
|
|
271
|
48 |
|
$request->setResponse($this->makeResponse($h)); |
272
|
|
|
|
273
|
48 |
|
if ($auto_close) { |
274
|
4 |
|
$request->close(); |
275
|
|
|
} |
276
|
|
|
|
277
|
48 |
|
return $request->response()->http_code(); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @return string |
282
|
|
|
*/ |
283
|
|
|
public function getInfo() |
|
|
|
|
284
|
|
|
{ |
285
|
|
|
return "runningRequests = {$this->runningRequests} , pending=" . sizeof($this->pendingRequests) . " "; |
|
|
|
|
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @throws TransportException |
290
|
|
|
*/ |
291
|
10 |
|
public function exec() |
|
|
|
|
292
|
|
|
{ |
293
|
10 |
|
$this->makePendingRequestsQue(); |
294
|
|
|
|
295
|
|
|
// ensure we're running |
296
|
|
|
// a request was just completed -- find out which one |
297
|
|
|
|
298
|
10 |
|
while (($execrun = curl_multi_exec($this->handlerMulti(), $running)) == CURLM_CALL_MULTI_PERFORM); |
|
|
|
|
299
|
|
|
|
300
|
10 |
|
if ($execrun != CURLM_OK) { |
|
|
|
|
301
|
|
|
throw new TransportException("[ NOT CURLM_OK ]"); |
|
|
|
|
302
|
|
|
} |
303
|
|
|
|
304
|
10 |
|
$this->runningRequests = $running; |
305
|
|
|
|
306
|
10 |
|
while ($done = curl_multi_info_read($this->handlerMulti())) { |
|
|
|
|
307
|
10 |
|
$response = $this->makeResponse($done['handle']); |
308
|
|
|
|
309
|
|
|
// send the return values to the callback function. |
310
|
|
|
|
311
|
10 |
|
$key = (string) $done['handle']; |
|
|
|
|
312
|
10 |
|
$task_id = $this->handleMapTasks[$key]; |
313
|
10 |
|
$request = $this->pendingRequests[$this->handleMapTasks[$key]]; |
314
|
|
|
|
315
|
10 |
|
unset($this->handleMapTasks[$key]); |
316
|
10 |
|
unset($this->activeRequests[$task_id]); |
317
|
|
|
|
318
|
10 |
|
$this->pendingRequests[$task_id]->setResponse($response); |
319
|
10 |
|
$this->pendingRequests[$task_id]->onCallback(); |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
322
|
10 |
|
if (!$request->isPersistent()) { |
|
|
|
|
323
|
10 |
|
unset($this->pendingRequests[$task_id]); |
324
|
|
|
} |
325
|
|
|
|
326
|
10 |
|
$this->completedRequestCount++; |
327
|
|
|
|
328
|
|
|
// remove the curl handle that just completed |
329
|
10 |
|
curl_multi_remove_handle($this->handlerMulti(), $done['handle']); |
|
|
|
|
330
|
|
|
|
331
|
|
|
// if something was requeued, this will get it running/update our loop check values |
332
|
10 |
|
$status = curl_multi_exec($this->handlerMulti(), $active); |
|
|
|
|
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
// see if there is anything to read |
336
|
10 |
|
curl_multi_select($this->handlerMulti(), 0.01); |
|
|
|
|
337
|
10 |
|
return $this->countActive(); |
338
|
|
|
} |
339
|
|
|
|
340
|
10 |
|
public function makePendingRequestsQue() |
|
|
|
|
341
|
|
|
{ |
|
|
|
|
342
|
|
|
|
343
|
10 |
|
$max = $this->getSimultaneousLimit(); |
|
|
|
|
344
|
10 |
|
$active = $this->countActive(); |
345
|
|
|
|
|
|
|
|
346
|
|
|
|
347
|
10 |
|
if ($active < $max) { |
|
|
|
|
348
|
|
|
|
349
|
10 |
|
$canAdd = $max - $active; |
350
|
|
|
// $pending = sizeof($this->pendingRequests); |
351
|
|
|
|
352
|
10 |
|
$add = []; |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
355
|
10 |
|
foreach ($this->pendingRequests as $task_id => $params) { |
356
|
10 |
|
if (empty($this->activeRequests[$task_id])) { |
|
|
|
|
357
|
10 |
|
$add[$task_id] = $task_id; |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
362
|
10 |
|
if (sizeof($add)) { |
|
|
|
|
363
|
10 |
|
if ($canAdd >= sizeof($add)) { |
|
|
|
|
364
|
10 |
|
$ll = $add; |
365
|
|
|
} else { |
366
|
1 |
|
$ll = array_rand($add, $canAdd); |
|
|
|
|
367
|
1 |
|
if (!is_array($ll)) { |
|
|
|
|
368
|
1 |
|
$ll = array($ll => $ll); |
|
|
|
|
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
372
|
10 |
|
foreach ($ll as $task_id) { |
373
|
10 |
|
$this->_prepareLoopQue($task_id); |
374
|
|
|
} |
375
|
|
|
|
|
|
|
|
376
|
|
|
}// if add |
377
|
|
|
}// if can add |
378
|
10 |
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* @param string $task_id |
382
|
|
|
*/ |
383
|
10 |
|
private function _prepareLoopQue($task_id) |
|
|
|
|
384
|
|
|
{ |
385
|
10 |
|
$this->activeRequests[$task_id] = 1; |
386
|
10 |
|
$this->waitRequests++; |
387
|
|
|
|
388
|
10 |
|
$h = $this->pendingRequests[$task_id]->handle(); |
389
|
|
|
|
390
|
|
|
// pool |
391
|
10 |
|
curl_multi_add_handle($this->handlerMulti(), $h); |
|
|
|
|
392
|
|
|
|
393
|
10 |
|
$key = (string) $h; |
|
|
|
|
394
|
10 |
|
$this->handleMapTasks[$key] = $task_id; |
395
|
10 |
|
} |
396
|
|
|
} |
397
|
|
|
|