-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRedisTransport.php
More file actions
272 lines (228 loc) · 9.1 KB
/
Copy pathRedisTransport.php
File metadata and controls
272 lines (228 loc) · 9.1 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
namespace Krak\SymfonyMessengerRedis\Transport;
use Krak\SymfonyMessengerRedis\Stamp\{UniqueStamp, DebounceStamp};
use Redis;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Stamp\DelayStamp;
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;
use Symfony\Component\Messenger\Exception\{InvalidArgumentException, TransportException};
final class RedisTransport implements TransportInterface, MessageCountAwareInterface
{
private $serializer;
private $redis;
private $queue;
private $connectParams;
private $configureConnection;
private $isConnected;
public function __construct(
SerializerInterface $serializer,
Redis $redis,
string $queue,
array $connectParams = ['127.0.0.1', 6379],
callable $configureConnection = null
) {
$this->serializer = $serializer;
$this->redis = $redis;
$this->queue = $queue;
$this->connectParams = $connectParams;
$this->configureConnection = $configureConnection;
$this->isConnected = false;
}
public static function fromDsn(SerializerInterface $serializer, string $dsn, array $options = []) {
$parsedUrl = \parse_url($dsn);
if (!$parsedUrl) {
throw new InvalidArgumentException(sprintf('The given Redis DSN "%s" is invalid.', $dsn));
}
\parse_str($parsedUrl['query'] ?? '', $query);
if (($options['queue'] ?? $query['queue'] ?? null) === null) {
throw new InvalidArgumentException('The queue option must be included in the query parameters or configuration options.');
}
$host = $parsedUrl['host'] ?? '127.0.0.1';
if ($parsedUrl['scheme'] === 'rediss') {
$host = 'tls://'.$host;
}
return new self(
$serializer,
new Redis(),
$options['queue'] ?? $query['queue'],
[$host, intval($parsedUrl['port'] ?? 6379)],
function(Redis $conn) use ($query, $parsedUrl, $options) {
$auth = $options['password'] ?? $parsedUrl['pass'] ?? null;
if ($auth) {
$conn->auth($auth);
}
$db = $parsedUrl['path'] ?? $options['db'] ?? $query['db'] ?? null;
if ($db !== null) {
$db = intval(ltrim($db, '/'));
$conn->select($db);
}
}
);
}
/** @return Envelope[] */
public function get(): iterable {
$this->connect();
$this->redis->clearLastError();
$encodedMessage = $this->redis->eval($this->popLuaScript(), [
$this->getUniqueSetName(),
$this->getDelayedSetName(),
$this->queue,
$this->getProcessingQueue(),
round(microtime(true) * 1000),
], 4);
if ($this->redis->getLastError()) {
throw new TransportException('Failed to retrieve message from queue. Redis Error: ' . $this->redis->getLastError());
}
if (!$encodedMessage) {
return [];
}
$res = json_decode($encodedMessage, true);
$message = isset($res[0], $res[1]) ? ['body' => $res[0], 'headers' => $res[1]] : $res;
$envelope = $this->serializer->decode($message);
return [$envelope->with(new TransportMessageIdStamp($encodedMessage))];
}
public function ack(Envelope $env): void {
$this->connect();
$transportIdStamp = $env->last(TransportMessageIdStamp::class);
$this->clearMessageFromProcessingQueue($this->redis, $transportIdStamp ? $transportIdStamp->getId() : $this->encodeEnvelope($env));
}
public function reject(Envelope $env): void {
$this->connect();
$transportIdStamp = $env->last(TransportMessageIdStamp::class);
$this->clearMessageFromProcessingQueue($this->redis, $transportIdStamp ? $transportIdStamp->getId() : $this->encodeEnvelope($env));
}
public function send(Envelope $env): Envelope {
$this->connect();
[$message, $uniqueId] = $this->encodeEnvelopeWithUniqueId($env);
$res = $this->redis->eval($this->pushLuaScript(), [
$this->getUniqueSetName(),
$this->getDelayedSetName(),
$this->queue,
$uniqueId,
$this->getStampDelayTimestampMs($env),
$message,
$this->isDebounceStampExist($env) ? "1" : "0"
], 3);
if ($this->redis->getLastError()) {
throw new TransportException('Failed to push message onto queue. Redis Error: ' . $this->redis->getLastError());
}
return $env;
}
private function getStampDelayTimestampMs(Envelope $env): ?float {
/** @var DebounceStamp|DelayStamp|null $stamp */
$stamp = $env->last(DebounceStamp::class) ?: $env->last(DelayStamp::class);
if (!$stamp) {
return null;
}
return $stamp->getDelay() + (microtime(true) * 1000);
}
private function encodeEnvelope(Envelope $env): string {
return $this->encodeEnvelopeWithUniqueId($env)[0];
}
private function encodeEnvelopeWithUniqueId(Envelope $env): array {
$encoded = $this->serializer->encode($env);
/** @var DebounceStamp|UniqueStamp|null $stamp */
$stamp = $env->last(DebounceStamp::class)?: $env->last(UniqueStamp::class);
$uniqueId = $stamp ? strval($stamp->getId() ?: md5($encoded['body'])) : null;
return [\json_encode(array_merge($encoded, [
'uniqueId' => $uniqueId,
])), $uniqueId];
}
private function isDebounceStampExist(Envelope $env): bool
{
return \boolval($env->last(DebounceStamp::class));
}
public function getMessageCount(): int {
$this->connect();
$pipe = $this->redis->multi(Redis::PIPELINE);
$pipe->lLen($this->queue);
$pipe->zCount($this->getDelayedSetName(), '-inf', '+inf');
return array_sum(array_map('intval', $pipe->exec()));
}
private function connect(): void {
if ($this->isConnected) {
return;
}
$this->isConnected = true;
$this->redis->connect(...$this->connectParams);
if ($configureConnection = $this->configureConnection) {
$configureConnection($this->redis);
}
}
private function clearMessageFromProcessingQueue(Redis $redis, string $message) {
return $redis->lRem($this->getProcessingQueue(), $message, 1);
}
private function getProcessingQueue(): string {
return $this->queue . '_processing';
}
private function getUniqueSetName(): string {
return $this->queue . ':unique';
}
private function getDelayedSetName(): string {
return $this->queue . ':delayed';
}
/** return the lua script for pushing an item into the queue */
private function pushLuaScript(): string {
return <<<LUA
-- Push a message onto the queue and set a unique key in
-- a set if a unique id is present. If the unique id already
-- exists, do nothing
local uniqueSetKey = KEYS[1]
local delaySetKey = KEYS[2]
local queueKey = KEYS[3]
local uniqueId = ARGV[1]
local delayTimestampMs = ARGV[2]
local message = ARGV[3]
local isDebounceStampExist = ARGV[4]
-- do we have a unique id and is it apart of the unique set and isDebounceStampExist is true?
if isDebounceStampExist == "1" and uniqueId ~= "" and redis.call("SISMEMBER", uniqueSetKey, uniqueId) == 1 then
redis.call("ZREM", delaySetKey, message)
redis.call("ZADD", delaySetKey, delayTimestampMs, message)
return 3
end
-- do we have a unique id and is it apart of the unique set?
if uniqueId ~= "" and redis.call("SISMEMBER", uniqueSetKey, uniqueId) == 1 then
return 1
end
if uniqueId ~= "" then
redis.call("SADD", uniqueSetKey, uniqueId)
end
if delayTimestampMs ~= "" then
redis.call("ZADD", delaySetKey, delayTimestampMs, message)
return 2
end
redis.call("LPUSH", queueKey, message);
return 2
LUA;
}
/** return the lua script for popping items off of the queue */
private function popLuaScript() {
return <<<LUA
-- Pop a message from the queue into the processing queue and remove the unique id
-- from the unique set if it exists
local uniqueSetKey = KEYS[1]
local delayedSetKey = KEYS[2]
local queueKey = KEYS[3]
local processingQueueKey = KEYS[4]
local currentTimestampMs = ARGV[1]
-- first, we need to enqueue any delayed messages that are ready to be processed
local readyMessages = redis.call("ZRANGEBYSCORE", delayedSetKey, "-inf", currentTimestampMs)
for key,message in pairs(readyMessages) do
redis.call("LPUSH", queueKey, message)
redis.call("ZREM", delayedSetKey, message)
end
local message = redis.call("RPOPLPUSH", queueKey, processingQueueKey)
if message == false then
return false
end
local decodedMessage = cjson.decode(message)
if type(decodedMessage["uniqueId"]) == "string" then
redis.call("SREM", uniqueSetKey, decodedMessage["uniqueId"])
end
return message
LUA;
}
}