Skip to content

Commit f1690f3

Browse files
Merge pull request #491 from HubSpot/feature/updateRetryMiddlewareFactory
Update retry middleware factory
2 parents 021e0d9 + 621be99 commit f1690f3

1 file changed

Lines changed: 61 additions & 19 deletions

File tree

lib/RetryMiddlewareFactory.php

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,34 @@
88

99
class RetryMiddlewareFactory
1010
{
11+
public const DEFAULT_MAX_RETRIES = 5;
12+
public const INTERNAL_ERROR_RANGES = [
13+
['from' => 500, 'to' => 503],
14+
['from' => 520, 'to' => 599]
15+
];
16+
1117
public static function createInternalErrorsMiddleware(
1218
?callable $delayFunction = null,
13-
int $maxRetries = 5
19+
int $maxRetries = static::DEFAULT_MAX_RETRIES
1420
) {
15-
return static::createMiddlewareByHttpCodeRange(500, 504, $delayFunction, $maxRetries);
21+
return static::createMiddlewareByHttpCodeRanges(
22+
static::INTERNAL_ERROR_RANGES,
23+
$delayFunction,
24+
$maxRetries
25+
);
1626
}
1727

1828
public static function createRateLimitMiddleware(
1929
?callable $delayFunction = null,
20-
int $maxRetries = 5
30+
int $maxRetries = static::DEFAULT_MAX_RETRIES
2131
) {
2232
return static::createMiddlewareByHttpCodes([429], $delayFunction, $maxRetries);
2333
}
2434

2535
public static function createMiddlewareByHttpCodes(
2636
array $codes,
27-
callable $delayFunction,
28-
int $maxRetries = 5
37+
?callable $delayFunction,
38+
int $maxRetries = static::DEFAULT_MAX_RETRIES
2939
): callable {
3040
return Middleware::retry(
3141
static::getRetryFunction($codes, $maxRetries),
@@ -36,41 +46,73 @@ public static function createMiddlewareByHttpCodes(
3646
public static function createMiddlewareByHttpCodeRange(
3747
int $from,
3848
int $to,
39-
callable $delayFunction,
40-
int $maxRetries = 5
49+
?callable $delayFunction,
50+
int $maxRetries = static::DEFAULT_MAX_RETRIES
51+
): callable {
52+
return static::createMiddlewareByHttpCodeRanges([[$from, $to]], $delayFunction, $maxRetries);
53+
}
54+
55+
/**
56+
* Create middleware by http code ranges
57+
* @param array $ranges [['from' => int, 'to' => int]]
58+
*/
59+
public static function createMiddlewareByHttpCodeRanges(
60+
array $ranges,
61+
?callable $delayFunction,
62+
int $maxRetries = static::DEFAULT_MAX_RETRIES
4163
): callable {
4264
return Middleware::retry(
43-
static::getRetryFunctionByRange($from, $to, $maxRetries),
65+
static::getRetryFunctionByRanges($ranges, $maxRetries),
4466
$delayFunction
4567
);
4668
}
4769

48-
public static function getRetryFunctionByRange(
49-
int $from,
50-
int $to,
51-
int $maxRetries = 5
52-
) {
70+
/**
71+
* Get retry function by code ranges
72+
* @param array $ranges [['from' => int, 'to' => int]]
73+
*/
74+
public static function getRetryFunctionByRanges(
75+
array $ranges,
76+
int $maxRetries
77+
): callable {
5378
return function (
5479
$retries,
5580
Request $request,
5681
?Response $response = null
57-
) use ($from, $to, $maxRetries) {
82+
) use ($ranges, $maxRetries) {
5883
if ($retries >= $maxRetries) {
5984
return false;
6085
}
6186

62-
if ($response instanceof Response) {
63-
if (($response->getStatusCode() >= $from) && ($response->getStatusCode() <= $to)) {
64-
return true;
87+
if (!$response instanceof Response) {
88+
return false;
89+
}
90+
91+
$statusCode = $response->getStatusCode();
92+
foreach ($ranges as $range) {
93+
if (key_exists('from', $range) && key_exists('to', $range)) {
94+
if ($statusCode >= $range['from'] && $statusCode <= $range['to']) {
95+
return true;
96+
}
6597
}
6698
}
6799

68100
return false;
69101
};
70102
}
71103

72-
public static function getRetryFunction(array $codes, int $maxRetries = 5)
73-
{
104+
public static function getRetryFunctionByRange(
105+
int $from,
106+
int $to,
107+
int $maxRetries = static::DEFAULT_MAX_RETRIES
108+
): callable {
109+
return static::getRetryFunctionByRanges([['from' => $from, 'to' => $to]], $maxRetries);
110+
}
111+
112+
public static function getRetryFunction(
113+
array $codes,
114+
int $maxRetries = static::DEFAULT_MAX_RETRIES
115+
): callable {
74116
return function (
75117
$retries,
76118
Request $request,

0 commit comments

Comments
 (0)