Skip to content

Commit

Permalink
bugfix: Remove php strict warning by using integer as array key inste…
Browse files Browse the repository at this point in the history
…ad of curl handle.
  • Loading branch information
yaozongyou committed Feb 21, 2017
1 parent 77ae28c commit b1d33e1
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions qcloudcos/libcurl_wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ class HttpResponse {

// A simple wrapper for libcurl using multi interface to do transfers in parallel.
class LibcurlWrapper {
private $sequence; // integer: sequence id for each request.
private $curlMultiHandle; // curl handle: curl multi handle.
private $curlHandleInfo; // array: array of active curl handle.
private $idleCurlHandle; // array: idle curl handle which can be reused.

public function __construct() {
$this->sequence = 0;
$this->curlMultiHandle = curl_multi_init();
$this->idleCurlHandle = array();
}
Expand All @@ -39,6 +41,8 @@ public function __destruct() {
}

public function startSendingRequest($httpRequest, $done) {
$this->sequence += 1;

if (count($this->idleCurlHandle) !== 0) {
$curlHandle = array_pop($this->idleCurlHandle);
} else {
Expand Down Expand Up @@ -69,8 +73,10 @@ public function startSendingRequest($httpRequest, $done) {

curl_multi_add_handle($this->curlMultiHandle, $curlHandle);

$this->curlHandleInfo[$curlHandle]['done'] = $done;
$this->curlHandleInfo[$curlHandle]['request'] = $httpRequest;

$this->curlHandleInfo[$this->sequence]['handle'] = $curlHandle;
$this->curlHandleInfo[$this->sequence]['done'] = $done;
$this->curlHandleInfo[$this->sequence]['request'] = $httpRequest;
}

public function performSendingRequest() {
Expand Down Expand Up @@ -108,8 +114,17 @@ public function performSendingRequest() {
private function processResult($info) {
$result = $info['result'];
$handle = $info['handle'];
$request = $this->curlHandleInfo[$handle]['request'];
$done = $this->curlHandleInfo[$handle]['done'];
$sequence = 0;

foreach ($this->curlHandleInfo as $key => $info) {
if ($info['handle'] === $handle) {
$sequence = $key;
break;
}
}

$request = $this->curlHandleInfo[$sequence]['request'];
$done = $this->curlHandleInfo[$sequence]['done'];
$response = new HttpResponse();

if ($result !== CURLE_OK) {
Expand Down Expand Up @@ -138,7 +153,7 @@ private function processResult($info) {
call_user_func($done, $request, $response);
}

unset($this->curlHandleInfo[$handle]);
unset($this->curlHandleInfo[$sequence]);
curl_multi_remove_handle($this->curlMultiHandle, $handle);

array_push($this->idleCurlHandle, $handle);
Expand Down

0 comments on commit b1d33e1

Please sign in to comment.