protected static function getAllGatewayAddressesFromRegister()
{
static $addresses_cache, $last_update;
$time_now = time();
$expiration_time = 1;
$register_addresses = (array)static::$registerAddress;
if(empty($addresses_cache) || $time_now - $last_update > $expiration_time) {
foreach ($register_addresses as $register_address) {
$client = stream_socket_client('tcp://' . $register_address, $errno, $errmsg, static::$connectTimeout);
if ($client) {
break;
}
}
if (!$client) {
throw new Exception('Can not connect to tcp://' . $register_address . ' ' . $errmsg);
}
fwrite($client, '{"event":"worker_connect","secret_key":"' . static::$secretKey . '"}' . "\n");
stream_set_timeout($client, 5);
$ret = fgets($client, 655350);
if (!$ret || !$data = json_decode(trim($ret), true)) {
throw new Exception('getAllGatewayAddressesFromRegister fail. tcp://' .
$register_address . ' return ' . var_export($ret, true));
}
$last_update = $time_now;
$addresses_cache = $data['addresses'];
这段代码用 businessworker 身份去连接到register 服务器获取网关地址。
这段代码在获取到网关连接数,为啥没看见主动关闭tcp连接?