From 9fab549e56386d5f5da395733a0beb7ce95c2b36 Mon Sep 17 00:00:00 2001 From: divinity76 Date: Fri, 19 Jun 2020 10:51:17 +0200 Subject: [PATCH] curl_multi optimizations in waitForData the old code would run the multi_handles, then wait until there was new data available on any of the sockets -or- until $timeout, and then return (and not fetch the new data), the new code will run the multi handles, then wait until there is new data available on any of the sockets -or- until $timeout, and if there is new data available on the sockets before $timeout, it will fetch the new data as well, this should make the function faster (waiting until there is data, and not fetching it, like the old function did, was kindof dumb) also the new code only runs select() if there are unfinished handles, the old code would run select() even if all handles had finished --- src/AsyncRequest/AsyncRequest.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/AsyncRequest/AsyncRequest.php b/src/AsyncRequest/AsyncRequest.php index e8e8c8a..8465640 100644 --- a/src/AsyncRequest/AsyncRequest.php +++ b/src/AsyncRequest/AsyncRequest.php @@ -87,7 +87,13 @@ public function waitForData(float $timeout = 1.0): void } while (curl_multi_exec($this->handle, $runningCount) === CURLM_CALL_MULTI_PERFORM); - curl_multi_select($this->handle, $timeout); + if($runningCount > 0){ + // still unfinished handles + if(curl_multi_select($this->handle, $timeout) > 0) { + // new data available on handles before $timeout, lets fetch it + while (curl_multi_exec($this->handle, $runningCount) === CURLM_CALL_MULTI_PERFORM); + } + } } /**