选择语言 :

 Module_Database_Driver::_get_rand_host

获取一个随机HOST

null Module_Database_Driver::_get_rand_host( [ array $exclude_hosts = array(0) , string $type = null ] )

参数列表

参数 类型 描述 默认值
$exclude_hosts array 排除的HOST array(0)
$type string 配置类型 null
File: ./modules/database/driver.class.php
protected function _get_rand_host($exclude_hosts = array() , $type = null)
{
    if (!$type)$type = $this->_connection_type;
    $hostname = $this->config['connection']['hostname'];

    if (!is_array($hostname))
    {
        if ( in_array($hostname, $exclude_hosts) )
        {
            return false;
        }

        if ($exclude_hosts && $type!='master' && in_array($hostname, $exclude_hosts))
        {
            # 如果相应的slave都已不可获取,则改由获取master
            return $this->_get_rand_host($exclude_hosts, 'master');
        }

        return $hostname;
    }

    $hostconfig = $hostname[$type];

    if (is_array($hostconfig))
    {
        if ($exclude_hosts)
        {
            $hostconfig = array_diff($hostconfig, $exclude_hosts);
        }

        $hostconfig = array_values($hostconfig);
        $count = count($hostconfig);

        if ($count==0)
        {
            if ($type!='master')
            {
                return $this->_get_rand_host($exclude_hosts,'master');
            }
            else
            {
                return false;
            }
        }

        # 获取一个随机链接
        $rand_id = mt_rand(0, $count - 1);

        return $hostconfig[$rand_id];
    }
    else
    {
        if (in_array($hostconfig, $exclude_hosts))
        {
            return false;
        }

        return $hostconfig;
    }
}