DurationLimiterBuilder.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Illuminate\Redis\Limiters;
  3. use Illuminate\Contracts\Redis\LimiterTimeoutException;
  4. use Illuminate\Support\InteractsWithTime;
  5. class DurationLimiterBuilder
  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 locks that can be obtained per time window.
  22. *
  23. * @var int
  24. */
  25. public $maxLocks;
  26. /**
  27. * The amount of time the lock window is maintained.
  28. *
  29. * @var int
  30. */
  31. public $decay;
  32. /**
  33. * The amount of time to block until a lock is available.
  34. *
  35. * @var int
  36. */
  37. public $timeout = 3;
  38. /**
  39. * The number of milliseconds to wait between attempts to acquire the lock.
  40. *
  41. * @var int
  42. */
  43. public $sleep = 750;
  44. /**
  45. * Create a new builder instance.
  46. *
  47. * @param \Illuminate\Redis\Connections\Connection $connection
  48. * @param string $name
  49. */
  50. public function __construct($connection, $name)
  51. {
  52. $this->name = $name;
  53. $this->connection = $connection;
  54. }
  55. /**
  56. * Set the maximum number of locks that can be obtained per time window.
  57. *
  58. * @param int $maxLocks
  59. * @return $this
  60. */
  61. public function allow($maxLocks)
  62. {
  63. $this->maxLocks = $maxLocks;
  64. return $this;
  65. }
  66. /**
  67. * Set the amount of time the lock window is maintained.
  68. *
  69. * @param \DateTimeInterface|\DateInterval|int $decay
  70. * @return $this
  71. */
  72. public function every($decay)
  73. {
  74. $this->decay = $this->secondsUntil($decay);
  75. return $this;
  76. }
  77. /**
  78. * Set the amount of time to block until a lock is available.
  79. *
  80. * @param int $timeout
  81. * @return $this
  82. */
  83. public function block($timeout)
  84. {
  85. $this->timeout = $timeout;
  86. return $this;
  87. }
  88. /**
  89. * The number of milliseconds to wait between lock acquisition attempts.
  90. *
  91. * @param int $sleep
  92. * @return $this
  93. */
  94. public function sleep($sleep)
  95. {
  96. $this->sleep = $sleep;
  97. return $this;
  98. }
  99. /**
  100. * Execute the given callback if a lock is obtained, otherwise call the failure callback.
  101. *
  102. * @param callable $callback
  103. * @param callable|null $failure
  104. * @return mixed
  105. *
  106. * @throws \Illuminate\Contracts\Redis\LimiterTimeoutException
  107. */
  108. public function then(callable $callback, ?callable $failure = null)
  109. {
  110. try {
  111. return (new DurationLimiter(
  112. $this->connection, $this->name, $this->maxLocks, $this->decay
  113. ))->block($this->timeout, $callback, $this->sleep);
  114. } catch (LimiterTimeoutException $e) {
  115. if ($failure) {
  116. return $failure($e);
  117. }
  118. throw $e;
  119. }
  120. }
  121. }