From e92a8823c0fc375dcb393fb6c9f37730ffd16418 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 12 Jul 2015 14:40:07 +0100 Subject: [PATCH] Migrate the curl retry functionality into a util class for re-use, refs #599 --- src/Monolog/Handler/CubeHandler.php | 4 +- src/Monolog/Handler/Curl/Util.php | 54 +++++++++++++++++++++++++ src/Monolog/Handler/LogglyHandler.php | 40 +----------------- src/Monolog/Handler/MandrillHandler.php | 5 +-- 4 files changed, 57 insertions(+), 46 deletions(-) create mode 100644 src/Monolog/Handler/Curl/Util.php diff --git a/src/Monolog/Handler/CubeHandler.php b/src/Monolog/Handler/CubeHandler.php index db8e6552..bf240fa7 100644 --- a/src/Monolog/Handler/CubeHandler.php +++ b/src/Monolog/Handler/CubeHandler.php @@ -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); } } diff --git a/src/Monolog/Handler/Curl/Util.php b/src/Monolog/Handler/Curl/Util.php new file mode 100644 index 00000000..8125d06b --- /dev/null +++ b/src/Monolog/Handler/Curl/Util.php @@ -0,0 +1,54 @@ + + * + * 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; + } + } +} diff --git a/src/Monolog/Handler/LogglyHandler.php b/src/Monolog/Handler/LogglyHandler.php index c59c21bd..bcd62e1c 100644 --- a/src/Monolog/Handler/LogglyHandler.php +++ b/src/Monolog/Handler/LogglyHandler.php @@ -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() diff --git a/src/Monolog/Handler/MandrillHandler.php b/src/Monolog/Handler/MandrillHandler.php index 6726e1e4..5c6b52f9 100644 --- a/src/Monolog/Handler/MandrillHandler.php +++ b/src/Monolog/Handler/MandrillHandler.php @@ -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); } }