PHP code example of otzy / intensity-throttle

1. Go to this page and download the library: Download otzy/intensity-throttle 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/ */

    

otzy / intensity-throttle example snippets


use Otzy\Intensity\InProcessStorage;
use Otzy\Intensity\IntensityThrottle;

//Create throttle
$throttle = new IntensityThrottle('test', new InProcessStorage());

//add leaky bucket to the throttle. You can add so many buckets as you wish
$max_drops = 3; //this is a backet volume
$time_span = 1; //how long it takes to get bucket empty

//add a bucket
$throttle->addLimit($max_drops, $time_span)

//simulate events
$start = microtime(true);
for ($i = 0; $i < 100; $i++) {

    //Register event. If buckets get overflowed, the method drip() returns false. If there is still space in them, it returns true
    $drip_result = $throttle->drip();

    if ($drip_result === false) {
        printf("Throttle Test: Drops limit exceeded after %.3f seconds.", (microtime(true) - $start));
        return;
    }

    usleep(250000);
}