Skip to content

Commit 01d981a

Browse files
author
Greg Bowler
committed
feature: wait for network activity after starting the request
closes #157
1 parent dc0ebc6 commit 01d981a

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/RequestResolver.php

+16
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public function add(
7171
}
7272

7373
// curlopt3: Finally, hard-code these curlopt settings:
74+
$curl->setOpt(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
7475
$curl->setOpt(CURLOPT_RETURNTRANSFER, false);
7576
$curl->setOpt(CURLOPT_HEADER, false);
7677
$curl->setOpt(CURLOPT_HEADERFUNCTION, $this->writeHeader(...));
@@ -102,6 +103,8 @@ public function tick():void {
102103
foreach($this->curlMultiList as $i => $curlMulti) {
103104
$active = 0;
104105

106+
// 1) This first do-while loop initiates all underlying curl handles, but on
107+
// slow networks, this might not be enough to download all responses...
105108
do {
106109
$status = $curlMulti->exec($active);
107110
}
@@ -127,6 +130,19 @@ public function tick():void {
127130
$response = null;
128131
$this->deferredList[$i] = null;
129132
}
133+
else {
134+
while($active && $status === CURLM_OK) {
135+
// 2) We must wait for network activity, because there may be no activity
136+
// between us starting the request and checking the response, especially with
137+
// slow servers.
138+
if($curlMulti->select() !== -1) {
139+
do {
140+
$status = $curlMulti->exec($active);
141+
}
142+
while($status === CURLM_CALL_MULTI_PERFORM);
143+
}
144+
}
145+
}
130146
}
131147

132148
if($totalActive === 0) {

0 commit comments

Comments
 (0)