1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mpyw\Co\Internal; |
4
|
|
|
use mpyw\Co\CURLException; |
5
|
|
|
use React\Promise\PromiseInterface; |
6
|
|
|
|
7
|
|
|
class Pool |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Options. |
11
|
|
|
* @var CoOption |
12
|
|
|
*/ |
13
|
|
|
private $options; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* cURL multi handle. |
17
|
|
|
* @var resource |
18
|
|
|
*/ |
19
|
|
|
private $mh; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Used for halting loop. |
23
|
|
|
* @var \Throwable|\Exception |
24
|
|
|
*/ |
25
|
|
|
private $haltException; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* cURL handle scheduler. |
29
|
|
|
* @var AbstractScheduler |
30
|
|
|
*/ |
31
|
|
|
private $scheduler; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Delay controller. |
35
|
|
|
* @var Delayer |
36
|
|
|
*/ |
37
|
|
|
private $delayer; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor. |
41
|
|
|
* Initialize cURL multi handle. |
42
|
|
|
* @param CoOption $options |
43
|
|
|
*/ |
44
|
39 |
|
public function __construct(CoOption $options) |
45
|
39 |
|
{ |
46
|
39 |
|
$this->mh = curl_multi_init(); |
47
|
39 |
|
$flags = (int)$options['pipeline'] + (int)$options['multiplex'] * 2; |
48
|
39 |
|
curl_multi_setopt($this->mh, CURLMOPT_PIPELINING, $flags); |
49
|
39 |
|
$this->options = $options; |
50
|
39 |
|
$this->scheduler = $options['autoschedule'] |
51
|
2 |
|
? new AutoScheduler($options, $this->mh) |
52
|
37 |
|
: new ManualScheduler($options, $this->mh); |
53
|
39 |
|
$this->delayer = new Delayer; |
54
|
39 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Call curl_multi_add_handle() or push into queue. |
58
|
|
|
* @param resource $ch |
59
|
|
|
* @return PromiseInterface |
60
|
|
|
*/ |
61
|
16 |
|
public function addCurl($ch) |
62
|
16 |
|
{ |
63
|
16 |
|
return $this->scheduler->add($ch); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Add delay. |
68
|
|
|
* @param int $time |
69
|
|
|
* @return PromiseInterface |
70
|
|
|
*/ |
71
|
6 |
|
public function addDelay($time) |
72
|
6 |
|
{ |
73
|
6 |
|
return $this->delayer->add($time); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Run curl_multi_exec() loop. |
78
|
|
|
*/ |
79
|
33 |
|
public function wait() |
80
|
33 |
|
{ |
81
|
33 |
|
curl_multi_exec($this->mh, $active); // Start requests. |
82
|
|
|
do { |
83
|
|
|
// if cURL handle is running, use curl_multi_select() |
84
|
|
|
// otherwise, just sleep until nearest time |
85
|
33 |
|
$this->scheduler->isEmpty() |
86
|
18 |
|
? $this->delayer->sleep() |
87
|
16 |
|
: curl_multi_select($this->mh, $this->options['interval']) < 0 |
88
|
16 |
|
&& usleep($this->options['interval'] * 1000000); |
89
|
33 |
|
curl_multi_exec($this->mh, $active); |
90
|
33 |
|
$this->scheduler->consume(); |
91
|
33 |
|
$this->delayer->consume(); |
92
|
33 |
|
} while (!$this->haltException && (!$this->scheduler->isEmpty() || !$this->delayer->isEmpty())); |
93
|
33 |
|
if ($this->haltException) { |
94
|
11 |
|
throw $this->haltException; |
95
|
|
|
} |
96
|
23 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Used for halting loop. |
100
|
|
|
* @param \Throwable|\RuntimeException $e |
101
|
|
|
*/ |
102
|
11 |
|
public function reserveHaltException($e) |
103
|
11 |
|
{ |
104
|
11 |
|
$this->haltException = $e; |
105
|
11 |
|
} |
106
|
|
|
} |
107
|
|
|
|