-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Connection.php
709 lines (591 loc) · 26.9 KB
/
Connection.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Messenger\Bridge\Redis\Transport;
use Relay\Relay;
use Relay\Sentinel;
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
use Symfony\Component\Messenger\Exception\LogicException;
use Symfony\Component\Messenger\Exception\TransportException;
/**
* A Redis connection.
*
* @author Alexander Schranz <alexander@sulu.io>
* @author Antoine Bluchet <soyuka@gmail.com>
* @author Robin Chalas <robin.chalas@gmail.com>
*
* @internal
*
* @final
*/
class Connection
{
private const DEFAULT_OPTIONS = [
'host' => '127.0.0.1',
'port' => 6379,
'stream' => 'messages',
'group' => 'symfony',
'consumer' => 'consumer',
'auto_setup' => true,
'delete_after_ack' => true,
'delete_after_reject' => true,
'stream_max_entries' => 0, // any value higher than 0 defines an approximate maximum number of stream entries
'dbindex' => 0,
'redeliver_timeout' => 3600, // Timeout before redeliver messages still in pending state (seconds)
'claim_interval' => 60000, // Interval by which pending/abandoned messages should be checked
'lazy' => false,
'auth' => null,
'serializer' => 1, // see \Redis::SERIALIZER_PHP,
'sentinel_master' => null, // String, master to look for (optional, default is NULL meaning Sentinel support is disabled)
'redis_sentinel' => null, // String, alias for 'sentinel_master'
'timeout' => 0.0, // Float, value in seconds (optional, default is 0 meaning unlimited)
'read_timeout' => 0.0, // Float, value in seconds (optional, default is 0 meaning unlimited)
'retry_interval' => 0, // Int, value in milliseconds (optional, default is 0)
'persistent_id' => null, // String, persistent connection id (optional, default is NULL meaning not persistent)
'ssl' => null, // see https://php.net/context.ssl
];
private \Redis|Relay|\RedisCluster|\Closure $redis;
private string $stream;
private string $queue;
private string $group;
private string $consumer;
private bool $autoSetup;
private int $maxEntries;
private int $redeliverTimeout;
private float $nextClaim = 0.0;
private float $claimInterval;
private bool $deleteAfterAck;
private bool $deleteAfterReject;
private bool $couldHavePendingMessages = true;
public function __construct(array $options, \Redis|Relay|\RedisCluster|null $redis = null)
{
if (version_compare(phpversion('redis'), '4.3.0', '<')) {
throw new LogicException('The redis transport requires php-redis 4.3.0 or higher.');
}
$options += self::DEFAULT_OPTIONS;
$host = $options['host'];
$port = $options['port'];
$auth = $options['auth'];
if (isset($options['redis_sentinel']) && isset($options['sentinel_master'])) {
throw new InvalidArgumentException('Cannot use both "redis_sentinel" and "sentinel_master" at the same time.');
}
$sentinelMaster = $options['sentinel_master'] ?? $options['redis_sentinel'] ?? null;
if (null !== $sentinelMaster && !class_exists(\RedisSentinel::class) && !class_exists(Sentinel::class)) {
throw new InvalidArgumentException('Redis Sentinel support requires ext-redis>=5.2, or ext-relay.');
}
if (null !== $sentinelMaster && $redis instanceof \RedisCluster) {
throw new InvalidArgumentException('Cannot configure Redis Sentinel and Redis Cluster instance at the same time.');
}
$booleanStreamOptions = [
'allow_self_signed',
'capture_peer_cert',
'capture_peer_cert_chain',
'disable_compression',
'SNI_enabled',
'verify_peer',
'verify_peer_name',
];
foreach ($options['ssl'] ?? [] as $streamOption => $value) {
if (\in_array($streamOption, $booleanStreamOptions, true) && \is_string($value)) {
$options['ssl'][$streamOption] = filter_var($value, \FILTER_VALIDATE_BOOL);
}
}
if ((\is_array($host) && null === $sentinelMaster) || $redis instanceof \RedisCluster) {
$hosts = \is_string($host) ? [$host.':'.$port] : $host; // Always ensure we have an array
$this->redis = static fn () => self::initializeRedisCluster($redis, $hosts, $auth, $options);
} else {
$this->redis = static function () use ($redis, $sentinelMaster, $host, $port, $options, $auth) {
if (null !== $sentinelMaster) {
$sentinelClass = \extension_loaded('redis') ? \RedisSentinel::class : Sentinel::class;
$hostIndex = 0;
$hosts = \is_array($host) ? $host : [['scheme' => 'tcp', 'host' => $host, 'port' => $port]];
do {
$host = $hosts[$hostIndex]['host'];
$port = $hosts[$hostIndex]['port'] ?? 0;
$tls = 'tls' === $hosts[$hostIndex]['scheme'];
$address = false;
if (isset($hosts[$hostIndex]['host']) && $tls) {
$host = 'tls://'.$host;
}
try {
if (\extension_loaded('redis') && version_compare(phpversion('redis'), '6.0.0', '>=')) {
$params = [
'host' => $host,
'port' => $port,
'connectTimeout' => $options['timeout'],
'persistent' => $options['persistent_id'],
'retryInterval' => $options['retry_interval'],
'readTimeout' => $options['read_timeout'],
];
$sentinel = new \RedisSentinel($params);
} else {
$sentinel = new $sentinelClass($host, $port, $options['timeout'], $options['persistent_id'], $options['retry_interval'], $options['read_timeout']);
}
if ($address = $sentinel->getMasterAddrByName($sentinelMaster)) {
[$host, $port] = $address;
}
} catch (\RedisException|\Relay\Exception $redisException) {
}
} while (++$hostIndex < \count($hosts) && !$address);
if (!$address) {
throw new InvalidArgumentException(sprintf('Failed to retrieve master information from sentinel "%s".', $sentinelMaster), previous: $redisException ?? null);
}
}
return self::initializeRedis($redis ?? (\extension_loaded('redis') ? new \Redis() : new Relay()), $host, $port, $auth, $options);
};
}
if (!$options['lazy']) {
$this->getRedis();
}
foreach (['stream', 'group', 'consumer'] as $key) {
if ('' === $options[$key]) {
throw new InvalidArgumentException(sprintf('"%s" should be configured, got an empty string.', $key));
}
}
$this->stream = $options['stream'];
$this->group = $options['group'];
$this->consumer = $options['consumer'];
$this->queue = $this->stream.'__queue';
$this->autoSetup = $options['auto_setup'];
$this->maxEntries = $options['stream_max_entries'];
$this->deleteAfterAck = $options['delete_after_ack'];
$this->deleteAfterReject = $options['delete_after_reject'];
$this->redeliverTimeout = $options['redeliver_timeout'] * 1000;
$this->claimInterval = $options['claim_interval'] / 1000;
}
/**
* @param string|string[]|null $auth
*/
private static function initializeRedis(\Redis|Relay $redis, string $host, int $port, string|array|null $auth, array $params): \Redis|Relay
{
if ($redis->isConnected()) {
return $redis;
}
$connect = isset($params['persistent_id']) ? 'pconnect' : 'connect';
$redis->{$connect}($host, $port, $params['timeout'], $params['persistent_id'], $params['retry_interval'], $params['read_timeout'], ...(\defined('Redis::SCAN_PREFIX') || \extension_loaded('relay')) ? [['stream' => $params['ssl'] ?? null]] : []);
$redis->setOption($redis instanceof \Redis ? \Redis::OPT_SERIALIZER : Relay::OPT_SERIALIZER, $params['serializer']);
if (null !== $auth && !$redis->auth($auth)) {
throw new InvalidArgumentException('Redis connection failed: '.$redis->getLastError());
}
if (($params['dbindex'] ?? false) && !$redis->select($params['dbindex'])) {
throw new InvalidArgumentException('Redis connection failed: '.$redis->getLastError());
}
return $redis;
}
/**
* @param string|string[]|null $auth
*/
private static function initializeRedisCluster(?\RedisCluster $redis, array $hosts, string|array|null $auth, array $params): \RedisCluster
{
$redis ??= new \RedisCluster(null, $hosts, $params['timeout'], $params['read_timeout'], (bool) ($params['persistent'] ?? false), $auth, ...\defined('Redis::SCAN_PREFIX') ? [$params['ssl'] ?? null] : []);
$redis->setOption(\Redis::OPT_SERIALIZER, $params['serializer']);
return $redis;
}
public static function fromDsn(#[\SensitiveParameter] string $dsn, array $options = [], \Redis|Relay|\RedisCluster|null $redis = null): self
{
if (!str_contains($dsn, ',')) {
$params = self::parseDsn($dsn, $options);
if (isset($params['host']) && 'rediss' === $params['scheme']) {
$params['host'] = 'tls://'.$params['host'];
}
} else {
$dsns = explode(',', $dsn);
$paramss = array_map(function ($dsn) use (&$options) {
return self::parseDsn($dsn, $options);
}, $dsns);
// Merge all the URLs, the last one overrides the previous ones
$params = array_merge(...$paramss);
$tls = 'rediss' === $params['scheme'];
// Regroup all the hosts in an array interpretable by RedisCluster
$params['host'] = array_map(function ($params) use ($tls) {
if (!isset($params['host'])) {
throw new InvalidArgumentException('Missing host in DSN, it must be defined when using Redis Cluster.');
}
if ($tls) {
$params['host'] = 'tls://'.$params['host'];
}
return $params['host'].':'.($params['port'] ?? 6379);
}, $paramss, $dsns);
}
if ($invalidOptions = array_diff(array_keys($options), array_keys(self::DEFAULT_OPTIONS), ['host', 'port'])) {
throw new LogicException(sprintf('Invalid option(s) "%s" passed to the Redis Messenger transport.', implode('", "', $invalidOptions)));
}
foreach (self::DEFAULT_OPTIONS as $k => $v) {
$options[$k] = match (\gettype($v)) {
'integer' => filter_var($options[$k] ?? $v, \FILTER_VALIDATE_INT),
'boolean' => filter_var($options[$k] ?? $v, \FILTER_VALIDATE_BOOL),
'double' => filter_var($options[$k] ?? $v, \FILTER_VALIDATE_FLOAT),
default => $options[$k] ?? $v,
};
}
$pass = '' !== ($params['pass'] ?? '') ? rawurldecode($params['pass']) : null;
$user = '' !== ($params['user'] ?? '') ? rawurldecode($params['user']) : null;
$options['auth'] ??= null !== $pass && null !== $user ? [$user, $pass] : ($pass ?? $user);
if (isset($params['query'])) {
parse_str($params['query'], $query);
if (isset($query['host'])) {
$tls = 'rediss' === $params['scheme'];
$tcpScheme = $tls ? 'tls' : 'tcp';
if (!\is_array($hosts = $query['host'])) {
throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s".', $dsn));
}
foreach ($hosts as $host => $parameters) {
if (\is_string($parameters)) {
parse_str($parameters, $parameters);
}
if (false === $i = strrpos($host, ':')) {
$hosts[$host] = ['scheme' => $tcpScheme, 'host' => $host, 'port' => 6379] + $parameters;
} elseif ($port = (int) substr($host, 1 + $i)) {
$hosts[$host] = ['scheme' => $tcpScheme, 'host' => substr($host, 0, $i), 'port' => $port] + $parameters;
} else {
$hosts[$host] = ['scheme' => 'unix', 'host' => substr($host, 0, $i)] + $parameters;
}
}
$params['host'] = array_values($hosts);
}
}
if (isset($params['host'])) {
$options['host'] = $params['host'] ?? $options['host'];
$options['port'] = $params['port'] ?? $options['port'];
$pathParts = explode('/', rtrim($params['path'] ?? '', '/'));
$options['stream'] = $pathParts[1] ?? $options['stream'];
$options['group'] = $pathParts[2] ?? $options['group'];
$options['consumer'] = $pathParts[3] ?? $options['consumer'];
} else {
$options['host'] = $params['path'];
$options['port'] = 0;
}
return new self($options, $redis);
}
private static function parseDsn(string $dsn, array &$options): array
{
$url = $dsn;
$scheme = str_starts_with($dsn, 'rediss:') ? 'rediss' : 'redis';
if (preg_match('#^'.$scheme.':///([^:@])+$#', $dsn)) {
$url = str_replace($scheme.':', 'file:', $dsn);
}
$url = preg_replace_callback('#^'.$scheme.':(//)?(?:(?:(?<user>[^:@]*+):)?(?<password>[^@]*+)@)?#', function ($m) use (&$auth) {
if (isset($m['password'])) {
if (!\in_array($m['user'], ['', 'default'], true)) {
$auth['user'] = rawurldecode($m['user']);
}
$auth['pass'] = rawurldecode($m['password']);
}
return 'file:'.($m[1] ?? '');
}, $url);
if (false === $params = parse_url($url)) {
throw new InvalidArgumentException('The given Redis DSN is invalid.');
}
if (null !== $auth) {
unset($params['user']); // parse_url thinks //0@localhost/ is a username of "0"! doh!
$params += ($auth ?? []); // But don't worry as $auth array will have user, user/pass or pass as needed
}
if (isset($params['query'])) {
parse_str($params['query'], $dsnOptions);
$options = array_merge($options, $dsnOptions);
}
$params['scheme'] = $scheme;
return $params;
}
private function claimOldPendingMessages(): void
{
try {
// This could soon be optimized with https://github.com/antirez/redis/issues/5212 or
// https://github.com/antirez/redis/issues/6256
$pendingMessages = $this->getRedis()->xpending($this->stream, $this->group, '-', '+', 1) ?: [];
} catch (\RedisException|\Relay\Exception $e) {
throw new TransportException($e->getMessage(), 0, $e);
}
$claimableIds = [];
foreach ($pendingMessages as $pendingMessage) {
if ($pendingMessage[1] === $this->consumer) {
$this->couldHavePendingMessages = true;
return;
}
if ($pendingMessage[2] >= $this->redeliverTimeout) {
$claimableIds[] = $pendingMessage[0];
}
}
if (\count($claimableIds) > 0) {
try {
$this->getRedis()->xclaim(
$this->stream,
$this->group,
$this->consumer,
$this->redeliverTimeout,
$claimableIds,
['JUSTID']
);
$this->couldHavePendingMessages = true;
} catch (\RedisException|\Relay\Exception $e) {
throw new TransportException($e->getMessage(), 0, $e);
}
}
$this->nextClaim = microtime(true) + $this->claimInterval;
}
public function get(): ?array
{
if ($this->autoSetup) {
$this->setup();
}
$now = microtime();
$now = substr($now, 11).substr($now, 2, 3);
$queuedMessageCount = $this->rawCommand('ZCOUNT', 0, $now) ?? 0;
while ($queuedMessageCount--) {
if (!$message = $this->rawCommand('ZPOPMIN', 1)) {
break;
}
[$queuedMessage, $expiry] = $message;
if (\strlen($expiry) === \strlen($now) ? $expiry > $now : \strlen($expiry) < \strlen($now)) {
// if a future-placed message is popped because of a race condition with
// another running consumer, the message is readded to the queue
if (!$this->rawCommand('ZADD', 'NX', $expiry, $queuedMessage)) {
throw new TransportException('Could not add a message to the redis stream.');
}
break;
}
$decodedQueuedMessage = json_decode($queuedMessage, true);
$this->add(\array_key_exists('body', $decodedQueuedMessage) ? $decodedQueuedMessage['body'] : $queuedMessage, $decodedQueuedMessage['headers'] ?? [], 0);
}
if (!$this->couldHavePendingMessages && $this->nextClaim <= microtime(true)) {
$this->claimOldPendingMessages();
}
$messageId = '>'; // will receive new messages
if ($this->couldHavePendingMessages) {
$messageId = '0'; // will receive consumers pending messages
}
$redis = $this->getRedis();
try {
$messages = $redis->xreadgroup(
$this->group,
$this->consumer,
[$this->stream => $messageId],
1,
1
);
} catch (\RedisException|\Relay\Exception $e) {
throw new TransportException($e->getMessage(), 0, $e);
}
if (false === $messages) {
if ($error = $redis->getLastError() ?: null) {
$redis->clearLastError();
}
throw new TransportException($error ?? 'Could not read messages from the redis stream.');
}
if ($this->couldHavePendingMessages && empty($messages[$this->stream])) {
$this->couldHavePendingMessages = false;
// No pending messages so get a new one
return $this->get();
}
foreach ($messages[$this->stream] ?? [] as $key => $message) {
return [
'id' => $key,
'data' => $message,
];
}
return null;
}
public function ack(string $id): void
{
$redis = $this->getRedis();
try {
$acknowledged = $redis->xack($this->stream, $this->group, [$id]);
if ($this->deleteAfterAck) {
$acknowledged = $redis->xdel($this->stream, [$id]);
}
} catch (\RedisException|\Relay\Exception $e) {
throw new TransportException($e->getMessage(), 0, $e);
}
if (!$acknowledged) {
if ($error = $redis->getLastError() ?: null) {
$redis->clearLastError();
}
throw new TransportException($error ?? sprintf('Could not acknowledge redis message "%s".', $id));
}
}
public function reject(string $id): void
{
$redis = $this->getRedis();
try {
$deleted = $redis->xack($this->stream, $this->group, [$id]);
if ($this->deleteAfterReject) {
$deleted = $redis->xdel($this->stream, [$id]) && $deleted;
}
} catch (\RedisException|\Relay\Exception $e) {
throw new TransportException($e->getMessage(), 0, $e);
}
if (!$deleted) {
if ($error = $redis->getLastError() ?: null) {
$redis->clearLastError();
}
throw new TransportException($error ?? sprintf('Could not delete message "%s" from the redis stream.', $id));
}
}
public function add(string $body, array $headers, int $delayInMs = 0): string
{
if ($this->autoSetup) {
$this->setup();
}
$redis = $this->getRedis();
try {
if ($delayInMs > 0) { // the delay is <= 0 for queued messages
$id = uniqid('', true);
$message = json_encode([
'body' => $body,
'headers' => $headers,
// Entry need to be unique in the sorted set else it would only be added once to the delayed messages queue
'uniqid' => $id,
]);
if (false === $message) {
throw new TransportException(json_last_error_msg());
}
$now = explode(' ', microtime(), 2);
$now[0] = str_pad($delayInMs + substr($now[0], 2, 3), 3, '0', \STR_PAD_LEFT);
if (3 < \strlen($now[0])) {
$now[1] += substr($now[0], 0, -3);
$now[0] = substr($now[0], -3);
if (\is_float($now[1])) {
throw new TransportException("Message delay is too big: {$delayInMs}ms.");
}
}
$added = $this->rawCommand('ZADD', 'NX', $now[1].$now[0], $message);
} else {
$message = json_encode([
'body' => $body,
'headers' => $headers,
]);
if (false === $message) {
throw new TransportException(json_last_error_msg());
}
if ($this->maxEntries) {
$added = $redis->xadd($this->stream, '*', ['message' => $message], $this->maxEntries, true);
} else {
$added = $redis->xadd($this->stream, '*', ['message' => $message]);
}
$id = $added;
}
} catch (\RedisException|\Relay\Exception $e) {
if ($error = $redis->getLastError() ?: null) {
$redis->clearLastError();
}
throw new TransportException($error ?? $e->getMessage(), 0, $e);
}
if (!$added) {
if ($error = $redis->getLastError() ?: null) {
$redis->clearLastError();
}
throw new TransportException($error ?? 'Could not add a message to the redis stream.');
}
return $id;
}
public function setup(): void
{
$redis = $this->getRedis();
try {
$redis->xgroup('CREATE', $this->stream, $this->group, 0, true);
} catch (\RedisException|\Relay\Exception $e) {
throw new TransportException($e->getMessage(), 0, $e);
}
// group might already exist, ignore
if ($redis->getLastError()) {
$redis->clearLastError();
}
if ($this->deleteAfterAck || $this->deleteAfterReject) {
$groups = $redis->xinfo('GROUPS', $this->stream);
if (
// support for Redis extension version 5+
(\is_array($groups) && 1 < \count($groups))
// support for Redis extension version 4.x
|| (\is_string($groups) && substr_count($groups, '"name"'))
) {
throw new LogicException(sprintf('More than one group exists for stream "%s", delete_after_ack and delete_after_reject cannot be enabled as it risks deleting messages before all groups could consume them.', $this->stream));
}
}
$this->autoSetup = false;
}
public function cleanup(): void
{
static $unlink = true;
$redis = $this->getRedis();
if ($unlink) {
try {
$unlink = false !== $redis->unlink($this->stream, $this->queue);
} catch (\Throwable) {
$unlink = false;
}
}
if (!$unlink) {
$redis->del($this->stream, $this->queue);
}
}
public function getMessageCount(): int
{
$redis = $this->getRedis();
$groups = $redis->xinfo('GROUPS', $this->stream) ?: [];
$lastDeliveredId = null;
foreach ($groups as $group) {
if ($group['name'] !== $this->group) {
continue;
}
// Use "lag" key provided by Redis 7.x. See https://redis.io/commands/xinfo-groups/#consumer-group-lag.
if (isset($group['lag'])) {
return $group['lag'];
}
if (!isset($group['last-delivered-id'])) {
return 0;
}
$lastDeliveredId = $group['last-delivered-id'];
break;
}
if (null === $lastDeliveredId) {
return 0;
}
// Iterate through the stream. See https://redis.io/commands/xrange/#iterating-a-stream.
$useExclusiveRangeInterval = version_compare(phpversion('redis'), '6.2.0', '>=');
$total = 0;
while (true) {
if (!$range = $redis->xRange($this->stream, $lastDeliveredId, '+', 100)) {
return $total;
}
$total += \count($range);
if ($useExclusiveRangeInterval) {
$lastDeliveredId = preg_replace_callback('#\d+$#', static fn (array $matches) => (int) $matches[0] + 1, array_key_last($range));
} else {
$lastDeliveredId = '('.array_key_last($range);
}
}
}
private function rawCommand(string $command, ...$arguments): mixed
{
$redis = $this->getRedis();
try {
if ($redis instanceof \RedisCluster) {
$result = $redis->rawCommand($this->queue, $command, $this->queue, ...$arguments);
} else {
$result = $redis->rawCommand($command, $this->queue, ...$arguments);
}
} catch (\RedisException|\Relay\Exception $e) {
throw new TransportException($e->getMessage(), 0, $e);
}
if (false === $result) {
if ($error = $redis->getLastError() ?: null) {
$redis->clearLastError();
}
throw new TransportException($error ?? sprintf('Could not run "%s" on Redis queue.', $command));
}
return $result;
}
private function getRedis(): \Redis|Relay|\RedisCluster
{
if ($this->redis instanceof \Closure) {
$this->redis = ($this->redis)();
}
return $this->redis;
}
}