$gateway->router偶现取不到值的情况,导致服务异常退出

IT小马

问题描述

$gateway->router偶现取不到值的情况,导致服务异常退出

程序代码

//指定 WebsocketBusinessWorker
        $gateway->router = function ($worker_connections, $client_connection) {
            $connection_names = [];
            foreach ($worker_connections as $key => $worker_connection) {
                $values = explode(':', $key);
                if ($values[1] == 'WebsocketBusinessWorker') {
                    $connection_names[$values[2]] = $key;
                }
            }
            if (!count($connection_names)) {
                return null;
            }
            $key = $connection_names[mt_rand(0, count($connection_names) - 1)];
            empty($client_connection->businessworker_address) && $client_connection->businessworker_address = $key;
            return $worker_connections[$client_connection->businessworker_address];
        };

截图报错信息里报错文件相关代码

截图

667 1 0
1个回答

walkor 打赏

找不到值是因为businessWorker和gateway没建立起连接,例如服刚启动时,例如业务报错导致businessWorker退出重启时,一般都是短暂的。如果没有可用的businessWorker进程,应该返回false,不要返回null

if (!count($connection_names)) {
    return null;
}

改成

if (!count($connection_names)) {
    return false;
}
  • IT小马 2023-03-15

    请问返回false就不会调用$worker_connection->send($gateway_data)方法了么?如果还会调的话,那还是会报错啊

  • walkor 2023-03-15

    返回false不会调用send

  • IT小马 2023-03-15

    好的,感谢

年代过于久远,无法发表回答
×
🔝