bandwidthThrottle\tokenBucket\TokenBucket::bootstrap PHP Method

bootstrap() public method

If the storage was already bootstrapped this method returns silently. While you could call bootstrap() on each request, you should not do that! This method will do unnecessary storage communications just to see that bootstrapping was performed already. You therefore should call that method in your application's bootstrap or deploy process. This method is threadsafe.
public bootstrap ( integer $tokens )
$tokens integer Initial amount of tokens, default is 0.
    public function bootstrap($tokens = 0)
    {
        try {
            if ($tokens > $this->capacity) {
                throw new \LengthException("Initial token amount ({$tokens}) is larger than the capacity ({$this->capacity}).");
            }
            if ($tokens < 0) {
                throw new \InvalidArgumentException("Initial token amount ({$tokens}) should be greater than 0.");
            }
            $this->storage->getMutex()->check(function () {
                return !$this->storage->isBootstrapped();
            })->then(function () use($tokens) {
                $this->storage->bootstrap($this->tokenConverter->convertTokensToMicrotime($tokens));
            });
        } catch (MutexException $e) {
            throw new StorageException("Could not lock bootstrapping", 0, $e);
        }
    }

Usage Example

 /**
  * Initialize the a new token bucket, then proload that bucket with tokens
  * @param  string       $bucketName The name of the bucket to create
  * @param  Rate         $fillRate   Rate Object used to define fill rate
  * @return TokenBucket              The initialized bucket
  * @access private
  */
 private function initializeBucket($bucketName, Rate $fillRate)
 {
     $storage = new PHPRedisStorage($bucketName, $this->redisObj);
     $bucket = new TokenBucket(self::MAXBUCKETSIZE, $fillRate, $storage);
     $bucket->bootstrap(self::MAXBUCKETSIZE);
     return $bucket;
 }
All Usage Examples Of bandwidthThrottle\tokenBucket\TokenBucket::bootstrap