Saturday, May 20, 2017

Queuing Guzzle Requests With Limits

I'm working on a Laravel application, using Guzzle 6. A lot of functionality relies on an API, of which I've created a wrapper for.

My wrapper's a single class, that creates the Guzzle client in the __construct(), and has a variety of public functions which return responses from Guzzle requests.

The API I'm using has a limit of 40 requests every 10 seconds. I am caching things, so it would be very rare to hit this limit, but I'd like to know that my application wouldn't just die if it did!

So, my question is, how should I make sure I do not hit this limit? A few ideas of mine are the following:

  • Use the Laravel queuing system to place the Guzzle requests into a queue, and only process them if we still have requests left. If not, wait until the 10 second cooldown has passed...
  • Use a HandlerStack for Guzzle directly. Not sure if this is possible, but I've used the HandlerStack for caching responses before.

I'm trying to not to provoke too opinionated responses, but I'm sure there's probably a better and/or easier way than the above, or if they are good ideas, any pointers or recommendations would be great.

Thanks in advance.



via Daniel Dewhurst

Advertisement