-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92aff02
commit 3684f0c
Showing
3 changed files
with
249 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/Illuminate/Redis/Limiters/ConcurrencyLimiterBuilder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
namespace Illuminate\Redis\Limiters; | ||
|
||
use Illuminate\Support\InteractsWithTime; | ||
use Illuminate\Contracts\Redis\LimiterTimeoutException; | ||
|
||
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; | ||
|
||
/** | ||
* Create a new builder instance. | ||
* | ||
* @param \Illuminate\Redis\Connetions\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 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; | ||
} | ||
|
||
/** | ||
* Execute the given callback if a lock is obtained, otherise call the failure callback. | ||
* | ||
* @param callable $callback | ||
* @param callable $failure | ||
* @return mixed | ||
*/ | ||
public function then(callable $callback, callable $failure = null) | ||
{ | ||
try { | ||
return (new ConcurrencyLimiter( | ||
$this->connection, $this->name, $this->maxLocks, $this->releaseAfter | ||
))->block($this->timeout, $callback); | ||
} catch (LimiterTimeoutException $e) { | ||
if ($failure) { | ||
return $failure($e); | ||
} | ||
|
||
throw $e; | ||
} | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
src/Illuminate/Redis/Limiters/DurationLimiterBuilder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
namespace Illuminate\Redis\Limiters; | ||
|
||
use Illuminate\Support\InteractsWithTime; | ||
use Illuminate\Contracts\Redis\LimiterTimeoutException; | ||
|
||
class DurationLimiterBuilder | ||
{ | ||
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 locks that can obtained per time window. | ||
* | ||
* @var int | ||
*/ | ||
public $maxLocks; | ||
|
||
/** | ||
* The amount of time the lock window is maintained. | ||
* | ||
* @var int | ||
*/ | ||
public $decay; | ||
|
||
/** | ||
* The amount of time to block until a lock is available. | ||
* | ||
* @var int | ||
*/ | ||
public $timeout = 3; | ||
|
||
/** | ||
* Create a new builder instance. | ||
* | ||
* @param \Illuminate\Redis\Connetions\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 obtained per time window. | ||
* | ||
* @param int $maxLocks | ||
* @return $this | ||
*/ | ||
public function allow($maxLocks) | ||
{ | ||
$this->maxLocks = $maxLocks; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set the amount of time the lock window is maintained. | ||
* | ||
* @param int $decay | ||
* @return $this | ||
*/ | ||
public function every($decay) | ||
{ | ||
$this->decay = $this->secondsUntil($decay); | ||
|
||
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; | ||
} | ||
|
||
/** | ||
* Execute the given callback if a lock is obtained, otherise call the failure callback. | ||
* | ||
* @param callable $callback | ||
* @param callable $failure | ||
* @return mixed | ||
*/ | ||
public function then(callable $callback, callable $failure = null) | ||
{ | ||
try { | ||
return (new DurationLimiter( | ||
$this->connection, $this->name, $this->maxLocks, $this->decay | ||
))->block($this->timeout, $callback); | ||
} catch (LimiterTimeoutException $e) { | ||
if ($failure) { | ||
return $failure($e); | ||
} | ||
|
||
throw $e; | ||
} | ||
} | ||
} |