1

I am using the 'Bandwidth Throttle' library to throttle API requests - essentially prevent someone from the same IP making tons of requests within a set timeframe. This creates a bucket (simply a file) that is stored within the buckets directory.

As this is will build up considerably over time what process does everyone use for this - would you recommend a x amount of time to purge this folder, if so what timeframe would be suggested.

use bandwidthThrottle\tokenBucket\Rate;
use bandwidthThrottle\tokenBucket\TokenBucket;
use bandwidthThrottle\tokenBucket\storage\FileStorage;

$ip = $_SERVER['REMOTE_ADDR'];
$storage = new FileStorage(__DIR__ . "/buckets/$ip.bucket"); //this will build up quickly
$rate    = new Rate(10, Rate::SECOND);
$bucket  = new TokenBucket(10, $rate, $storage);
$bucket->bootstrap(10);

if (!$bucket->consume(1, $seconds)) {
  http_response_code(429);
  header(sprintf("Retry-After: %d", floor($seconds)));
  exit(); 
}
Zabs
  • 13,852
  • 45
  • 173
  • 297

0 Answers0