Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retry logic to default http client #455

Merged
merged 5 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/MercadoPago/MercadoPagoConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class MercadoPagoConfig
/** @var string access token */
private static string $access_token = "";

/** @var int max retries */
private static int $max_retries = 3;

/** @var int retry delay (in seconds) */
private static int $retry_delay = 1;

/** @var int max connections */
private static int $max_connections = 10;

Expand Down Expand Up @@ -69,6 +75,44 @@ public static function setAccessToken(string $access_token): void
self::$access_token = $access_token;
}

/**
* Gets max retries.
* @return int max retries
*/
public static function getMaxRetries(): int
{
return self::$max_retries;
}

/**
* Sets max retries.
* @param int $max_retries max retries
* @return void max retries
*/
public static function setMaxRetries(int $max_retries): void
{
self::$max_retries = $max_retries;
}

/**
* Gets retry delay.
* @return int retry delay
*/
public static function getRetryDelay(): int
{
return self::$retry_delay;
}

/**
* Sets retry delay.
* @param int $retry_delay retry delay
* @return void retry delay
*/
public static function setRetryDelay(int $retry_delay): void
{
self::$retry_delay = $retry_delay;
}

/**
* Gets max connections.
* @return int max connections
Expand Down
43 changes: 42 additions & 1 deletion src/MercadoPago/Net/MPDefaultHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@ public function __construct(?HttpRequest $httpRequest = null)
* @return \MercadoPago\Net\MPResponse response from the request.
*/
public function send(MPRequest $request): MPResponse
{
$max_retries = MercadoPagoConfig::getMaxRetries();

for ($retry_count = 0; $retry_count < $max_retries; $retry_count++) {
try {
return $this->makeRequest($request);
} catch (MPApiException $e) {
$status_code = $e->getApiResponse()->getStatusCode();
if ($this->isServerError($status_code) && !$this->isLastRetry($retry_count)) {
sleep(MercadoPagoConfig::getRetryDelay());
} else {
throw $e;
}
} catch (Exception $e) {
if (!$this->isLastRetry($retry_count)) {
sleep(MercadoPagoConfig::getRetryDelay());
} else {
throw $e;
}
}
}

throw new Exception("Error processing request. Please try again.");
}

private function makeRequest(MPRequest $request): MPResponse
{
$request_options = $this->createHttpRequestOptions($request);
$this->httpRequest->setOptionArray($request_options);
Expand All @@ -41,7 +67,7 @@ public function send(MPRequest $request): MPResponse
$this->httpRequest->close();
throw new Exception($error_message);
}
if ($status_code < 200 || $status_code >= 300) {
if ($this->isApiError($status_code)) {
$this->httpRequest->close();
throw new MPApiException("Api error. Check response for details", $mp_response);
}
Expand All @@ -64,4 +90,19 @@ private function createHttpRequestOptions(MPRequest $request): array
CURLOPT_RETURNTRANSFER => true
);
}

private function isServerError(int $status_code): bool
{
return $status_code >= 500;
}

private function isApiError(int $status_code): bool
{
return $status_code < 200 || $status_code >= 300;
}

private function isLastRetry(int $retry_count): bool
{
return $retry_count >= MercadoPagoConfig::getMaxRetries();
}
}
Loading