diff --git a/src/Illuminate/Redis/Connections/Connection.php b/src/Illuminate/Redis/Connections/Connection.php index 17769a4868ac..eb549c892a84 100644 --- a/src/Illuminate/Redis/Connections/Connection.php +++ b/src/Illuminate/Redis/Connections/Connection.php @@ -3,8 +3,8 @@ namespace Illuminate\Redis\Connections; use Closure; -use Illuminate\Redis\Limiters\DurationLimiter; -use Illuminate\Redis\Limiters\ConcurrencyLimiter; +use Illuminate\Redis\Limiters\DurationLimiterBuilder; +use Illuminate\Redis\Limiters\ConcurrencyLimiterBuilder; /** * @mixin \Predis\Client @@ -32,14 +32,12 @@ abstract public function createSubscription($channels, Closure $callback, $metho * Funnel a callback for a maximum number of simultaneous executions. * * @param string $name - * @param int $maxLocks - * @param int $seconds - * @param callable $callback - * @param int $timeout - * @return mixed + * @return \Illuminate\Redis\Limiters\ConcurrencyLimiterBuilder */ - public function funnel($name, $maxLocks, $seconds, callable $callback, $timeout = 10) + public function funnel($name) { + return new ConcurrencyLimiterBuilder($this, $name); + return (new ConcurrencyLimiter($this, $name, $maxLocks, $seconds))->block($timeout, $callback); } @@ -47,15 +45,11 @@ public function funnel($name, $maxLocks, $seconds, callable $callback, $timeout * Throttle a callback for a maximum number of executions over a given duration. * * @param string $name - * @param int $maxLocks - * @param int $decay - * @param callable $callback - * @param int $timeout - * @return mixed + * @return \Illuminate\Redis\Limiters\DurationLimiterBuilder */ - public function throttle($name, $maxLocks, $decay, callable $callback, $timeout = 10) + public function throttle($name) { - return (new DurationLimiter($this, $name, $maxLocks, $decay))->block($timeout, $callback); + return new DurationLimiterBuilder($this, $name); } /** diff --git a/src/Illuminate/Redis/Limiters/ConcurrencyLimiterBuilder.php b/src/Illuminate/Redis/Limiters/ConcurrencyLimiterBuilder.php new file mode 100644 index 000000000000..ab74b1b3633b --- /dev/null +++ b/src/Illuminate/Redis/Limiters/ConcurrencyLimiterBuilder.php @@ -0,0 +1,120 @@ +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; + } + } +} diff --git a/src/Illuminate/Redis/Limiters/DurationLimiterBuilder.php b/src/Illuminate/Redis/Limiters/DurationLimiterBuilder.php new file mode 100644 index 000000000000..c752ac42e46e --- /dev/null +++ b/src/Illuminate/Redis/Limiters/DurationLimiterBuilder.php @@ -0,0 +1,120 @@ +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; + } + } +}