88
99class RetryMiddlewareFactory
1010{
11- private const DEFAULT_MAX_RETRIES = 5 ;
12- private const INTERNAL_ERROR_RANGES = [
13- [500 , 503 ],
14- [520 , 599 ]
15- ];
16-
1711 public static function createInternalErrorsMiddleware (
1812 ?callable $ delayFunction = null ,
19- int $ maxRetries = self :: DEFAULT_MAX_RETRIES
13+ int $ maxRetries = 5
2014 ) {
21- return static ::createMiddlewareByHttpCodeRanges (
22- self ::INTERNAL_ERROR_RANGES ,
23- $ delayFunction ,
24- $ maxRetries
25- );
15+ return static ::createMiddlewareByHttpCodeRange (500 , 504 , $ delayFunction , $ maxRetries );
2616 }
2717
2818 public static function createRateLimitMiddleware (
2919 ?callable $ delayFunction = null ,
30- int $ maxRetries = self :: DEFAULT_MAX_RETRIES
20+ int $ maxRetries = 5
3121 ) {
3222 return static ::createMiddlewareByHttpCodes ([429 ], $ delayFunction , $ maxRetries );
3323 }
3424
3525 public static function createMiddlewareByHttpCodes (
3626 array $ codes ,
37- ? callable $ delayFunction ,
38- int $ maxRetries = self :: DEFAULT_MAX_RETRIES
27+ callable $ delayFunction ,
28+ int $ maxRetries = 5
3929 ): callable {
4030 return Middleware::retry (
4131 static ::getRetryFunction ($ codes , $ maxRetries ),
@@ -46,43 +36,31 @@ public static function createMiddlewareByHttpCodes(
4636 public static function createMiddlewareByHttpCodeRange (
4737 int $ from ,
4838 int $ to ,
49- ?callable $ delayFunction ,
50- int $ maxRetries = self ::DEFAULT_MAX_RETRIES
51- ): callable {
52- return static ::createMiddlewareByHttpCodeRanges ([[$ from , $ to ]], $ delayFunction , $ maxRetries );
53- }
54-
55- private static function createMiddlewareByHttpCodeRanges (
56- array $ ranges ,
57- ?callable $ delayFunction ,
58- int $ maxRetries = self ::DEFAULT_MAX_RETRIES
39+ callable $ delayFunction ,
40+ int $ maxRetries = 5
5941 ): callable {
6042 return Middleware::retry (
61- static ::getRetryFunctionByRanges ( $ ranges , $ maxRetries ),
43+ static ::getRetryFunctionByRange ( $ from , $ to , $ maxRetries ),
6244 $ delayFunction
6345 );
6446 }
6547
66- private static function getRetryFunctionByRanges (
67- array $ ranges ,
68- int $ maxRetries
69- ): callable {
48+ public static function getRetryFunctionByRange (
49+ int $ from ,
50+ int $ to ,
51+ int $ maxRetries = 5
52+ ) {
7053 return function (
7154 $ retries ,
7255 Request $ request ,
7356 ?Response $ response = null
74- ) use ($ ranges , $ maxRetries ) {
57+ ) use ($ from , $ to , $ maxRetries ) {
7558 if ($ retries >= $ maxRetries ) {
7659 return false ;
7760 }
7861
79- if (!$ response instanceof Response) {
80- return false ;
81- }
82-
83- $ statusCode = $ response ->getStatusCode ();
84- foreach ($ ranges as $ range ) {
85- if ($ statusCode >= $ range [0 ] && $ statusCode <= $ range [1 ]) {
62+ if ($ response instanceof Response) {
63+ if (($ response ->getStatusCode () >= $ from ) && ($ response ->getStatusCode () <= $ to )) {
8664 return true ;
8765 }
8866 }
@@ -91,18 +69,8 @@ private static function getRetryFunctionByRanges(
9169 };
9270 }
9371
94- public static function getRetryFunctionByRange (
95- int $ from ,
96- int $ to ,
97- int $ maxRetries = self ::DEFAULT_MAX_RETRIES
98- ): callable {
99- return static ::getRetryFunctionByRanges ([[$ from , $ to ]], $ maxRetries );
100- }
101-
102- public static function getRetryFunction (
103- array $ codes ,
104- int $ maxRetries = self ::DEFAULT_MAX_RETRIES
105- ): callable {
72+ public static function getRetryFunction (array $ codes , int $ maxRetries = 5 )
73+ {
10674 return function (
10775 $ retries ,
10876 Request $ request ,
0 commit comments