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
-------

115a9bbeda [Messenger] Allow to configure the db index on Redis transport
This commit is contained in:
Nicolas Grekas 2019-10-07 13:15:42 +02:00
commit ce7c0b4590
3 changed files with 22 additions and 0 deletions

View File

@ -10,6 +10,7 @@ CHANGELOG
* `InMemoryTransport` handle acknowledged and rejected messages.
* Made all dispatched worker event classes final.
* Added support for `from_transport` attribute on `messenger.message_handler` tag.
* Added support for passing `dbindex` as a query parameter to the redis transport DSN.
4.3.0
-----

View File

@ -104,6 +104,15 @@ class ConnectionTest extends TestCase
Connection::fromDsn('redis://password@localhost/queue', [], $redis);
}
public function testDbIndex()
{
$redis = new \Redis();
Connection::fromDsn('redis://password@localhost/queue?dbindex=2', [], $redis);
$this->assertSame(2, $redis->getDbNum());
}
public function testFirstGetPendingMessagesThenNewMessages()
{
$redis = $this->getMockBuilder(\Redis::class)->disableOriginalConstructor()->getMock();

View File

@ -32,6 +32,7 @@ class Connection
'consumer' => 'consumer',
'auto_setup' => true,
'stream_max_entries' => 0, // any value higher than 0 defines an approximate maximum number of stream entries
'dbindex' => 0,
];
private $connection;
@ -56,6 +57,10 @@ class Connection
$this->connection->auth($connectionCredentials['auth']);
}
if (($dbIndex = $configuration['dbindex'] ?? self::DEFAULT_OPTIONS['dbindex']) && !$this->connection->select($dbIndex)) {
throw new InvalidArgumentException(sprintf('Redis connection failed: %s', $redis->getLastError()));
}
$this->stream = $configuration['stream'] ?? self::DEFAULT_OPTIONS['stream'];
$this->group = $configuration['group'] ?? self::DEFAULT_OPTIONS['group'];
$this->consumer = $configuration['consumer'] ?? self::DEFAULT_OPTIONS['consumer'];
@ -97,12 +102,19 @@ class Connection
unset($redisOptions['stream_max_entries']);
}
$dbIndex = null;
if (\array_key_exists('dbindex', $redisOptions)) {
$dbIndex = filter_var($redisOptions['dbindex'], FILTER_VALIDATE_INT);
unset($redisOptions['dbindex']);
}
return new self([
'stream' => $stream,
'group' => $group,
'consumer' => $consumer,
'auto_setup' => $autoSetup,
'stream_max_entries' => $maxEntries,
'dbindex' => $dbIndex,
], $connectionCredentials, $redisOptions, $redis);
}