ConcurrencyLimiterBuilder.php
TLDR
The ConcurrencyLimiterBuilder.php
file is a PHP file located at src/Illuminate/Redis/Limiters
directory. It contains a class called ConcurrencyLimiterBuilder
that is used to build a concurrency limiter for Redis. The class provides methods to set various properties of the limiter and execute a callback if a lock is obtained.
Methods
limit($maxLocks)
This method sets the maximum number of locks that can be obtained per time window. It takes an integer parameter $maxLocks
and returns an instance of the ConcurrencyLimiterBuilder
class.
releaseAfter($releaseAfter)
This method sets the number of seconds until the lock will be automatically released. It takes an integer parameter $releaseAfter
representing the number of seconds and returns an instance of the ConcurrencyLimiterBuilder
class.
block($timeout)
This method sets the amount of time to block until a lock is available. It takes an integer parameter $timeout
representing the timeout in seconds and returns an instance of the ConcurrencyLimiterBuilder
class.
sleep($sleep)
This method sets the number of milliseconds to wait between lock acquisition attempts. It takes an integer parameter $sleep
representing the sleep time in milliseconds and returns an instance of the ConcurrencyLimiterBuilder
class.
then($callback, $failure)
This method executes the given callback if a lock is obtained, otherwise calls the failure callback. It takes two parameters: $callback
, which is a callable and represents the callback to be executed if a lock is obtained; and $failure
, which is an optional callable and represents the callback to be executed if a lock is not obtained. It returns the result of the executed callback or throws a LimiterTimeoutException
if a lock is not obtained and no failure callback is provided.
Classes
ConcurrencyLimiterBuilder
This class is used to build a concurrency limiter for Redis. It has the following properties:
-
connection
: The Redis connection. -
name
: The name of the lock. -
maxLocks
: The maximum number of entities that can hold the lock at the same time. -
releaseAfter
: The number of seconds to maintain the lock until it is automatically released. -
timeout
: The amount of time to block until a lock is available. -
sleep
: The number of milliseconds to wait between attempts to acquire the lock.
The class has a constructor that takes a connection and a name as parameters and initializes the corresponding properties. It also uses the InteractsWithTime
trait.
The class provides methods to set the maximum number of locks, release time, block timeout, and sleep time. It also provides a then
method to execute a callback if a lock is obtained.
(END)
<?php
namespace Illuminate\Redis\Limiters;
use Illuminate\Contracts\Redis\LimiterTimeoutException;
use Illuminate\Support\InteractsWithTime;
class ConcurrencyLimiterBuilder
{
use InteractsWithTime;
/**
* The Redis connection.
*
* @var \Illuminate\Redis\Connections\Connection
*/
public $connection;
/**
* The name of the lock.
*
* @var string
*/
public $name;
/**
* The maximum number of entities that can hold the lock at the same time.
*
* @var int
*/
public $maxLocks;
/**
* The number of seconds to maintain the lock until it is automatically released.
*
* @var int
*/
public $releaseAfter = 60;
/**
* The amount of time to block until a lock is available.
*
* @var int
*/
public $timeout = 3;
/**
* The number of milliseconds to wait between attempts to acquire the lock.
*
* @var int
*/
public $sleep = 250;
/**
* Create a new builder instance.
*
* @param \Illuminate\Redis\Connections\Connection $connection
* @param string $name
* @return void
*/
public function __construct($connection, $name)
{
$this->name = $name;
$this->connection = $connection;
}
/**
* Set the maximum number of locks that can be obtained per time window.
*
* @param int $maxLocks
* @return $this
*/
public function limit($maxLocks)
{
$this->maxLocks = $maxLocks;
return $this;
}
/**
* Set the number of seconds until the lock will be released.
*
* @param int $releaseAfter
* @return $this
*/
public function releaseAfter($releaseAfter)
{
$this->releaseAfter = $this->secondsUntil($releaseAfter);
return $this;
}
/**
* Set the amount of time to block until a lock is available.
*
* @param int $timeout
* @return $this
*/
public function block($timeout)
{
$this->timeout = $timeout;
return $this;
}
/**
* The number of milliseconds to wait between lock acquisition attempts.
*
* @param int $sleep
* @return $this
*/
public function sleep($sleep)
{
$this->sleep = $sleep;
return $this;
}
/**
* Execute the given callback if a lock is obtained, otherwise call the failure callback.
*
* @param callable $callback
* @param callable|null $failure
* @return mixed
*
* @throws \Illuminate\Contracts\Redis\LimiterTimeoutException
*/
public function then(callable $callback, callable $failure = null)
{
try {
return (new ConcurrencyLimiter(
$this->connection, $this->name, $this->maxLocks, $this->releaseAfter
))->block($this->timeout, $callback, $this->sleep);
} catch (LimiterTimeoutException $e) {
if ($failure) {
return $failure($e);
}
throw $e;
}
}
}