@@ -37,6 +37,7 @@ public function send(Request $request, MockClient $mockClient = null, callable $
3737 $ maxTries = $ request ->tries ?? $ this ->tries ?? 1 ;
3838 $ retryInterval = $ request ->retryInterval ?? $ this ->retryInterval ?? 0 ;
3939 $ throwOnMaxTries = $ request ->throwOnMaxTries ?? $ this ->throwOnMaxTries ?? true ;
40+ $ useExponentialBackoff = $ request ->useExponentialBackoff ?? $ this ->useExponentialBackoff ?? false ;
4041
4142 if ($ maxTries <= 0 ) {
4243 $ maxTries = 1 ;
@@ -53,7 +54,11 @@ public function send(Request $request, MockClient $mockClient = null, callable $
5354 // the interval (if it has been provided)
5455
5556 if ($ attempts > 1 ) {
56- usleep ($ retryInterval * 1000 );
57+ $ sleepTime = $ useExponentialBackoff
58+ ? $ retryInterval * (2 ** ($ attempts - 2 )) * 1000
59+ : $ retryInterval * 1000 ;
60+
61+ usleep ($ sleepTime );
5762 }
5863
5964 try {
@@ -151,11 +156,12 @@ public function sendAsync(Request $request, MockClient $mockClient = null): Prom
151156 *
152157 * @param callable(\Throwable, \Saloon\Http\Request): (bool)|null $handleRetry
153158 */
154- public function sendAndRetry (Request $ request , int $ tries , int $ interval = 0 , callable $ handleRetry = null , bool $ throw = true , MockClient $ mockClient = null ): Response
159+ public function sendAndRetry (Request $ request , int $ tries , int $ interval = 0 , callable $ handleRetry = null , bool $ throw = true , MockClient $ mockClient = null , bool $ useExponentialBackoff = false ): Response
155160 {
156161 $ request ->tries = $ tries ;
157162 $ request ->retryInterval = $ interval ;
158163 $ request ->throwOnMaxTries = $ throw ;
164+ $ request ->useExponentialBackoff = $ useExponentialBackoff ;
159165
160166 return $ this ->send ($ request , $ mockClient , $ handleRetry );
161167 }
0 commit comments