1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* hirak/prestissimo |
4
|
|
|
* @author Hiraku NAKANO |
5
|
|
|
* @license MIT https://github.com/hirak/prestissimo |
6
|
|
|
*/ |
7
|
|
|
namespace Hirak\Prestissimo; |
8
|
|
|
|
9
|
|
|
class CurlMulti |
10
|
|
|
{ |
11
|
|
|
/** @var resource curl_multi */ |
12
|
|
|
private $mh; |
13
|
|
|
|
14
|
|
|
/** @var resource[] curl */ |
15
|
|
|
private $unused = array(); |
16
|
|
|
|
17
|
|
|
/** @var resource[] curl */ |
18
|
|
|
private $using = array(); |
19
|
|
|
|
20
|
|
|
/** @var array {src: Aspects\HttpGetRequest, dest: OutputFile}*/ |
21
|
|
|
private $targets; |
22
|
|
|
|
23
|
|
|
/** @var array {src: Aspects\HttpGetRequest, dest: OutputFile}*/ |
24
|
|
|
private $runningTargets; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param int $maxConnections |
28
|
|
|
*/ |
29
|
3 |
|
public function __construct($maxConnections) |
30
|
|
|
{ |
31
|
3 |
|
$this->mh = curl_multi_init(); |
32
|
|
|
|
33
|
3 |
|
for ($i = 0; $i < $maxConnections; ++$i) { |
34
|
3 |
|
$this->unused[] = curl_init(); |
35
|
3 |
|
} |
36
|
3 |
|
} |
37
|
|
|
|
38
|
3 |
|
public function __destruct() |
39
|
|
|
{ |
40
|
3 |
|
foreach ($this->using as $ch) { |
41
|
|
|
curl_multi_remove_handle($this->mh, $ch); |
42
|
|
|
curl_close($ch); |
43
|
3 |
|
} |
44
|
|
|
|
45
|
3 |
|
foreach ($this->unused as $ch) { |
46
|
3 |
|
curl_close($ch); |
47
|
3 |
|
} |
48
|
|
|
|
49
|
3 |
|
curl_multi_close($this->mh); |
50
|
3 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param bool $pipeline |
54
|
|
|
* @codeCoverageIgnore |
55
|
|
|
*/ |
56
|
|
|
public function setupShareHandler($pipeline) |
57
|
|
|
{ |
58
|
|
|
if (function_exists('curl_share_init')) { |
59
|
|
|
$sh = curl_share_init(); |
60
|
|
|
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION); |
61
|
|
|
|
62
|
|
|
foreach ($this->unused as $ch) { |
63
|
|
|
curl_setopt($ch, CURLOPT_SHARE, $sh); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($pipeline && function_exists('curl_multi_setopt')) { |
68
|
|
|
curl_multi_setopt($this->mh, CURLMOPT_PIPELINING, true); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $targets {src: Aspects\HttpGetRequest, dest: OutputFile} |
74
|
|
|
*/ |
75
|
3 |
|
public function setTargets(array $targets) |
76
|
|
|
{ |
77
|
3 |
|
$this->targets = $targets; |
78
|
3 |
|
} |
79
|
|
|
|
80
|
3 |
|
public function setupEventLoop() |
81
|
|
|
{ |
82
|
3 |
|
while (count($this->unused) > 0 && count($this->targets) > 0) { |
83
|
2 |
|
$target = array_pop($this->targets); |
84
|
2 |
|
$ch = array_pop($this->unused); |
85
|
2 |
|
$index = (int)$ch; |
86
|
|
|
|
87
|
2 |
|
$this->using[$index] = $ch; |
88
|
2 |
|
$this->runningTargets[$index] = $target; |
89
|
2 |
|
Factory::getPreEvent($target['src'])->notify(); |
90
|
|
|
|
91
|
2 |
|
$opts = $target['src']->getCurlOpts(); |
92
|
2 |
|
unset($opts[CURLOPT_ENCODING], $opts[CURLOPT_USERPWD]); |
93
|
2 |
|
curl_setopt_array($ch, $opts); |
94
|
2 |
|
curl_setopt($ch, CURLOPT_FILE, $target['dest']->getPointer()); |
95
|
2 |
|
curl_multi_add_handle($this->mh, $ch); |
96
|
2 |
|
} |
97
|
3 |
|
} |
98
|
|
|
|
99
|
3 |
|
public function wait() |
100
|
|
|
{ |
101
|
3 |
|
$expectRunning = count($this->using); |
102
|
3 |
|
$running = 0; |
103
|
3 |
|
$retryCnt = 0; |
104
|
|
|
|
105
|
|
|
do { |
106
|
|
|
do { |
107
|
3 |
|
$stat = curl_multi_exec($this->mh, $running); |
108
|
3 |
|
} while ($stat === CURLM_CALL_MULTI_PERFORM); |
109
|
3 |
|
if (-1 === curl_multi_select($this->mh)) { |
110
|
|
|
// @codeCoverageIgnoreStart |
111
|
|
|
if ($retryCnt++ > 100) { |
112
|
|
|
throw new \RuntimeException('curl_multi_select failure'); |
113
|
|
|
} |
114
|
|
|
// @codeCoverageIgnoreEnd |
115
|
3 |
|
usleep(100000); |
116
|
3 |
|
} |
117
|
3 |
|
} while ($running > 0 && $running >= $expectRunning); |
118
|
3 |
|
} |
119
|
|
|
|
120
|
3 |
|
public function getFinishedResults() |
121
|
|
|
{ |
122
|
3 |
|
$results = array(); |
123
|
3 |
|
$successCnt = $failureCnt = 0; |
124
|
|
|
do { |
125
|
3 |
|
if ($raised = curl_multi_info_read($this->mh, $remains)) { |
126
|
2 |
|
$ch = $raised['handle']; |
127
|
2 |
|
$errno = curl_errno($ch); |
128
|
2 |
|
$info = curl_getinfo($ch); |
129
|
2 |
|
curl_setopt($ch, CURLOPT_FILE, STDOUT); |
130
|
2 |
|
$index = (int)$ch; |
131
|
2 |
|
$target = $this->runningTargets[$index]; |
132
|
2 |
|
if (CURLE_OK === $errno && 200 === $info['http_code']) { |
133
|
1 |
|
++$successCnt; |
134
|
1 |
|
$target['dest']->setSuccess(); |
135
|
1 |
|
$results[] = $info['url']; |
136
|
1 |
|
} else { |
137
|
1 |
|
++$failureCnt; |
138
|
|
|
} |
139
|
2 |
|
unset($this->using[$index], $this->runningTargets[$index], $target); |
140
|
2 |
|
curl_multi_remove_handle($this->mh, $ch); |
141
|
2 |
|
$this->unused[] = $ch; |
142
|
2 |
|
} |
143
|
3 |
|
} while ($remains > 0); |
144
|
|
|
|
145
|
3 |
|
return compact('successCnt', 'failureCnt', 'results'); |
146
|
|
|
} |
147
|
|
|
|
148
|
3 |
|
public function remain() |
149
|
|
|
{ |
150
|
3 |
|
return count($this->runningTargets) > 0; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|