mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-05 20:57:36 +02:00
Migrate the curl retry functionality into a util class for re-use, refs #599
This commit is contained in:
@@ -144,8 +144,6 @@ class CubeHandler extends AbstractProcessingHandler
|
||||
'Content-Length: ' . strlen('['.$data.']'))
|
||||
);
|
||||
|
||||
if (curl_exec($this->httpConnection) === false) {
|
||||
throw new \RuntimeException(sprintf('Curl error (code %s): %s', curl_errno($ch), curl_error($ch)));
|
||||
}
|
||||
Curl\Util::execute($ch, 5, false);
|
||||
}
|
||||
}
|
||||
|
54
src/Monolog/Handler/Curl/Util.php
Normal file
54
src/Monolog/Handler/Curl/Util.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Monolog package.
|
||||
*
|
||||
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Monolog\Handler\Curl;
|
||||
|
||||
class Util
|
||||
{
|
||||
private static $retriableErrorCodes = array(
|
||||
CURLE_COULDNT_RESOLVE_HOST,
|
||||
CURLE_COULDNT_CONNECT,
|
||||
CURLE_HTTP_NOT_FOUND,
|
||||
CURLE_READ_ERROR,
|
||||
CURLE_OPERATION_TIMEOUTED,
|
||||
CURLE_HTTP_POST_ERROR,
|
||||
CURLE_SSL_CONNECT_ERROR,
|
||||
);
|
||||
|
||||
/**
|
||||
* Executes a CURL request with optional retries and exception on failure
|
||||
*
|
||||
* @param resource $ch curl handler
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public static function execute($ch, $retries = 5, $closeAfterDone = true)
|
||||
{
|
||||
while ($retries--) {
|
||||
if (curl_exec($ch) === false) {
|
||||
|
||||
if (false === in_array(curl_errno($ch), self::$retriableErrorCodes, true) || !$retries) {
|
||||
if ($closeAfterDone) {
|
||||
curl_close($ch);
|
||||
}
|
||||
|
||||
throw new \RuntimeException(sprintf('Curl error (code %s): %s', curl_errno($ch), curl_error($ch)));
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($closeAfterDone) {
|
||||
curl_close($ch);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
@@ -92,45 +92,7 @@ class LogglyHandler extends AbstractProcessingHandler
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
$RETRY_COUNT = 5;
|
||||
$TOTAL_RETRIES = 0;
|
||||
|
||||
/*
|
||||
* Curl error code 6 : CURLE_COULDNT_RESOLVE_HOST
|
||||
* Curl error code 7 : CURLE_COULDNT_CONNECT
|
||||
* Curl error code 9 : CURLE_REMOTE_ACCESS_DENIED
|
||||
* Curl error code 22 : CURLE_HTTP_RETURNED_ERROR
|
||||
* Curl error code 25 : CURLE_UPLOAD_FAILED
|
||||
* Curl error code 26 : CURLE_READ_ERROR
|
||||
* Curl error code 28 : CURLE_OPERATION_TIMEDOUT
|
||||
* Curl error code 34 : CURLE_HTTP_POST_ERROR
|
||||
* Curl error code 35 : CURLE_SSL_CONNECT_ERROR
|
||||
*
|
||||
* Curl Error Codes : http://curl.haxx.se/libcurl/c/libcurl-errors.html
|
||||
*/
|
||||
$CurlErrorCodesForRetries = array(6,7,9,22,25,26,28,34,35);
|
||||
|
||||
do
|
||||
{
|
||||
$TOTAL_RETRIES = $TOTAL_RETRIES + 1;
|
||||
if (curl_exec($ch) === false) {
|
||||
/*
|
||||
If the error cannot be controlled by retries or
|
||||
total retry count is already completed then
|
||||
show error and break the loop
|
||||
*/
|
||||
if(in_array(curl_errno($ch),$CurlErrorCodesForRetries) === false
|
||||
|| $TOTAL_RETRIES > $RETRY_COUNT){
|
||||
echo sprintf('Curl error (code %s): %s', curl_errno($ch), curl_error($ch));
|
||||
curl_close($ch);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else{
|
||||
curl_close($ch);
|
||||
break;
|
||||
}
|
||||
}while($TOTAL_RETRIES <= $RETRY_COUNT);
|
||||
Curl\Util::execute($ch);
|
||||
}
|
||||
|
||||
protected function getDefaultFormatter()
|
||||
|
@@ -63,9 +63,6 @@ class MandrillHandler extends MailHandler
|
||||
'async' => false,
|
||||
)));
|
||||
|
||||
if (curl_exec($ch) === false) {
|
||||
throw new \RuntimeException(sprintf('Curl error (code %s): %s', curl_errno($ch), curl_error($ch)));
|
||||
}
|
||||
curl_close($ch);
|
||||
Curl\Util::execute($ch);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user