yii\db\Connection::openFromPool PHP Method

openFromPool() protected method

This method implements the load balancing among the given list of the servers.
protected openFromPool ( array $pool, array $sharedConfig ) : Connection
$pool array the list of connection configurations in the server pool
$sharedConfig array the configuration common to those given in `$pool`.
return Connection the opened DB connection, or null if no server is available
    protected function openFromPool(array $pool, array $sharedConfig)
    {
        if (empty($pool)) {
            return null;
        }
        if (!isset($sharedConfig['class'])) {
            $sharedConfig['class'] = get_class($this);
        }
        $cache = is_string($this->serverStatusCache) ? Yii::$app->get($this->serverStatusCache, false) : $this->serverStatusCache;
        shuffle($pool);
        foreach ($pool as $config) {
            $config = array_merge($sharedConfig, $config);
            if (empty($config['dsn'])) {
                throw new InvalidConfigException('The "dsn" option must be specified.');
            }
            $key = [__METHOD__, $config['dsn']];
            if ($cache instanceof Cache && $cache->get($key)) {
                // should not try this dead server now
                continue;
            }
            /* @var $db Connection */
            $db = Yii::createObject($config);
            try {
                $db->open();
                return $db;
            } catch (\Exception $e) {
                Yii::warning("Connection ({$config['dsn']}) failed: " . $e->getMessage(), __METHOD__);
                if ($cache instanceof Cache) {
                    // mark this server as dead and only retry it after the specified interval
                    $cache->set($key, 1, $this->serverRetryInterval);
                }
            }
        }
        return null;
    }