Blame view

app/Support/ConcurrentSearch.php 924 Bytes
e77200db5   nologostudio.ru   Initial commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  <?php
  
  namespace FootyRoom\Support;
  
  use FootyRoom\Support\ConcurrencyLimiter;
  use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
  
  class ConcurrentSearch
  {
      /** @var int */
      protected const LIMIT = 50;
  
      /** @var int */
      protected const RESET_INTERVAL_MINUTES = 10;
  
      /** @var string */
      protected const KEY = 'concurrent-search';
  
      /** @var \FootyRoom\Support\ConcurrencyLimiter */
      protected $limiter;
  
      public function __construct(ConcurrencyLimiter $limiter)
      {
          $this->limiter = $limiter;
      }
  
      /**
       * Start search.
       */
      public function start(): void
      {
          if (!$this->limiter->lock(self::KEY, self::LIMIT, self::RESET_INTERVAL_MINUTES)) {
              throw new TooManyRequestsHttpException();
          }
      }
  
      /**
       * End search.
       */
      public function end(): void
      {
          $this->limiter->release(self::KEY);
      }
  }