1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mpyw\Co\Internal; |
4
|
|
|
use mpyw\Co\CURLException; |
5
|
|
|
use React\Promise\Deferred; |
6
|
|
|
use React\Promise\PromiseInterface; |
7
|
|
|
|
8
|
|
|
class AutoScheduler extends AbstractScheduler |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Constructor. |
12
|
|
|
* Initialize cURL multi handle. |
13
|
|
|
* @param CoOption $options |
14
|
|
|
* @param resource $mh curl_multi |
15
|
|
|
*/ |
16
|
2 |
|
public function __construct(CoOption $options, $mh) |
17
|
2 |
|
{ |
18
|
2 |
|
curl_multi_setopt($mh, CURLMOPT_MAX_TOTAL_CONNECTIONS, $options['concurrency']); |
19
|
2 |
|
$this->mh = $mh; |
20
|
2 |
|
$this->options = $options; |
21
|
2 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Call curl_multi_add_handle(). |
25
|
|
|
* @param resource $ch |
26
|
|
|
* @return PromiseInterface |
27
|
|
|
*/ |
28
|
2 |
|
public function add($ch) |
29
|
2 |
|
{ |
30
|
2 |
|
$deferred = new Deferred; |
31
|
2 |
|
$errno = curl_multi_add_handle($this->mh, $ch); |
32
|
2 |
View Code Duplication |
if ($errno !== CURLM_OK) { |
33
|
|
|
// @codeCoverageIgnoreStart |
34
|
|
|
$msg = curl_multi_strerror($errno) . ": $ch"; |
35
|
|
|
$deferred->reject(new \RuntimeException($msg)); |
36
|
|
|
return $deferred->promise(); |
37
|
|
|
// @codeCoverageIgnoreEnd |
38
|
|
|
} |
39
|
2 |
|
$this->added[(string)$ch] = $ch; |
40
|
2 |
|
$this->deferreds[(string)$ch] = $deferred; |
41
|
2 |
|
return $deferred->promise(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Are there no cURL handles? |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
2 |
|
public function isEmpty() |
49
|
2 |
|
{ |
50
|
2 |
|
return !$this->added; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Do nothing. |
55
|
|
|
*/ |
56
|
|
|
protected function interruptConsume() {} |
57
|
|
|
} |
58
|
|
|
|