Browse Source

Support TCP_NODELAY for stream-based connections on PHP >= 5.4.0.

This cannot be implemented for previous versions of PHP because we
need socket_import_stream() to extract the underlying socket resource
from the stream in order to be able to set the TCP_NODELAY flag.
Daniele Alessandri 12 years ago
parent
commit
8d01be388d

+ 4 - 0
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)
 ===============================================================================

+ 5 - 0
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;
     }
 

+ 18 - 0
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
      */

+ 18 - 0
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
      */