Пример #1
0
func Init(cfg *config.Conf) error {
	if cfg == nil {
		err := fmt.Errorf("config is nil")
		return err
	}

	cfgHandler = cfg
	proxyConfig := cfg.GetConfig()
	allClusterConfigs, _ := proxyConfig.GetAllClusters()
	serverTimeout := proxyConfig.ServerTimeout()

	for clusterName, clusterCfg := range allClusterConfigs {

		master := clusterCfg.GetMasterNode()
		slaves := clusterCfg.GetSlaveNodes()
		slaveNum := len(slaves)
		DBName := clusterCfg.GetMasterNode().DBName
		oneCluster := Cluster{nil, make([]*mysql.DB, slaveNum), slaveNum, clusterName, DBName}

		db, err := openDBFromNode(master)
		if err != nil {
			return err
		}
		db.SetMaxOpenConns(master.MaxConnections)
		db.SetMaxIdleConns(master.MaxConnectionPoolSize)
		oneCluster.masterDB = db

		for i, slave := range slaves {
			db, err := openDBFromNode(slave)
			if err != nil {
				return err
			}
			db.SetMaxOpenConns(slave.MaxConnections)
			db.SetMaxIdleConns(slave.MaxConnectionPoolSize)
			oneCluster.slavesDB[i] = db
		}
		clusterConns[clusterName] = &oneCluster
		go oneCluster.Probe(serverTimeout)
	}

	return nil

}
Пример #2
0
func Init(cfg *config.Conf) error {
	if cfg == nil {
		err := fmt.Errorf("config is nil")
		return err
	}

	cfgHandler = cfg
	allClusterConfigs, err := cfg.GetConfig().GetAllClusters()
	if err != nil {
		return err
	}

	for clusterName, clusterCfg := range allClusterConfigs {
		cluster, err := makeCluster(clusterName, clusterCfg)
		if err != nil {
			log.Errorf("Make cluster error clusterName:%s msg:%s", clusterName, err.Error())
			return err
		}
		clusterConns[clusterName] = cluster
	}

	go monitor()
	return nil
}