Skip to content
Merged
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: 18 additions & 50 deletions lib/RetryMiddlewareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,24 @@

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 = self::DEFAULT_MAX_RETRIES
int $maxRetries = 5
) {
return static::createMiddlewareByHttpCodeRanges(
self::INTERNAL_ERROR_RANGES,
$delayFunction,
$maxRetries
);
return static::createMiddlewareByHttpCodeRange(500, 504, $delayFunction, $maxRetries);
}

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

public static function createMiddlewareByHttpCodes(
array $codes,
?callable $delayFunction,
int $maxRetries = self::DEFAULT_MAX_RETRIES
callable $delayFunction,
int $maxRetries = 5
): callable {
return Middleware::retry(
static::getRetryFunction($codes, $maxRetries),
Expand All @@ -46,43 +36,31 @@ public static function createMiddlewareByHttpCodes(
public static function createMiddlewareByHttpCodeRange(
int $from,
int $to,
?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 $delayFunction,
int $maxRetries = 5
): callable {
return Middleware::retry(
static::getRetryFunctionByRanges($ranges, $maxRetries),
static::getRetryFunctionByRange($from, $to, $maxRetries),
$delayFunction
);
}

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

if (!$response instanceof Response) {
return false;
}

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

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 {
public static function getRetryFunction(array $codes, int $maxRetries = 5)
{
return function (
$retries,
Request $request,
Expand Down