diff --git a/lib/Segment/Consumer/LibCurl.php b/lib/Segment/Consumer/LibCurl.php index 09e9500..22726a3 100644 --- a/lib/Segment/Consumer/LibCurl.php +++ b/lib/Segment/Consumer/LibCurl.php @@ -84,24 +84,32 @@ public function flushBatch($messages) { curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // retry failed requests just once to diminish impact on performance - $httpResponse = $this->executePost($ch); + $responseContent = curl_exec($ch); + + $err = curl_error($ch); + if ($err) { + $this->handleError(curl_errno($ch), $err); + return; + } + + $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); //close connection curl_close($ch); $elapsed_time = microtime(true) - $start_time; - if (200 != $httpResponse) { + if (200 != $responseCode) { // log error - $this->handleError($ch, $httpResponse); + $this->handleError($responseCode, $responseContent); - if (($httpResponse >= 500 && $httpResponse <= 600) || 429 == $httpResponse) { + if (($responseCode >= 500 && $responseCode <= 600) || 429 == $responseCode) { // If status code is greater than 500 and less than 600, it indicates server error // Error code 429 indicates rate limited. // Retry uploading in these cases. usleep($backoff * 1000); $backoff *= 2; - } elseif ($httpResponse >= 400) { + } elseif ($responseCode >= 400) { break; } } else { @@ -109,13 +117,6 @@ public function flushBatch($messages) { } } - return $httpResponse; - } - - public function executePost($ch) { - curl_exec($ch); - $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - - return $httpCode; + return $responseCode; } }