소스 검색

gateway ping optimization && add TcpConnection defaultMaxSendBufferSize

walkor 10 년 전
부모
커밋
442eceeb31
5개의 변경된 파일37개의 추가작업 그리고 13개의 파일을 삭제
  1. 6 0
      GatewayWorker/BusinessWorker.php
  2. 14 5
      GatewayWorker/Gateway.php
  3. 1 0
      Workerman/Connection/AsyncTcpConnection.php
  4. 15 7
      Workerman/Connection/TcpConnection.php
  5. 1 1
      Workerman/Worker.php

+ 6 - 0
GatewayWorker/BusinessWorker.php

@@ -1,6 +1,8 @@
 <?php
 namespace GatewayWorker;
 
+use Workerman\Connection\TcpConnection;
+
 use \Workerman\Worker;
 use \Workerman\Connection\AsyncTcpConnection;
 use \Workerman\Protocols\GatewayProtocol;
@@ -185,6 +187,10 @@ class BusinessWorker extends Worker
                 $gateway_connection->onMessage = array($this, 'onGatewayMessage');
                 $gateway_connection->onClose = array($this, 'onClose');
                 $gateway_connection->onError = array($this, 'onError');
+                if(TcpConnection::$defaultMaxSendBufferSize == $gateway_connection->maxSendBufferSize)
+                {
+                    $gateway_connection->maxSendBufferSize = 10*1024*1024;
+                }
                 $gateway_connection->connect();
                 $this->_connectingGatewayAddress[$addr] = 1;
             }

+ 14 - 5
GatewayWorker/Gateway.php

@@ -1,6 +1,8 @@
 <?php 
 namespace GatewayWorker;
 
+use Workerman\Connection\TcpConnection;
+
 use \Workerman\Worker;
 use \Workerman\Lib\Timer;
 use \Workerman\Protocols\GatewayProtocol;
@@ -171,7 +173,7 @@ class Gateway extends Worker
      */
     public function onClientMessage($connection, $data)
     {
-        $connection->pingNotResponseCount = 0;
+        $connection->pingNotResponseCount = -1;
         $this->sendToWorker(GatewayProtocol::CMD_ON_MESSAGE, $connection, $data);
     }
     
@@ -195,7 +197,7 @@ class Gateway extends Worker
         // 连接的session
         $connection->session = '';
         // 该连接的心跳参数
-        $connection->pingNotResponseCount = 0;
+        $connection->pingNotResponseCount = -1;
         // 保存客户端连接connection对象
         $this->_clientConnections[$connection->globalClientId] = $connection;
         // 保存该连接的内部gateway通讯地址
@@ -403,6 +405,10 @@ class Gateway extends Worker
     public function onWorkerConnect($connection)
     {
         $connection->remoteAddress = $connection->getRemoteIp().':'.$connection->getRemotePort();
+        if(TcpConnection::$defaultMaxSendBufferSize == $connection->maxSendBufferSize)
+        {
+            $connection->maxSendBufferSize = 10*1024*1024;
+        }
         $this->_workerConnections[$connection->remoteAddress] = $connection;
     }
     
@@ -583,10 +589,13 @@ class Gateway extends Worker
                 $connection->destroy();
                 continue;
             }
-            $connection->pingNotResponseCount++;
-            if($this->pingData)
+            // $connection->pingNotResponseCount为-1说明最近客户端有发来消息,则不给客户端发送心跳
+            if($connection->pingNotResponseCount++ >= 0)
             {
-                $connection->send($this->pingData);
+                if($this->pingData)
+                {
+                    $connection->send($this->pingData);
+                }
             }
         }
     }

+ 1 - 0
Workerman/Connection/AsyncTcpConnection.php

@@ -52,6 +52,7 @@ class AsyncTcpConnection extends TcpConnection
         $this->id = self::$_idRecorder++;
         // 统计数据
         self::$statistics['connection_count']++;
+        $this->maxSendBufferSize = self::$defaultMaxSendBufferSize;
     }
     
     public function connect()

+ 15 - 7
Workerman/Connection/TcpConnection.php

@@ -94,14 +94,21 @@ class TcpConnection extends ConnectionInterface
     public $id = 0;
     
     /**
-     * 发送缓冲区大小,当发送缓冲区满时,会尝试触发onBufferFull回调(如果有设置的话)
+     * 设置当前连接的最大发送缓冲区大小,默认大小为TcpConnection::$defaultMaxSendBufferSize
+     * 当发送缓冲区满时,会尝试触发onBufferFull回调(如果有设置的话)
      * 如果没设置onBufferFull回调,由于发送缓冲区满,则后续发送的数据将被丢弃,
-     * 直到发送缓冲区有空
+     * 并触发onError回调,直到发送缓冲区有空位
      * 注意 此值可以动态设置
-     * 例如 Workerman\Connection\TcpConnection::$maxSendBufferSize=1024000;
      * @var int
      */
-    public static $maxSendBufferSize = 1048576;
+    public $maxSendBufferSize = 1048576;
+    
+    /**
+     * 默认发送缓冲区大小,设置此属性会影响所有连接的默认发送缓冲区大小
+     * 如果想设置某个连接发送缓冲区的大小,可以单独设置对应连接的$maxSendBufferSize属性
+     * @var int
+     */
+    public static $defaultMaxSendBufferSize = 1048576;
     
     /**
      * 能接受的最大数据包,为了防止恶意攻击,当数据包的大小大于此值时执行断开
@@ -140,7 +147,7 @@ class TcpConnection extends ConnectionInterface
      * @var int
      */
     protected $_currentPackageLength = 0;
-
+    
     /**
      * 当前的连接状态
      * @var int
@@ -185,6 +192,7 @@ class TcpConnection extends ConnectionInterface
         $this->_socket = $socket;
         stream_set_blocking($this->_socket, 0);
         Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
+        $this->maxSendBufferSize = self::$defaultMaxSendBufferSize;
     }
     
     /**
@@ -264,7 +272,7 @@ class TcpConnection extends ConnectionInterface
         else
         {
             // 缓冲区已经标记为满,仍然然有数据发送,则丢弃数据包
-            if(self::$maxSendBufferSize <= strlen($this->_sendBuffer))
+            if($this->maxSendBufferSize <= strlen($this->_sendBuffer))
             {
                 // 为status命令统计发送失败次数
                 self::$statistics['send_fail']++;
@@ -559,7 +567,7 @@ class TcpConnection extends ConnectionInterface
      */
     protected function checkBufferIsFull()
     {
-        if(self::$maxSendBufferSize <= strlen($this->_sendBuffer))
+        if($this->maxSendBufferSize <= strlen($this->_sendBuffer))
         {
             if($this->onBufferFull)
             {

+ 1 - 1
Workerman/Worker.php

@@ -21,7 +21,7 @@ class Worker
      * 版本号
      * @var string
      */
-    const VERSION = '3.1.4';
+    const VERSION = '3.1.5';
     
     /**
      * 状态 启动中