1 | <?php |
||
9 | class CurlMulti |
||
10 | { |
||
11 | const MAX_CONNECTIONS = 10; |
||
12 | |||
13 | /** @var resource<curl_multi> */ |
||
14 | private $mh; |
||
15 | |||
16 | /** @var resource<curl>[] */ |
||
17 | private $unused = array(); |
||
18 | |||
19 | /** @var resource<curl>[] */ |
||
20 | private $using = array(); |
||
21 | |||
22 | /** @var CopyRequest[] */ |
||
23 | private $requests = array(); |
||
24 | |||
25 | /** @var CopyRequest[] */ |
||
26 | private $runningRequests = array(); |
||
27 | |||
28 | /** @var bool */ |
||
29 | private $permanent = true; |
||
30 | |||
31 | private $blackhole; |
||
32 | |||
33 | /** |
||
34 | * @param bool $permanent |
||
35 | */ |
||
36 | 5 | public function __construct($permanent = true) |
|
37 | { |
||
38 | 5 | static $mh_cache, $ch_cache; |
|
39 | |||
40 | 5 | if (!$permanent || !$mh_cache) { |
|
41 | 1 | $mh_cache = curl_multi_init(); |
|
42 | |||
43 | 1 | $ch_cache = array(); |
|
44 | 1 | for ($i = 0; $i < self::MAX_CONNECTIONS; ++$i) { |
|
45 | 1 | $ch = curl_init(); |
|
46 | 1 | Share::setup($ch); |
|
47 | 1 | $ch_cache[] = $ch; |
|
48 | } |
||
49 | } |
||
50 | |||
51 | 5 | $this->mh = $mh_cache; |
|
52 | 5 | $this->unused = $ch_cache; |
|
53 | 5 | $this->permanent = $permanent; |
|
54 | |||
55 | // for PHP<5.5 @see getFinishedResults() |
||
56 | 5 | $this->blackhole = fopen('php://temp', 'wb'); |
|
57 | 5 | } |
|
58 | |||
59 | /** |
||
60 | * @codeCoverageIgnore |
||
61 | */ |
||
62 | public function __destruct() |
||
79 | |||
80 | /** |
||
81 | * @param CopyRequest[] $requests |
||
82 | */ |
||
83 | 4 | public function setRequests(array $requests) |
|
87 | |||
88 | 5 | public function setupEventLoop() |
|
102 | |||
103 | 4 | public function wait() |
|
104 | { |
||
105 | 4 | $expectRunning = count($this->using); |
|
106 | 4 | $running = 0; |
|
107 | 4 | $retryCnt = 0; |
|
108 | |||
109 | do { |
||
110 | do { |
||
111 | 4 | $stat = curl_multi_exec($this->mh, $running); |
|
112 | 4 | } while ($stat === CURLM_CALL_MULTI_PERFORM); |
|
113 | 4 | if (-1 === curl_multi_select($this->mh)) { |
|
114 | // @codeCoverageIgnoreStart |
||
115 | if ($retryCnt++ > 100) { |
||
116 | throw new FetchException('curl_multi_select failure'); |
||
117 | } |
||
118 | // @codeCoverageIgnoreEnd |
||
119 | usleep(100000); |
||
120 | } |
||
121 | 4 | } while ($running > 0 && $running >= $expectRunning); |
|
122 | 4 | } |
|
123 | |||
124 | 4 | public function getFinishedResults() |
|
125 | { |
||
126 | 4 | $urls = $errors = array(); |
|
127 | 4 | $successCnt = $failureCnt = 0; |
|
128 | do { |
||
129 | 4 | if ($raised = curl_multi_info_read($this->mh, $remains)) { |
|
130 | 4 | $ch = $raised['handle']; |
|
131 | 4 | $errno = curl_errno($ch); |
|
132 | 4 | if ($errno == CURLE_OK && $raised['result'] != CURLE_OK) { |
|
133 | $errno = $raised['result']; |
||
134 | } |
||
135 | 4 | $error = curl_error($ch); |
|
136 | 4 | $info = curl_getinfo($ch); |
|
137 | 4 | curl_setopt($ch, CURLOPT_FILE, $this->blackhole); //release file pointer |
|
138 | 4 | $index = (int)$ch; |
|
139 | 4 | $request = $this->runningRequests[$index]; |
|
140 | 4 | if (CURLE_OK === $errno && ('http' !== substr($info['url'], 0, 4) || 200 === $info['http_code'])) { |
|
141 | 2 | ++$successCnt; |
|
142 | 2 | $request->makeSuccess(); |
|
143 | 2 | $urls[] = $request->getMaskedURL(); |
|
144 | } else { |
||
145 | 2 | ++$failureCnt; |
|
146 | 2 | $errors[$request->getMaskedURL()] = "$errno: $error"; |
|
147 | } |
||
148 | 4 | unset($this->using[$index], $this->runningRequests[$index], $request); |
|
149 | 4 | curl_multi_remove_handle($this->mh, $ch); |
|
150 | 4 | $this->unused[] = $ch; |
|
151 | } |
||
152 | 4 | } while ($remains > 0); |
|
153 | |||
154 | 4 | return compact('successCnt', 'failureCnt', 'urls', 'errors'); |
|
155 | } |
||
156 | |||
157 | 5 | public function remain() |
|
161 | } |
||
162 |