From b7ffcac8b1c9dedb8ad476fc965504c9f21604f8 Mon Sep 17 00:00:00 2001 From: Rex Lorenzo Date: Fri, 12 Oct 2018 17:17:24 -0700 Subject: [PATCH] Reusing cURL handlers --- src/RollingCurl/RollingCurl.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/RollingCurl/RollingCurl.php b/src/RollingCurl/RollingCurl.php index bdd8f4c..7ceacaf 100644 --- a/src/RollingCurl/RollingCurl.php +++ b/src/RollingCurl/RollingCurl.php @@ -222,13 +222,16 @@ public function execute() return; } + $requestHandlers = array(); foreach ($firstBatch as $request) { // setup the curl request, queue it up, and put it in the active array $ch = curl_init(); $options = $this->prepareRequestOptions($request); curl_setopt_array($ch, $options); curl_multi_add_handle($master, $ch); - $this->activeRequests[(int) $ch] = $request; + $key = (int) $ch; + $this->activeRequests[$key] = $request; + $requestHandlers[$key] = $ch; } $active = null; @@ -259,19 +262,19 @@ public function execute() $this->completedRequests[] = $request; $this->completedRequestCount++; - // start a new request (it's important to do this before removing the old one) + // remove the curl handle that just completed + curl_multi_remove_handle($master, $transfer['handle']); + + // start a new request if ($nextRequest = $this->getNextPendingRequest()) { // setup the curl request, queue it up, and put it in the active array - $ch = curl_init(); + $ch = $requestHandlers[$key]; $options = $this->prepareRequestOptions($nextRequest); curl_setopt_array($ch, $options); curl_multi_add_handle($master, $ch); $this->activeRequests[(int) $ch] = $nextRequest; } - // remove the curl handle that just completed - curl_multi_remove_handle($master, $transfer['handle']); - // if there is a callback, run it if (is_callable($this->callback)) { $callback = $this->callback;