Implementing Leaky Bucket Rate Limiting for External API
Hello,
I'm trying to make calls to Shopify's API using Guzzle and need rate limit the external requests.
I'm not quite sure how to do this.
I saw this library:
https://github.com/bandwidth-throttle/token-bucket
but not sure how to get it to work with Laravel (5.4). Would I need to implement the 'bootstrap' part in a service provider?
Any other recommendations on how to get this setup would be very helpful.
Thanks!
Not sure about guzzle but i need to do the same so I will just write a little check to run on each api call.
On my API call to shopify grab the
HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT header in the reponse
which will return something like:
X-Shopify-Shop-Api-Call-Limit: 32/40
if ($call_limit == '39/40') { sleep(1); // Delay the script execution for 1 seconds }
If you are at 39 calls, wait a second before making the next api call.
The call rate you are at will drop 2 every second.
thanks so much!
I forgot to mention that I did find a library that does incorporate checking from the headers as you mentioned
https://github.com/tyler-king/Basic-Shopify-API
I have to figure out the best way to integrate the package into laravel. probably a service provider.
@dhonions are you developing shopify apps using laravel?
ok, i'll look into that. unless i missed something, this library doesn't seem to read the header for rate limiting though.
or are you adding that around your API call as you mentioned earlier?
It would be nice to create a little group that is doing Shopify / Laravel development.
Please or to participate in this conversation.