Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
consumerpollingtrait.php
1<?php
2
4
6
11{
16 protected int $pollingInterval = 1000;
17
21 public function setPollingInterval(int $msec): self
22 {
23 $this->pollingInterval = $msec;
24 return $this;
25 }
26
30 public function getPollingInterval(): int
31 {
32 return $this->pollingInterval;
33 }
34
35 public function receive(int $timeout = 0): ?Interfaces\Message
36 {
37 $timeout *= 1000; // from milliseconds to microseconds
38 $startAt = microtime(true);
39
40 while(true)
41 {
42 $message = $this->receiveNoWait();
43
44 if($message)
45 {
46 return $message;
47 }
48
49 if($timeout)
50 {
51
52 $timeSpent = microtime(true) - $startAt;
53 $timeSpent *= 1000000; // from seconds to microseconds
54 $timeLeft = $timeout - $timeSpent;
55
56 // No time left to wait
57 if($timeLeft <= 0)
58 {
59 return null;
60 }
61
62 // We pay attention not to wait too long to go over the timeout limit
63 $sleep = min($timeLeft, $this->pollingInterval * 1000);
64
65 }
66 else
67 {
68 $sleep = $this->pollingInterval * 1000;
69 }
70
71 usleep((int)$sleep);
72 }
73 }
74}