src/SMProxyServer.php
private function setStartConns()
    {
        $clients = [];
        foreach ($this->dbConfig as $key => $value) {
            if (count(explode(DB_DELIMITER, $key)) < 2) {
                continue;
            }
            //测试数据库host port是否可连接
            $test_client = new \Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
            if (!$test_client->connect($value['serverInfo']['host'], $value['serverInfo']['port'], $value['serverInfo']['timeout'])) {
                throw new MySQLException('connect ' . explode(DB_DELIMITER, $key)[0] .
                    ' ' . explode(DB_DELIMITER, $key)[1] . ' failed, ErrorCode: ' . $test_client->errCode . "\n");
            }
            $test_client->close();
            //初始化连接
            if (!isset($value['startConns'])) {
                $value['startConns'] = 1;
            }
            while ($value['startConns']) {
                //初始化startConns
                $mysql = new \Swoole\Coroutine\MySQL();
                $mysql->connect([
                    'host'     => CONFIG['server']['host'],
                    'user'     => CONFIG['server']['user'],
                    'port'     => CONFIG['server']['port'],
                    'password' => CONFIG['server']['password'],
                    'database' => explode(DB_DELIMITER, $key)[1],
                ]);
                if ($mysql ->connect_errno) {
                    throw new MySQLException(CONFIG['server']['host'] . ':' . CONFIG['server']['port'] . $mysql ->connect_error);
                }
                $mysql->setDefer();
                switch (explode(DB_DELIMITER, $key)[0]) {
                    case 'read':
                        $mysql->query('/*SMProxy test sql*/select sleep(0.1)');
                        break;
                    case 'write':
                        $mysql->query('/*SMProxy test sql*/set autocommit=1');
                        break;
                }
                $clients[] = $mysql;
                $value['startConns']--;
            }
        }
        foreach ($clients as $client) {
            $client->recv();
            if ($client ->errno) {
                throw new MySQLException($client ->error);
            }
            $client->close();
        }
        unset($clients);
    }
private function setStartConns()
    {
        $clients = [];
        foreach ($this->dbConfig as $key => $value) {
            if (count(explode(DB_DELIMITER, $key)) < 2) {
                continue;
            }
            //测试数据库host port是否可连接
            $test_client = new \Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
            if (!$test_client->connect($value['serverInfo']['host'], $value['serverInfo']['port'], $value['serverInfo']['timeout'])) {
                throw new MySQLException('connect ' . explode(DB_DELIMITER, $key)[0] .
                    ' ' . explode(DB_DELIMITER, $key)[1] . ' failed, ErrorCode: ' . $test_client->errCode . "\n");
            }
            $test_client->close();
            //初始化连接
            if (!isset($value['startConns'])) {
                $value['startConns'] = 1;
            }
            while ($value['startConns']) {
                //初始化startConns
                $mysql = new \Swoole\Coroutine\MySQL();
                $mysql->connect([
                    'host'     => CONFIG['server']['host'],
                    'user'     => CONFIG['server']['user'],
                    'port'     => CONFIG['server']['port'],
                    'password' => CONFIG['server']['password'],
                    'database' => explode(DB_DELIMITER, $key)[1],
                ]);
                if ($mysql ->connect_errno) {
                    throw new MySQLException(CONFIG['server']['host'] . ':' . CONFIG['server']['port'] . $mysql ->connect_error);
                }
                $mysql->setDefer();
                switch (explode(DB_DELIMITER, $key)[0]) {
                    case 'read':
                        $mysql->query('/*SMProxy test sql*/select sleep(0.1)');
                        break;
                    case 'write':
                        $mysql->query('/*SMProxy test sql*/set autocommit=1');
                        break;
                }
                $clients[] = $mysql;
                $value['startConns']--;
            }
        }
        foreach ($clients as $client) {
            $client->recv();
            if ($client ->errno) {
                throw new MySQLException($client ->error);
            }
            $client->close();
        }
        unset($clients);
    }
设置服务启动连接数

@throws MySQLException