-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathFsConsumer.php
214 lines (172 loc) · 5.54 KB
/
FsConsumer.php
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
declare(strict_types=1);
namespace Enqueue\Fs;
use Interop\Queue\Consumer;
use Interop\Queue\Exception\InvalidMessageException;
use Interop\Queue\Message;
use Interop\Queue\Queue;
class FsConsumer implements Consumer
{
/**
* @var FsDestination
*/
private $destination;
/**
* @var FsContext
*/
private $context;
/**
* @var int
*/
private $preFetchCount;
/**
* @var FsMessage[]
*/
private $preFetchedMessages;
/**
* In milliseconds.
*
* @var int
*/
private $pollingInterval = 100;
public function __construct(FsContext $context, FsDestination $destination, int $preFetchCount)
{
$this->context = $context;
$this->destination = $destination;
$this->preFetchCount = $preFetchCount;
$this->preFetchedMessages = [];
}
/**
* Set polling interval in milliseconds.
*/
public function setPollingInterval(int $msec): void
{
$this->pollingInterval = $msec;
}
/**
* Get polling interval in milliseconds.
*/
public function getPollingInterval(): int
{
return $this->pollingInterval;
}
/**
* @return FsDestination
*/
public function getQueue(): Queue
{
return $this->destination;
}
/**
* @return FsMessage
*/
public function receive(int $timeout = 0): ?Message
{
$timeout /= 1000;
$startAt = microtime(true);
while (true) {
$message = $this->receiveNoWait();
if ($message) {
return $message;
}
if ($timeout && (microtime(true) - $startAt) >= $timeout) {
return null;
}
usleep($this->pollingInterval * 1000);
if ($timeout && (microtime(true) - $startAt) >= $timeout) {
return null;
}
}
}
/**
* @return FsMessage
*/
public function receiveNoWait(): ?Message
{
if ($this->preFetchedMessages) {
return array_shift($this->preFetchedMessages);
}
$this->context->workWithFile($this->destination, 'c+', function (FsDestination $destination, $file) {
$count = $this->preFetchCount;
while ($count) {
$frame = $this->readFrame($file, 1);
// guards
if ($frame && false == ('|' == $frame[0] || ' ' == $frame[0])) {
throw new \LogicException(sprintf('The frame could start from either " " or "|". The malformed frame starts with "%s".', $frame[0]));
}
if (0 !== $reminder = strlen($frame) % 64) {
throw new \LogicException(sprintf('The frame size is "%d" and it must divide exactly to 64 but it leaves a reminder "%d".', strlen($frame), $reminder));
}
ftruncate($file, fstat($file)['size'] - strlen($frame));
rewind($file);
$rawMessage = str_replace('\|\{', '|{', $frame);
$rawMessage = substr(trim($rawMessage), 1);
if ($rawMessage) {
try {
$fetchedMessage = FsMessage::jsonUnserialize($rawMessage);
$expireAt = $fetchedMessage->getHeader('x-expire-at');
if ($expireAt && $expireAt - microtime(true) < 0) {
// message has expired, just drop it.
return null;
}
$this->preFetchedMessages[] = $fetchedMessage;
} catch (\Exception $e) {
throw new \LogicException(sprintf("Cannot decode json message '%s'", $rawMessage), 0, $e);
}
} else {
return null;
}
--$count;
}
});
if ($this->preFetchedMessages) {
return array_shift($this->preFetchedMessages);
}
return null;
}
public function acknowledge(Message $message): void
{
// do nothing. fs transport always works in auto ack mode
}
public function reject(Message $message, bool $requeue = false): void
{
InvalidMessageException::assertMessageInstanceOf($message, FsMessage::class);
// do nothing on reject. fs transport always works in auto ack mode
if ($requeue) {
$this->context->createProducer()->send($this->destination, $message);
}
}
public function getPreFetchCount(): int
{
return $this->preFetchCount;
}
public function setPreFetchCount(int $preFetchCount): void
{
$this->preFetchCount = $preFetchCount;
}
/**
* @param resource $file
*/
private function readFrame($file, int $frameNumber): string
{
$frameSize = 64;
$offset = $frameNumber * $frameSize;
fseek($file, -$offset, \SEEK_END);
$frame = fread($file, $frameSize);
if ('' == $frame) {
return '';
}
if (str_contains($frame, '|{')) {
return $frame;
}
$previousFrame = $this->readFrame($file, $frameNumber + 1);
if ('|' === substr($previousFrame, -1) && '{' === $frame[0]) {
$matched = [];
if (false === preg_match('/\ *?\|$/', $previousFrame, $matched)) {
throw new \LogicException('Something went completely wrong.');
}
return $matched[0].$frame;
}
return $previousFrame.$frame;
}
}