문제

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:

  1. request is made to the application server from a desktop app. Server side php then makes a search and returns data.

  2. 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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

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:

use bandwidthThrottle\tokenBucket\Rate;
use bandwidthThrottle\tokenBucket\TokenBucket;
use bandwidthThrottle\tokenBucket\BlockingConsumer;
use bandwidthThrottle\tokenBucket\storage\PDOStorage;

$storage  = new PDOStorate("tmdb", $pdo);
$rate     = new Rate(3, Rate::SECOND);
$bucket   = new TokenBucket(30, $rate, $storage);
$consumer = new BlockingConsumer($bucket);
$bucket->bootstrap(30);

$consumer->consume(1);
$api->foo();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top