From cbf394b8820d70367cf71c1fb8f34b802a726209 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Till=20Kr=C3=BCss?= Date: Wed, 13 Sep 2023 09:35:49 -0700 Subject: [PATCH] Disable `CLIENT SETINFO` calls by default (#1399) Add `client_info` parameter --- src/Connection/Factory.php | 2 +- src/Connection/ParametersInterface.php | 1 + tests/Predis/ClientTest.php | 17 ++++++++++- tests/Predis/Connection/FactoryTest.php | 38 +++++++------------------ 4 files changed, 28 insertions(+), 30 deletions(-) diff --git a/src/Connection/Factory.php b/src/Connection/Factory.php index c78c6d40..86b18c4a 100644 --- a/src/Connection/Factory.php +++ b/src/Connection/Factory.php @@ -175,7 +175,7 @@ class Factory implements FactoryInterface ); } - if (!$connection instanceof RelayConnection) { + if ($parameters->client_info ?? false && !$connection instanceof RelayConnection) { $connection->addConnectCommand( new RawCommand('CLIENT', ['SETINFO', 'LIB-NAME', 'predis']) ); diff --git a/src/Connection/ParametersInterface.php b/src/Connection/ParametersInterface.php index 37ed97f7..7893ea11 100644 --- a/src/Connection/ParametersInterface.php +++ b/src/Connection/ParametersInterface.php @@ -31,6 +31,7 @@ namespace Predis\Connection; * @property string $database Database index (see the SELECT command). * @property bool $async_connect Performs the connect() operation asynchronously. * @property bool $tcp_nodelay Toggles the Nagle's algorithm for coalescing. + * @property bool $client_info Whether to set LIB-NAME and LIB-VER when connecting. * @property bool $cache (Relay only) Whether to use in-memory caching. * @property string $serializer (Relay only) Serializer used for data serialization. * @property string $compression (Relay only) Algorithm used for data compression. diff --git a/tests/Predis/ClientTest.php b/tests/Predis/ClientTest.php index 01905834..4489b2f0 100644 --- a/tests/Predis/ClientTest.php +++ b/tests/Predis/ClientTest.php @@ -1258,12 +1258,27 @@ class ClientTest extends PredisTestCase * @group relay-incompatible * @requiresRedisVersion >= 7.2.0 */ - public function testSetClientInfoOnConnection(): void + public function testDoNoSetClientInfoOnConnection(): void { $client = new Client($this->getParameters()); $libName = $client->client('LIST')[0]['lib-name']; $libVer = $client->client('LIST')[0]['lib-ver']; + $this->assertEmpty($libName); + $this->assertEmpty($libVer); + } + + /** + * @group connected + * @group relay-incompatible + * @requiresRedisVersion >= 7.2.0 + */ + public function testSetClientInfoOnConnectionWhenEnabled(): void + { + $client = new Client($this->getParameters(['client_info' => true])); + $libName = $client->client('LIST')[0]['lib-name']; + $libVer = $client->client('LIST')[0]['lib-ver']; + $this->assertSame('predis', $libName); $this->assertSame(Client::VERSION, $libVer); } diff --git a/tests/Predis/Connection/FactoryTest.php b/tests/Predis/Connection/FactoryTest.php index 5e64497b..9d48bd53 100644 --- a/tests/Predis/Connection/FactoryTest.php +++ b/tests/Predis/Connection/FactoryTest.php @@ -304,12 +304,10 @@ class FactoryTest extends PredisTestCase ->method('getParameters') ->willReturn($parameters); $connection - ->expects($this->exactly(4)) + ->expects($this->exactly(2)) ->method('addConnectCommand') ->withConsecutive( [$this->isRedisCommand('AUTH', ['foobar'])], - [$this->isRedisCommand('CLIENT', ['SETINFO', 'LIB-NAME', 'predis'])], - [$this->isRedisCommand('CLIENT', ['SETINFO', 'LIB-VER', Client::VERSION])], [$this->isRedisCommand('SELECT', ['0'])] ); @@ -335,13 +333,9 @@ class FactoryTest extends PredisTestCase $connection->expects($this->once()) ->method('getParameters') ->will($this->returnValue($parameters)); - $connection->expects($this->exactly(3)) + $connection->expects($this->once()) ->method('addConnectCommand') - ->withConsecutive( - [$this->isRedisCommand('AUTH', ['foobar'])], - [$this->isRedisCommand('CLIENT', ['SETINFO', 'LIB-NAME', 'predis'])], - [$this->isRedisCommand('CLIENT', ['SETINFO', 'LIB-VER', Client::VERSION])] - ); + ->with($this->isRedisCommand('AUTH', ['foobar'])); $factory = new Factory(); @@ -366,13 +360,9 @@ class FactoryTest extends PredisTestCase $connection->expects($this->once()) ->method('getParameters') ->will($this->returnValue($parameters)); - $connection->expects($this->exactly(3)) + $connection->expects($this->once()) ->method('addConnectCommand') - ->withConsecutive( - [$this->isRedisCommand('AUTH', ['myusername', 'foobar'])], - [$this->isRedisCommand('CLIENT', ['SETINFO', 'LIB-NAME', 'predis'])], - [$this->isRedisCommand('CLIENT', ['SETINFO', 'LIB-VER', Client::VERSION])] - ); + ->with($this->isRedisCommand('AUTH', ['myusername', 'foobar'])); $factory = new Factory(); @@ -396,12 +386,8 @@ class FactoryTest extends PredisTestCase $connection->expects($this->once()) ->method('getParameters') ->will($this->returnValue($parameters)); - $connection->expects($this->exactly(2)) - ->method('addConnectCommand') - ->withConsecutive( - [$this->isRedisCommand('CLIENT', ['SETINFO', 'LIB-NAME', 'predis'])], - [$this->isRedisCommand('CLIENT', ['SETINFO', 'LIB-VER', Client::VERSION])] - ); + $connection->expects($this->never()) + ->method('addConnectCommand'); $factory = new Factory(); @@ -426,12 +412,8 @@ class FactoryTest extends PredisTestCase $connection->expects($this->once()) ->method('getParameters') ->will($this->returnValue($parameters)); - $connection->expects($this->exactly(2)) - ->method('addConnectCommand') - ->withConsecutive( - [$this->isRedisCommand('CLIENT', ['SETINFO', 'LIB-NAME', 'predis'])], - [$this->isRedisCommand('CLIENT', ['SETINFO', 'LIB-VER', Client::VERSION])] - ); + $connection->expects($this->never()) + ->method('addConnectCommand'); $factory = new Factory(); @@ -564,7 +546,7 @@ class FactoryTest extends PredisTestCase */ public function testSetClientNameAndVersionOnConnection(): void { - $parameters = []; + $parameters = ['client_info' => true]; $factory = new Factory(); $connection = $factory->create($parameters);