1. Go to this page and download the library: Download xp-forge/ratelimit library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
xp-forge / ratelimit example snippets
use util\invoke\RateLimiting;
$rateLimiter= new RateLimiting(2);
foreach ($tasks as $task) {
$rateLimiter->acquire(); // will wait if necessary
$task->run();
}
use util\invoke\{RateLimiting, Rate, Per};
$rateLimiter= new RateLimiting(new Rate(1000000, Per::$MINUTE));
while ($bytes= $source->read()) {
$rateLimiter->acquire(strlen($bytes));
$target->write($bytes);
}
use web\{Filter, Error};
use util\invoke\{RateLimiting, Rate, Per};
class RateLimitingFilter implements Filter {
private $rates, $rate, $timeout;
public function __construct(KeyValueStorage $rates) {
$this->rates= $rates;
$this->rate= new Rate(5000, Per::$HOUR);
$this->timeout= 0.2;
}
public function filter($request, $response, $invocation) {
$remote= $request->header('Remote-Addr');
$limits= $this->rates->get($remote) ?: new RateLimiting($this->rate);
$permitted= $limits->tryAcquiring(1, $this->timeout);
$this->rates->put($remote, $limits);
$response->header('X-RateLimit-Limit', $limits->rate()->value());
$response->header('X-RateLimit-Remaining', $limits->remaining());
$response->header('X-RateLimit-Reset', $limits->resetTime());
if (!$permitted) {
throw new Error(429, 'Rate limit exceeded');
}
return $invocation->proceed($request, $response);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.