$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];
};
找不到值是因为businessWorker和gateway没建立起连接,例如服刚启动时,例如业务报错导致businessWorker退出重启时,一般都是短暂的。如果没有可用的businessWorker进程,应该返回false,不要返回null
改成
请问返回false就不会调用$worker_connection->send($gateway_data)方法了么?如果还会调的话,那还是会报错啊
返回false不会调用send
好的,感谢