Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7fe06bc

Browse files
Toflarsroze
authored andcommitted
[Messenger] Added support for auto trimming of redis streams
1 parent c8f3cef commit 7fe06bc

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Deprecated passing a `ContainerInterface` instance as first argument of the `ConsumeMessagesCommand` constructor,
88
pass a `RoutableMessageBus` instance instead.
9+
* Added support for auto trimming of Redis streams.
910

1011
4.3.0
1112
-----

src/Symfony/Component/Messenger/Tests/Transport/RedisExt/ConnectionTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ public function testFromDsn()
4242
public function testFromDsnWithOptions()
4343
{
4444
$this->assertEquals(
45-
new Connection(['stream' => 'queue', 'group' => 'group1', 'consumer' => 'consumer1', 'auto_setup' => false], [
45+
new Connection(['stream' => 'queue', 'group' => 'group1', 'consumer' => 'consumer1', 'auto_setup' => false, 'stream_max_entries' => 20000], [
4646
'host' => 'localhost',
4747
'port' => 6379,
4848
], [
4949
'serializer' => 2,
5050
]),
51-
Connection::fromDsn('redis://localhost/queue/group1/consumer1', ['serializer' => 2, 'auto_setup' => false])
51+
Connection::fromDsn('redis://localhost/queue/group1/consumer1', ['serializer' => 2, 'auto_setup' => false, 'stream_max_entries' => 20000])
5252
);
5353
}
5454

@@ -142,4 +142,16 @@ public function testGetNonBlocking()
142142
$connection->reject($message['id']);
143143
$redis->del('messenger-getnonblocking');
144144
}
145+
146+
public function testMaxEntries()
147+
{
148+
$redis = $this->getMockBuilder(\Redis::class)->disableOriginalConstructor()->getMock();
149+
150+
$redis->expects($this->exactly(1))->method('xadd')
151+
->with('queue', '*', ['message' => '{"body":"1","headers":[]}'], 20000, true)
152+
->willReturn(1);
153+
154+
$connection = Connection::fromDsn('redis://localhost/queue?stream_max_entries=20000', [], $redis); // 1 = always
155+
$connection->add('1', []);
156+
}
145157
}

src/Symfony/Component/Messenger/Transport/RedisExt/Connection.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ class Connection
3232
'group' => 'symfony',
3333
'consumer' => 'consumer',
3434
'auto_setup' => true,
35+
'stream_max_entries' => 0, // any value higher than 0 defines an approximate maximum number of stream entries
3536
];
3637

3738
private $connection;
3839
private $stream;
3940
private $group;
4041
private $consumer;
4142
private $autoSetup;
43+
private $maxEntries;
4244
private $couldHavePendingMessages = true;
4345

4446
public function __construct(array $configuration, array $connectionCredentials = [], array $redisOptions = [], \Redis $redis = null)
@@ -50,6 +52,7 @@ public function __construct(array $configuration, array $connectionCredentials =
5052
$this->group = $configuration['group'] ?? self::DEFAULT_OPTIONS['group'];
5153
$this->consumer = $configuration['consumer'] ?? self::DEFAULT_OPTIONS['consumer'];
5254
$this->autoSetup = $configuration['auto_setup'] ?? self::DEFAULT_OPTIONS['auto_setup'];
55+
$this->maxEntries = $configuration['stream_max_entries'] ?? self::DEFAULT_OPTIONS['stream_max_entries'];
5356
}
5457

5558
public static function fromDsn(string $dsn, array $redisOptions = [], \Redis $redis = null): self
@@ -79,7 +82,19 @@ public static function fromDsn(string $dsn, array $redisOptions = [], \Redis $re
7982
unset($redisOptions['auto_setup']);
8083
}
8184

82-
return new self(['stream' => $stream, 'group' => $group, 'consumer' => $consumer, 'auto_setup' => $autoSetup], $connectionCredentials, $redisOptions, $redis);
85+
$maxEntries = null;
86+
if (\array_key_exists('stream_max_entries', $redisOptions)) {
87+
$maxEntries = filter_var($redisOptions['stream_max_entries'], FILTER_VALIDATE_INT);
88+
unset($redisOptions['stream_max_entries']);
89+
}
90+
91+
return new self([
92+
'stream' => $stream,
93+
'group' => $group,
94+
'consumer' => $consumer,
95+
'auto_setup' => $autoSetup,
96+
'stream_max_entries' => $maxEntries,
97+
], $connectionCredentials, $redisOptions, $redis);
8398
}
8499

85100
public function get(): ?array
@@ -166,9 +181,15 @@ public function add(string $body, array $headers): void
166181

167182
$e = null;
168183
try {
169-
$added = $this->connection->xadd($this->stream, '*', ['message' => json_encode(
170-
['body' => $body, 'headers' => $headers]
171-
)]);
184+
if ($this->maxEntries) {
185+
$added = $this->connection->xadd($this->stream, '*', ['message' => json_encode(
186+
['body' => $body, 'headers' => $headers]
187+
)], $this->maxEntries, true);
188+
} else {
189+
$added = $this->connection->xadd($this->stream, '*', ['message' => json_encode(
190+
['body' => $body, 'headers' => $headers]
191+
)]);
192+
}
172193
} catch (\RedisException $e) {
173194
}
174195

0 commit comments

Comments
 (0)