소스 검색

TCP_KEEPIDLE TCP_KEEPINTVL TCP_KEEPCNT option

walkor 1 년 전
부모
커밋
b103862cd7
4개의 변경된 파일24개의 추가작업 그리고 10개의 파일을 삭제
  1. 8 3
      src/Connection/AsyncTcpConnection.php
  2. 6 0
      src/Connection/TcpConnection.php
  3. 0 7
      src/Protocols/Http/Request.php
  4. 10 0
      src/Worker.php

+ 8 - 3
src/Connection/AsyncTcpConnection.php

@@ -385,9 +385,14 @@ class AsyncTcpConnection extends TcpConnection
             }
             // Try to open keepalive for tcp and disable Nagle algorithm.
             if (function_exists('socket_import_stream') && $this->transport === 'tcp') {
-                $rawSocket = socket_import_stream($this->socket);
-                socket_set_option($rawSocket, SOL_SOCKET, SO_KEEPALIVE, 1);
-                socket_set_option($rawSocket, SOL_TCP, TCP_NODELAY, 1);
+                $socket = socket_import_stream($this->socket);
+                socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 1);
+                socket_set_option($socket, SOL_TCP, TCP_NODELAY, 1);
+                if (defined('TCP_KEEPIDLE')) {
+                    socket_set_option($socket, SOL_TCP, TCP_KEEPIDLE, static::TCP_KEEPALIVE_INTERVAL);
+                    socket_set_option($socket, SOL_TCP, TCP_KEEPINTVL, static::TCP_KEEPALIVE_INTERVAL);
+                    socket_set_option($socket, SOL_TCP, TCP_KEEPCNT, 1);
+                }
             }
             // SSL handshake.
             if ($this->transport === 'ssl') {

+ 6 - 0
src/Connection/TcpConnection.php

@@ -117,6 +117,11 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
     public const MAX_CACHE_SIZE = 512;
 
     /**
+     * Tcp keepalive interval.
+     */
+    public const TCP_KEEPALIVE_INTERVAL = 55;
+
+    /**
      * Emitted when socket connection is successfully established.
      *
      * @var ?callable
@@ -1060,6 +1065,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
             if ($this->worker) {
                 unset($this->worker->connections[$this->realId]);
             }
+            $this->worker = null;
             unset(static::$connections[$this->realId]);
         }
     }

+ 0 - 7
src/Protocols/Http/Request.php

@@ -60,13 +60,6 @@ class Request implements Stringable
     public ?TcpConnection $connection = null;
 
     /**
-     * Session instance.
-     *
-     * @var ?Session
-     */
-    public ?Session $session = null;
-
-    /**
      * @var int
      */
     public static int $maxFileUploads = 1024;

+ 10 - 0
src/Worker.php

@@ -555,6 +555,11 @@ class Worker
             $this->socketContext = stream_context_create($socketContext);
         }
 
+        // Set an empty onMessage callback.
+        $this->onMessage = function () {
+            // Empty.
+        };
+
     }
 
     /**
@@ -2346,6 +2351,11 @@ class Worker
                 $socket = socket_import_stream($this->mainSocket);
                 socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 1);
                 socket_set_option($socket, SOL_TCP, TCP_NODELAY, 1);
+                if (defined('TCP_KEEPIDLE')) {
+                    socket_set_option($socket, SOL_TCP, TCP_KEEPIDLE, TcpConnection::TCP_KEEPALIVE_INTERVAL);
+                    socket_set_option($socket, SOL_TCP, TCP_KEEPINTVL, TcpConnection::TCP_KEEPALIVE_INTERVAL);
+                    socket_set_option($socket, SOL_TCP, TCP_KEEPCNT, 1);
+                }
                 restore_error_handler();
             }