walkor hace 11 años
padre
commit
cbac27da36

+ 29 - 2
applications/ChatDemo/Bootstrap/BusinessWorker.php

@@ -21,6 +21,19 @@ class BusinessWorker extends Man\Core\SocketWorker
     protected $gatewayConnections = array();
     
     /**
+     * 连不上的gateway地址
+     * ['ip:port' => retry_count, 'ip:port' => retry_count, ...]
+     * @var array
+     */
+    protected $badGatewayAddress = array();
+    
+    /**
+     * 连接gateway失败重试次数
+     * @var int
+     */
+    const MAX_RETRY_COUNT = 5;
+    
+    /**
      * 进程启动时初始化
      * @see Man\Core.SocketWorker::onStart()
      */
@@ -120,12 +133,26 @@ class BusinessWorker extends Man\Core\SocketWorker
             if(!isset($this->gatewayConnections[$addr]))
             {
                 // 执行连接
-                $conn = stream_socket_client("tcp://$addr", $errno, $errstr, 1);
+                $conn = @stream_socket_client("tcp://$addr", $errno, $errstr, 10);
                 if(!$conn)
                 {
-                    $this->notice($errstr);
+                    if(!isset($this->badGatewayAddress[$addr]))
+                    {
+                        $this->badGatewayAddress[$addr] = 0;
+                    }
+                    // 删除连不上的端口
+                    if($this->badGatewayAddress[$addr]++ > self::MAX_RETRY_COUNT)
+                    {
+                        $addresses_list = Store::get($key);
+                        unset($addresses_list[$addr]);
+                        Store::set($key, $addresses_list);
+                        $this->notice("tcp://$addr ".$errstr." del $addr from store");
+                        continue;
+                    }
+                    $this->notice("tcp://$addr ".$errstr);
                     continue;
                 }
+                unset($this->badGatewayAddress[$addr]);
                 $this->gatewayConnections[$addr] = $conn;
                 stream_set_blocking($this->gatewayConnections[$addr], 0);
                 

+ 1 - 1
applications/ChatDemo/Bootstrap/Gateway.php

@@ -112,7 +112,7 @@ class Gateway extends Man\Core\SocketWorker
         else
         {
             // 认证过, 触发ON_MESSAGE
-            $interface = 'ON_CONNECTION';
+            $interface = 'CMD_ON_MESSAGE';
             $ret =$this->sendToWorker(GatewayProtocol::CMD_ON_MESSAGE, $this->currentDealFd, $recv_str);
             if($ret === false)
             {

+ 1 - 1
workerman/Core/SocketWorker.php

@@ -542,7 +542,7 @@ abstract class SocketWorker extends AbstractWorker
             }
             if(!isset($this->connections[$this->currentDealFd]))
             {
-                $debug_str = new \Exception('sendToClient fail $this->connections[$this->currentDealFd] is null');
+                $debug_str = new \Exception('sendToClient fail $this->connections['.var_export($this->currentDealFd, true).'] is null');
                 $this->notice((string)$debug_str);
                 return false;
             }