php-shopify icon indicating copy to clipboard operation
php-shopify copied to clipboard

Automatic throttling of API calls

Open warely opened this issue 6 years ago • 4 comments

Is it possible to build in an automatic throttling of API calls to avoid the too many requests error? There are many other implementations of the leaky bucket algorithm.

Basically, if the API response is the too many requests error, wait X seconds and retry the request.

warely avatar Feb 25 '19 17:02 warely

It's already there.

tareqtms avatar Feb 25 '19 17:02 tareqtms

@warely just bare in mind that the library will keep retrying indefinitely in a while loop until it gets a success. This can be a huge problem if you have an application that runs concurrent workers that runs jobs in parallel. It can leave some of your worker processes hanging for indefinite amounts of time waiting for requests to succeed.

I ended up forking this repo to remove the retry logic and implemented it into our queue retry system.

shawnhind avatar Feb 25 '19 19:02 shawnhind

@shawnhind Can you expand into what you did to create a queue retry system? I assume the loops can be broken by setting hard call limits for script execution times on the PHP script?

warely avatar Feb 26 '19 17:02 warely

@warely well our app is in laravel and we use background jobs and Laravel's queue system to process some shopify data. When an API call fails due to throttling we re-queue the job with a delay. But the queue is set up to be prioritized in such a way that if there are other jobs to process it won't keep attempting ones that are failing. If I had left this libraries throttling code in, all the queue workers who started getting a throttle error would have kept being retried and tying up that worker instead of processing other things.

shawnhind avatar Feb 26 '19 21:02 shawnhind