PHP code example of mehr-it / lara-token-bucket

1. Go to this page and download the library: Download mehr-it/lara-token-bucket 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/ */

    

mehr-it / lara-token-bucket example snippets


// Create a token bucket with rate of 5 tokens per second
// and burst size of 20 tokens.
$bucket = \TokenBucket::bucket('myBucket', 5.0, 20);

// try to take 2 tokens from the bucket
$success = $bucket->tryTake(2);

// $secUntilAvailableNext is filled with the duration until 
// another 2 tokens are available after an eventually
// successful taking
$bucket->tryTake(2, $secUntilAvailableNext);

// returns the duration until 2 tokens are available
$secUntilAvailableNext = $bucket->estimateAvailability(3);

// give back 2 tokens
$bucket->putTokens(2);

// register
\TokenBucket::registerBucket('myBucket', 5.0, 20);

// resolve
$bucket = \TokenBucket::resolveBucket('myBucket');

// create a token bucket with initial token number of 3
$bucket = \TokenBucket::bucket('myBucket', 5.0, 20, 3);