ConcurrencyLimiterBuilder.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Illuminate\Redis\Limiters;
  3. use Illuminate\Support\InteractsWithTime;
  4. use Illuminate\Contracts\Redis\LimiterTimeoutException;
  5. class ConcurrencyLimiterBuilder
  6. {
  7. use InteractsWithTime;
  8. /**
  9. * The Redis connection.
  10. *
  11. * @var \Illuminate\Redis\Connections\Connection
  12. */
  13. public $connection;
  14. /**
  15. * The name of the lock.
  16. *
  17. * @var string
  18. */
  19. public $name;
  20. /**
  21. * The maximum number of entities that can hold the lock at the same time.
  22. *
  23. * @var int
  24. */
  25. public $maxLocks;
  26. /**
  27. * The number of seconds to maintain the lock until it is automatically released.
  28. *
  29. * @var int
  30. */
  31. public $releaseAfter = 60;
  32. /**
  33. * The amount of time to block until a lock is available.
  34. *
  35. * @var int
  36. */
  37. public $timeout = 3;
  38. /**
  39. * Create a new builder instance.
  40. *
  41. * @param \Illuminate\Redis\Connections\Connection $connection
  42. * @param string $name
  43. * @return void
  44. */
  45. public function __construct($connection, $name)
  46. {
  47. $this->name = $name;
  48. $this->connection = $connection;
  49. }
  50. /**
  51. * Set the maximum number of locks that can obtained per time window.
  52. *
  53. * @param int $maxLocks
  54. * @return $this
  55. */
  56. public function limit($maxLocks)
  57. {
  58. $this->maxLocks = $maxLocks;
  59. return $this;
  60. }
  61. /**
  62. * Set the number of seconds until the lock will be released.
  63. *
  64. * @param int $releaseAfter
  65. * @return $this
  66. */
  67. public function releaseAfter($releaseAfter)
  68. {
  69. $this->releaseAfter = $this->secondsUntil($releaseAfter);
  70. return $this;
  71. }
  72. /**
  73. * Set the amount of time to block until a lock is available.
  74. *
  75. * @param int $timeout
  76. * @return $this
  77. */
  78. public function block($timeout)
  79. {
  80. $this->timeout = $timeout;
  81. return $this;
  82. }
  83. /**
  84. * Execute the given callback if a lock is obtained, otherwise call the failure callback.
  85. *
  86. * @param callable $callback
  87. * @param callable|null $failure
  88. * @return mixed
  89. *
  90. * @throws \Illuminate\Contracts\Redis\LimiterTimeoutException
  91. */
  92. public function then(callable $callback, callable $failure = null)
  93. {
  94. try {
  95. return (new ConcurrencyLimiter(
  96. $this->connection, $this->name, $this->maxLocks, $this->releaseAfter
  97. ))->block($this->timeout, $callback);
  98. } catch (LimiterTimeoutException $e) {
  99. if ($failure) {
  100. return $failure($e);
  101. }
  102. throw $e;
  103. }
  104. }
  105. }