ConcurrencyLimiterBuilder.php 3.0 KB

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