Completed
Push — master ( ec8c5b...0b9fa7 )
by Ryosuke
02:59
created

Pool::reserveHaltException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace mpyw\Co\Internal;
4
use mpyw\Co\CURLException;
5
use mpyw\RuntimePromise\Deferred;
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 \RuntimeException
24
     */
25
    private $haltException;
26
27
    /**
28
     * cURL handle scheduler.
29
     * @var Scheduler
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 30
    public function __construct(CoOption $options)
45 30
    {
46 30
        $this->mh = curl_multi_init();
47 30
        $flags = (int)$options['pipeline'] + (int)$options['multiplex'] * 2;
48 30
        curl_multi_setopt($this->mh, CURLMOPT_PIPELINING, $flags);
49 30
        $this->options = $options;
50 30
        $this->scheduler = new Scheduler($options, $this->mh);
51 30
        $this->delayer = new Delayer;
52 30
    }
53
54
    /**
55
     * Call curl_multi_add_handle() or push into queue.
56
     * @param resource $ch
57
     * @param Deferred $deferred
58
     */
59 10
    public function addCurl($ch, Deferred $deferred = null)
60 10
    {
61 10
        $this->scheduler->add($ch, $deferred);
62 10
    }
63
64
    /**
65
     * Add delay.
66
     * @param int      $time
67
     * @param Deferred $deferred
68
     */
69 6
    public function addDelay($time, Deferred $deferred)
70 6
    {
71 6
        $this->delayer->add($time, $deferred);
72 4
    }
73
74
    /**
75
     * Run curl_multi_exec() loop.
76
     */
77 21
    public function wait()
78 21
    {
79 21
        curl_multi_exec($this->mh, $active); // Start requests.
80
        do {
81
            // if cURL handle is running, use curl_multi_select()
82
            // otherwise, just sleep until nearest time
83 21
            $this->scheduler->isEmpty()
84 12
                ? $this->delayer->sleep()
85 10
                : curl_multi_select($this->mh, $this->options['interval']) < 0
86 10
                  && usleep($this->options['interval'] * 1000000);
87 21
            curl_multi_exec($this->mh, $active);
88 21
            $this->scheduler->consume();
89 21
            $this->delayer->consume();
90 21
        } while (!$this->haltException && (!$this->scheduler->isEmpty() || !$this->delayer->isEmpty()));
91 21
        if ($this->haltException) {
92 4
            throw $this->haltException;
93
        }
94 17
    }
95
96
    /**
97
     * Used for halting loop.
98
     */
99 4
    public function reserveHaltException($e)
100 4
    {
101 4
        $this->haltException = $e;
102 4
    }
103
}
104