@@ -54,7 +54,7 @@ public function bootHasRateLimits(PendingRequest $pendingRequest): void
5454 }
5555 });
5656
57- $ pendingRequest ->middleware ()->onResponse (function (Response $ response ): void {
57+ $ pendingRequest ->middleware ()->onResponse (function (Response $ response ): Response {
5858 $ limitThatWasExceeded = null ;
5959 $ store = $ this ->rateLimitStore ();
6060
@@ -92,8 +92,18 @@ public function bootHasRateLimits(PendingRequest $pendingRequest): void
9292 // place. We should make sure to throw the exception here.
9393
9494 if (isset ($ limitThatWasExceeded )) {
95- $ this ->throwLimitException ($ limitThatWasExceeded );
95+ if (! $ limit ->getShouldSleep ()) {
96+ $ this ->throwLimitException ($ limitThatWasExceeded );
97+ }
98+
99+ // When the limit has been instructed to sleep() we will make the request
100+ // again, which will trigger the request middleware to sleep, and then
101+ // hopefully get a successful response afterward.
102+
103+ return $ this ->send ($ response ->getRequest ());
96104 }
105+
106+ return $ response ;
97107 }, order: PipeOrder::FIRST );
98108 }
99109
@@ -105,6 +115,16 @@ protected function getLimiterPrefix(): ?string
105115 return (new ReflectionClass ($ this ))->getShortName ();
106116 }
107117
118+ /**
119+ * Define the "Too Many Attempts" (429) limiter
120+ *
121+ * This limiter will automatically attempt to detect 429 requests.
122+ */
123+ protected function getTooManyAttemptsLimiter (): ?Limit
124+ {
125+ return Limit::custom ($ this ->handleTooManyAttempts (...));
126+ }
127+
108128 /**
109129 * Handle too many attempts (429) statuses
110130 */
@@ -209,9 +229,9 @@ public function rateLimitStore(): RateLimitStore
209229 */
210230 public function getLimits (): array
211231 {
212- $ tooManyAttemptsHandler = $ this ->detectTooManyAttempts === true ? $ this ->handleTooManyAttempts (... ) : null ;
232+ $ tooManyAttemptsLimit = $ this ->detectTooManyAttempts === true ? $ this ->getTooManyAttemptsLimiter ( ) : null ;
213233
214- return LimitHelper::configureLimits ($ this ->resolveLimits (), $ this ->getLimiterPrefix (), $ tooManyAttemptsHandler );
234+ return LimitHelper::configureLimits ($ this ->resolveLimits (), $ this ->getLimiterPrefix (), $ tooManyAttemptsLimit );
215235 }
216236
217237 /**
0 commit comments