Skip to content

Commit

Permalink
feature: wait for network activity after starting the request
Browse files Browse the repository at this point in the history
closes #157
  • Loading branch information
g105b committed Sep 24, 2024
1 parent dc0ebc6 commit 01d981a
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/RequestResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function add(
}

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

// 1) This first do-while loop initiates all underlying curl handles, but on
// slow networks, this might not be enough to download all responses...
do {
$status = $curlMulti->exec($active);
}
Expand All @@ -127,6 +130,19 @@ public function tick():void {
$response = null;
$this->deferredList[$i] = null;
}
else {
while($active && $status === CURLM_OK) {
// 2) We must wait for network activity, because there may be no activity
// between us starting the request and checking the response, especially with
// slow servers.
if($curlMulti->select() !== -1) {
do {
$status = $curlMulti->exec($active);
}
while($status === CURLM_CALL_MULTI_PERFORM);
}
}
}
}

if($totalActive === 0) {
Expand Down

0 comments on commit 01d981a

Please sign in to comment.