laravel / ideas

Issues board used for Laravel internals discussions.
939 stars 28 forks source link

Handle Laravel API throttling automatically (429 Too Many Requests) #1900

Open amcsi opened 5 years ago

amcsi commented 5 years ago

There's the built-in API throttling feature that comes out of the box in Laravel.

Though its primary purpose is to defend against DOS attacks, a side-effect is that especially during development, too many API requests may be made legitimately. This can cause a breakage of the flow of the app, possibly leaving it in an unusable state.

My proposal is that it would be nice to automatically handle when a response status code is 429 (Too Many Requests) that the all newer API requests be stalled, the original 429 status response code requests be retried after Retry-After seconds, then continue with the requests that were stalled.

I know that axios allows for these retry-and-until-then-stall solutions, because I myself have implemented a retry mechanism for refreshing jwt-auth tokens automatically on expiry response with this project: https://www.npmjs.com/package/axios-auth-refresh I actually made it handle 429 errors myself in my code, though the package is more aimed at refreshing tokens, so it might not be appropriate to use.

EDIT: the axios-auth-refresh project is completely appropriate for this as it turns out; it's just the name of the project that's a little misleading.

driesvints commented 5 years ago

Hey @amcsi. Thanks for posting this. This seems like an idea for the framework in general so I transfered your issue to the ideas repo so others can pitch in if they want.