diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cc045ce..efb94a8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ v0.8.3 (2013-xx-xx) returning `Predis\Connection\ConnectionInterface`. Users can create their own self-contained strategies to create and set up the underlying connection. +- Added support for the TCP_NODELAY flag via the `tcp_nodelay` parameter for + for stream-based connections, namely `Predis\Connection\StreamConnection` and + `Predis\Connection\PhpiredisStreamConnection` (__requires PHP >= 5.4.0__). + v0.8.2 (2013-02-03) =============================================================================== diff --git a/lib/Predis/Connection/StreamConnection.php b/lib/Predis/Connection/StreamConnection.php index aea63d44..580c7204 100644 --- a/lib/Predis/Connection/StreamConnection.php +++ b/lib/Predis/Connection/StreamConnection.php @@ -101,6 +101,11 @@ class StreamConnection extends AbstractConnection stream_set_timeout($resource, $timeoutSeconds, $timeoutUSeconds); } + if (isset($parameters->tcp_nodelay) && version_compare(PHP_VERSION, '5.4.0', '>=')) { + $socket = socket_import_stream($resource); + socket_set_option($socket, SOL_TCP, TCP_NODELAY, (int) $parameters->tcp_nodelay); + } + return $resource; } diff --git a/tests/Predis/Connection/PhpiredisStreamConnectionTest.php b/tests/Predis/Connection/PhpiredisStreamConnectionTest.php index 313dd990..11701397 100644 --- a/tests/Predis/Connection/PhpiredisStreamConnectionTest.php +++ b/tests/Predis/Connection/PhpiredisStreamConnectionTest.php @@ -70,6 +70,24 @@ class PhpiredisStreamConnectionTest extends ConnectionTestCase // ---- INTEGRATION TESTS --------------------------------------------- // // ******************************************************************** // + /** + * @group connected + */ + public function testAcceptsTcpNodelayParameter() + { + if (!version_compare(PHP_VERSION, '5.4.0', '>=')) { + $this->markTestSkipped('Setting TCP_NODELAY on PHP socket streams works on PHP >= 5.4.0'); + } + + $connection = new PhpiredisStreamConnection($this->getParameters(array('tcp_nodelay' => false))); + $connection->connect(); + $this->assertTrue($connection->isConnected()); + + $connection = new PhpiredisStreamConnection($this->getParameters(array('tcp_nodelay' => true))); + $connection->connect(); + $this->assertTrue($connection->isConnected()); + } + /** * @group connected */ diff --git a/tests/Predis/Connection/StreamConnectionTest.php b/tests/Predis/Connection/StreamConnectionTest.php index a080a789..251dc3c3 100644 --- a/tests/Predis/Connection/StreamConnectionTest.php +++ b/tests/Predis/Connection/StreamConnectionTest.php @@ -69,6 +69,24 @@ class StreamConnectionTest extends ConnectionTestCase // ---- INTEGRATION TESTS --------------------------------------------- // // ******************************************************************** // + /** + * @group connected + */ + public function testAcceptsTcpNodelayParameter() + { + if (!version_compare(PHP_VERSION, '5.4.0', '>=')) { + $this->markTestSkipped('Setting TCP_NODELAY on PHP socket streams works on PHP >= 5.4.0'); + } + + $connection = new StreamConnection($this->getParameters(array('tcp_nodelay' => false))); + $connection->connect(); + $this->assertTrue($connection->isConnected()); + + $connection = new StreamConnection($this->getParameters(array('tcp_nodelay' => true))); + $connection->connect(); + $this->assertTrue($connection->isConnected()); + } + /** * @group connected */