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);
}
}
/** * 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; }