1 | <?php |
||||
2 | require_once __DIR__ . "/_prepend.php"; |
||||
3 | |||||
4 | use PhpXmlRpc\Encoder; |
||||
5 | use PhpXmlRpc\Client; |
||||
6 | use PhpXmlRpc\PhpXmlRpc; |
||||
7 | use PhpXmlRpc\Request; |
||||
8 | use PhpXmlRpc\Response; |
||||
9 | |||||
10 | /** |
||||
11 | * A class taking advantage of cURL to send many requests in parallel (to a single server), for when the given server |
||||
12 | * does not support system.multicall method |
||||
13 | */ |
||||
14 | class ParallelClient extends Client |
||||
15 | { |
||||
16 | public function sendParallel($requests, $timeout = 0, $method = '') |
||||
17 | 1 | { |
|||
18 | if ($method == '') { |
||||
19 | 1 | $method = $this->method; |
|||
20 | 1 | } |
|||
21 | |||||
22 | /// @todo validate that $method can be handled by the current curl install |
||||
23 | |||||
24 | $handles = array(); |
||||
25 | 1 | $curl = curl_multi_init(); |
|||
26 | 1 | ||||
27 | foreach($requests as $k => $req) { |
||||
28 | 1 | $req->setDebug($this->debug); |
|||
29 | 1 | ||||
30 | $handle = $this->prepareCurlHandle( |
||||
31 | 1 | $req, |
|||
32 | 1 | $this->server, |
|||
33 | 1 | $this->port, |
|||
34 | 1 | $timeout, |
|||
35 | $this->username, |
||||
36 | 1 | $this->password, |
|||
37 | 1 | $this->authtype, |
|||
38 | 1 | $this->cert, |
|||
39 | 1 | $this->certpass, |
|||
40 | 1 | $this->cacert, |
|||
41 | 1 | $this->cacertdir, |
|||
42 | 1 | $this->proxy, |
|||
43 | 1 | $this->proxyport, |
|||
44 | 1 | $this->proxy_user, |
|||
45 | 1 | $this->proxy_pass, |
|||
46 | 1 | $this->proxy_authtype, |
|||
47 | 1 | $method, |
|||
48 | false, |
||||
49 | 1 | $this->key, |
|||
50 | 1 | $this->keypass, |
|||
51 | 1 | $this->sslversion |
|||
52 | 1 | ); |
|||
53 | curl_multi_add_handle($curl, $handle); |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
It seems like
$handle can also be of type false ; however, parameter $handle of curl_multi_add_handle() does only seem to accept CurlHandle|resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
54 | 1 | $handles[$k] = $handle; |
|||
55 | 1 | } |
|||
56 | |||||
57 | $running = 0; |
||||
58 | 1 | do { |
|||
59 | curl_multi_exec($curl, $running); |
||||
0 ignored issues
–
show
It seems like
$curl can also be of type true ; however, parameter $multi_handle of curl_multi_exec() does only seem to accept CurlMultiHandle|resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
60 | 1 | } while($running > 0); |
|||
61 | 1 | ||||
62 | $responses = array(); |
||||
63 | 1 | foreach($handles as $k => $h) { |
|||
64 | 1 | $responses[$k] = curl_multi_getcontent($handles[$k]); |
|||
65 | 1 | ||||
66 | if ($this->debug > 1) { |
||||
67 | 1 | $message = "---CURL INFO---\n"; |
|||
68 | foreach (curl_getinfo($h) as $name => $val) { |
||||
69 | if (is_array($val)) { |
||||
70 | $val = implode("\n", $val); |
||||
71 | } |
||||
72 | $message .= $name . ': ' . $val . "\n"; |
||||
73 | } |
||||
74 | $message .= '---END---'; |
||||
75 | $this->getLogger()->debugMessage($message); |
||||
76 | } |
||||
77 | |||||
78 | //curl_close($h); |
||||
79 | curl_multi_remove_handle($curl, $h); |
||||
0 ignored issues
–
show
It seems like
$curl can also be of type true ; however, parameter $multi_handle of curl_multi_remove_handle() does only seem to accept CurlMultiHandle|resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
80 | 1 | } |
|||
81 | curl_multi_close($curl); |
||||
0 ignored issues
–
show
It seems like
$curl can also be of type true ; however, parameter $multi_handle of curl_multi_close() does only seem to accept CurlMultiHandle|resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
82 | 1 | ||||
83 | foreach($responses as $k => $resp) { |
||||
84 | 1 | if (!$resp) { |
|||
85 | 1 | $responses[$k] = new Response(0, PhpXmlRpc::$xmlrpcerr['curl_fail'], PhpXmlRpc::$xmlrpcstr['curl_fail'] . ': ' . curl_error($curl)); |
|||
0 ignored issues
–
show
$curl of type CurlMultiHandle|true is incompatible with the type CurlHandle|resource expected by parameter $handle of curl_error() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
86 | } else { |
||||
87 | $responses[$k] = $requests[$k]->parseResponse($resp, true, $this->return_type); |
||||
88 | 1 | } |
|||
89 | } |
||||
90 | |||||
91 | return $responses; |
||||
92 | 1 | } |
|||
93 | } |
||||
94 | |||||
95 | $num_tests = 25; |
||||
96 | 1 | ||||
97 | $data = array(1, 1.0, 'hello world', true, '20051021T23:43:00', -1, 11.0, '~!@#$%^&*()_+|', false, '20051021T23:43:00'); |
||||
98 | 1 | $encoder = new Encoder(); |
|||
99 | 1 | $value = $encoder->encode($data, array('auto_dates')); |
|||
100 | 1 | $req = new Request('interopEchoTests.echoValue', array($value)); |
|||
101 | 1 | $reqs = array(); |
|||
102 | 1 | for ($i = 0; $i < $num_tests; $i++) { |
|||
103 | 1 | $reqs[] = $req; |
|||
104 | 1 | } |
|||
105 | |||||
106 | $client = new ParallelClient(XMLRPCSERVER); |
||||
107 | 1 | $client->no_multicall = true; |
|||
108 | 1 | ||||
109 | $t = microtime(true); |
||||
110 | 1 | $resp = $client->send($reqs); |
|||
111 | 1 | $t = microtime(true) - $t; |
|||
112 | 1 | echo "Sequential send: $t secs\n"; |
|||
113 | 1 | ||||
114 | $t = microtime(true); |
||||
115 | 1 | $resp = $client->sendParallel($reqs); |
|||
116 | 1 | $t = microtime(true) - $t; |
|||
117 | 1 | echo "Parallel send: $t secs\n"; |
|||
118 | 1 | ||||
119 | $client->no_multicall = false; |
||||
120 | 1 | $t = microtime(true); |
|||
121 | 1 | $resp = $client->send($reqs); |
|||
122 | 1 | $t = microtime(true) - $t; |
|||
123 | echo "Multicall send: $t secs\n"; |
||||
124 |