I am using tmdb API that has a new limit on requests which is 30 requests in 10 seconds per IP. I could track these calls by having a table in MySQL that holds the amount of requests per the last 10 seconds but how can I accomplish this with the different IP addresses?
The way my app works is:
request is made to the application server from a desktop app. Server side php then makes a search and returns data.
desktop app then makes a different call to app server and that in turns makes several api calls to tmdb (4-7) and other api's.
There can be anywhere from a few search requests to a few thousands in some cases. What's the best way to keep track and delay api calls to keep it below the limit?
The token bucket algorithm can do rate limiting. I made a threadsafe implementation with a shared storage for you: bandwidth-throttle/token-bucket
This example would throttle the application to a global consumption rate of 3 requests per second:
You can use php sleep function to limit the rate at which you make requests. Or indeed use something like usleep function to make a halt by giving in number of microseconds by which you want delay.
if your server is sending requests to another server which has a rate limit, you'll need a global way to keep track of requests from your server, regardless of which client is doing requests to your server etc.
I would use a flat file, and save unix time and a delimiter for each request, and if the file contains ten of those, you've done ten requests, and you'll need to clean the file and remove any timestamp that is older than ten seconds, and that would give you the simplest form of a PHP rate limiter there is.
There are of course more advanced ways to this, but I'd start there, and make sure you test it so you don't loose your API access etc.