Skip to content

Commit c853ac9

Browse files
committed
[Messenger] Allow to configure the db index on Redis transport
1 parent 8f92594 commit c853ac9

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* `InMemoryTransport` handle acknowledged and rejected messages.
1111
* Made all dispatched worker event classes final.
1212
* Added support for `from_transport` attribute on `messenger.message_handler` tag.
13+
* Added support for passing `dbindex` as a query parameter to the redis transport DSN.
1314

1415
4.3.0
1516
-----

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ public function testAuth()
104104
Connection::fromDsn('redis://password@localhost/queue', [], $redis);
105105
}
106106

107+
public function testDbIndex()
108+
{
109+
$redis = new \Redis();
110+
111+
Connection::fromDsn('redis://password@localhost/queue?dbindex=2', [], $redis);
112+
113+
$this->assertSame(2, $redis->getDbNum());
114+
}
115+
107116
public function testFirstGetPendingMessagesThenNewMessages()
108117
{
109118
$redis = $this->getMockBuilder(\Redis::class)->disableOriginalConstructor()->getMock();

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ class Connection
3131
'group' => 'symfony',
3232
'consumer' => 'consumer',
3333
'auto_setup' => true,
34-
'stream_max_entries' => 0, // any value higher than 0 defines an approximate maximum number of stream entries
34+
'stream_max_entries' => 0, // any value higher than 0 defines an approximate maximum number of stream entries,
35+
'dbindex' => 0,
3536
];
3637

3738
private $connection;
@@ -56,6 +57,10 @@ public function __construct(array $configuration, array $connectionCredentials =
5657
$this->connection->auth($connectionCredentials['auth']);
5758
}
5859

60+
if (($dbIndex = $configuration['dbindex'] ?? self::DEFAULT_OPTIONS['dbindex']) && !$this->connection->select($dbIndex)) {
61+
throw new InvalidArgumentException(sprintf('Redis connection failed: %s', $redis->getLastError()));
62+
}
63+
5964
$this->stream = $configuration['stream'] ?? self::DEFAULT_OPTIONS['stream'];
6065
$this->group = $configuration['group'] ?? self::DEFAULT_OPTIONS['group'];
6166
$this->consumer = $configuration['consumer'] ?? self::DEFAULT_OPTIONS['consumer'];
@@ -97,12 +102,19 @@ public static function fromDsn(string $dsn, array $redisOptions = [], \Redis $re
97102
unset($redisOptions['stream_max_entries']);
98103
}
99104

105+
$dbIndex = null;
106+
if (\array_key_exists('dbindex', $redisOptions)) {
107+
$dbIndex = filter_var($redisOptions['dbindex'], FILTER_VALIDATE_INT);
108+
unset($redisOptions['dbindex']);
109+
}
110+
100111
return new self([
101112
'stream' => $stream,
102113
'group' => $group,
103114
'consumer' => $consumer,
104115
'auto_setup' => $autoSetup,
105116
'stream_max_entries' => $maxEntries,
117+
'dbindex' => $dbIndex,
106118
], $connectionCredentials, $redisOptions, $redis);
107119
}
108120

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy