Skip to content

Commit ce7c0b4

Browse files
feature #33856 [Messenger] Allow to configure the db index on Redis transport (chalasr)
This PR was merged into the 4.4 branch. Discussion ---------- [Messenger] Allow to configure the db index on Redis transport | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #33724 | License | MIT | Doc PR | todo Quite useful for testing. Commits ------- 115a9bb [Messenger] Allow to configure the db index on Redis transport
2 parents 38ec2f3 + 115a9bb commit ce7c0b4

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Connection
3232
'consumer' => 'consumer',
3333
'auto_setup' => true,
3434
'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)