Skip to content

Add new HTTP Error codes handling in RetryMiddlewareFactory.php #433

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
68 changes: 50 additions & 18 deletions lib/RetryMiddlewareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,34 @@

class RetryMiddlewareFactory
{
private const DEFAULT_MAX_RETRIES = 5;
private const INTERNAL_ERROR_RANGES = [
[500, 503],
[520, 599]
];

public static function createInternalErrorsMiddleware(
?callable $delayFunction = null,
int $maxRetries = 5
int $maxRetries = self::DEFAULT_MAX_RETRIES
) {
return static::createMiddlewareByHttpCodeRange(500, 504, $delayFunction, $maxRetries);
return static::createMiddlewareByHttpCodeRanges(
self::INTERNAL_ERROR_RANGES,
$delayFunction,
$maxRetries
);
}

public static function createRateLimitMiddleware(
?callable $delayFunction = null,
int $maxRetries = 5
int $maxRetries = self::DEFAULT_MAX_RETRIES
) {
return static::createMiddlewareByHttpCodes([429], $delayFunction, $maxRetries);
}

public static function createMiddlewareByHttpCodes(
array $codes,
callable $delayFunction,
int $maxRetries = 5
?callable $delayFunction,
int $maxRetries = self::DEFAULT_MAX_RETRIES
): callable {
return Middleware::retry(
static::getRetryFunction($codes, $maxRetries),
Expand All @@ -36,31 +46,43 @@ public static function createMiddlewareByHttpCodes(
public static function createMiddlewareByHttpCodeRange(
int $from,
int $to,
callable $delayFunction,
int $maxRetries = 5
?callable $delayFunction,
int $maxRetries = self::DEFAULT_MAX_RETRIES
): callable {
return static::createMiddlewareByHttpCodeRanges([[$from, $to]], $delayFunction, $maxRetries);
}

private static function createMiddlewareByHttpCodeRanges(
array $ranges,
?callable $delayFunction,
int $maxRetries = self::DEFAULT_MAX_RETRIES
): callable {
return Middleware::retry(
static::getRetryFunctionByRange($from, $to, $maxRetries),
static::getRetryFunctionByRanges($ranges, $maxRetries),
$delayFunction
);
}

public static function getRetryFunctionByRange(
int $from,
int $to,
int $maxRetries = 5
) {
private static function getRetryFunctionByRanges(
array $ranges,
int $maxRetries
): callable {
return function (
$retries,
Request $request,
?Response $response = null
) use ($from, $to, $maxRetries) {
) use ($ranges, $maxRetries) {
if ($retries >= $maxRetries) {
return false;
}

if ($response instanceof Response) {
if (($response->getStatusCode() >= $from) && ($response->getStatusCode() <= $to)) {
if (!$response instanceof Response) {
return false;
}

$statusCode = $response->getStatusCode();
foreach ($ranges as $range) {
if ($statusCode >= $range[0] && $statusCode <= $range[1]) {
return true;
}
}
Expand All @@ -69,8 +91,18 @@ public static function getRetryFunctionByRange(
};
}

public static function getRetryFunction(array $codes, int $maxRetries = 5)
{
public static function getRetryFunctionByRange(
int $from,
int $to,
int $maxRetries = self::DEFAULT_MAX_RETRIES
): callable {
return static::getRetryFunctionByRanges([[$from, $to]], $maxRetries);
}

public static function getRetryFunction(
array $codes,
int $maxRetries = self::DEFAULT_MAX_RETRIES
): callable {
return function (
$retries,
Request $request,
Expand Down