Skip to content

Commit

Permalink
helpers for limiters
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Aug 24, 2017
1 parent fcbba2d commit ceb260e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
30 changes: 30 additions & 0 deletions src/Illuminate/Redis/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Illuminate\Redis\Connections;

use Closure;
use Illuminate\Redis\Limiters\DurationLimiter;
use Illuminate\Redis\Limiters\ConcurrencyLimiter;

/**
* @mixin \Predis\Client
Expand All @@ -26,6 +28,34 @@ abstract class Connection
*/
abstract public function createSubscription($channels, Closure $callback, $method = 'subscribe');

/**
* Funnel a callback for a maximum number of simultaneous executions.
*
* @param string $name
* @param int $maxLocks
* @param int $seconds
* @param callable $callback
* @return mixed
*/
public function funnel($name, $maxLocks, $seconds, callable $callback)
{
return (new ConcurrencyLimiter($this, $name, $maxLocks, $seconds))->block($callback);
}

/**
* Throttle a callback for a maximum number of executions over a given duration.
*
* @param string $name
* @param int $maxLocks
* @param int $seconds
* @param callable $callback
* @return mixed
*/
public function throttle($name, $maxLocks, $seconds, callable $callback)
{
return (new DurationLimiter($this, $name, $maxLocks, $seconds))->block($callback);
}

/**
* Get the underlying Redis client.
*
Expand Down
10 changes: 5 additions & 5 deletions src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ConcurrencyLimiter
*
* @var int
*/
protected $size;
protected $maxLocks;

/**
* The number of seconds a slot should be maintained.
Expand All @@ -39,16 +39,16 @@ class ConcurrencyLimiter
*
* @param \Illuminate\Redis\Connections\Connection $redis
* @param string $name
* @param int $size
* @param int $maxLocks
* @param int $seconds
* @return void
*/
public function __construct($redis, $name, $size, $seconds)
public function __construct($redis, $name, $maxLocks, $seconds)
{
$this->name = $name;
$this->size = $size;
$this->redis = $redis;
$this->seconds = $seconds;
$this->maxLocks = $maxLocks;
}

/**
Expand Down Expand Up @@ -89,7 +89,7 @@ protected function acquire()
{
$slots = array_map(function ($i) {
return $this->name.$i;
}, range(1, $this->size));
}, range(1, $this->maxLocks));

return $this->redis->eval($this->luaScript(), count($slots),
...array_merge($slots, [$this->name, $this->seconds])
Expand Down
10 changes: 5 additions & 5 deletions src/Illuminate/Redis/Limiters/DurationLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DurationLimiter
*
* @var int
*/
private $size;
private $maxLocks;

/**
* The number of seconds a slot should be maintained.
Expand All @@ -39,16 +39,16 @@ class DurationLimiter
*
* @param \Illuminate\Redis\Connections\Connection $redis
* @param string $name
* @param int $size
* @param int $maxLocks
* @param int $seconds
* @return void
*/
public function __construct($redis, $name, $size, $seconds)
public function __construct($redis, $name, $maxLocks, $seconds)
{
$this->name = $name;
$this->size = $size;
$this->redis = $redis;
$this->seconds = $seconds;
$this->maxLocks = $maxLocks;
}

/**
Expand Down Expand Up @@ -86,7 +86,7 @@ public function block($timeout, $callback = null)
protected function acquire()
{
return $this->redis->eval($this->luaScript(), 1,
$this->name, microtime(true), time(), $this->seconds, $this->size
$this->name, microtime(true), time(), $this->seconds, $this->maxLocks
);
}

Expand Down

0 comments on commit ceb260e

Please sign in to comment.