Browse Source

TCP_NODELAY for AsyncTcpConnection

walkor 9 years ago
parent
commit
1423377f41
1 changed files with 7 additions and 0 deletions
  1. 7 0
      Connection/AsyncTcpConnection.php

+ 7 - 0
Connection/AsyncTcpConnection.php

@@ -115,6 +115,13 @@ class AsyncTcpConnection extends TcpConnection
             Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
             Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
             // Nonblocking.
             // Nonblocking.
             stream_set_blocking($this->_socket, 0);
             stream_set_blocking($this->_socket, 0);
+            // Try to open keepalive for tcp and disable Nagle algorithm.
+            if(function_exists('socket_import_stream'))
+            {
+                $raw_socket = socket_import_stream($this->_socket);
+                socket_set_option($raw_socket, SOL_SOCKET, SO_KEEPALIVE, 1);
+                socket_set_option($raw_socket, SOL_TCP, TCP_NODELAY, 1);
+            }
             // Register a listener waiting read event.
             // Register a listener waiting read event.
             Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
             Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
             // There are some data waiting to send.
             // There are some data waiting to send.