pub async fn from_config(
        client_server_map: ClientServerMap,
        stats: Reporter,
    ) -> ConnectionPool {
        let config = get_config();
        let mut shards = Vec::new();
        let mut addresses = Vec::new();
        let mut banlist = Vec::new();
        let mut address_id = 0;
        let mut shard_ids = config
            .shards
            .clone()
            .into_keys()
            .map(|x| x.to_string())
            .collect::<Vec<String>>();
        shard_ids.sort_by_key(|k| k.parse::<i64>().unwrap());

        for shard_idx in shard_ids {
            let shard = &config.shards[&shard_idx];
            let mut pools = Vec::new();
            let mut servers = Vec::new();
            let mut replica_number = 0;

            for server in shard.servers.iter() {
                let role = match server.2.as_ref() {
                    "primary" => Role::Primary,
                    "replica" => Role::Replica,
                    _ => {
                        error!("Config error: server role can be 'primary' or 'replica', have: '{}'. Defaulting to 'replica'.", server.2);
                        Role::Replica
                    }
                };

                let address = Address {
                    id: address_id,
                    host: server.0.clone(),
                    port: server.1.to_string(),
                    role: role,
                    replica_number,
                    shard: shard_idx.parse::<usize>().unwrap(),
                };

                address_id += 1;

                if role == Role::Replica {
                    replica_number += 1;
                }

                let manager = ServerPool::new(
                    address.clone(),
                    config.user.clone(),
                    &shard.database,
                    client_server_map.clone(),
                    stats.clone(),
                );

                let pool = Pool::builder()
                    .max_size(config.general.pool_size)
                    .connection_timeout(std::time::Duration::from_millis(
                        config.general.connect_timeout,
                    ))
                    .test_on_check_out(false)
                    .build(manager)
                    .await
                    .unwrap();

                pools.push(pool);
                servers.push(address);
            }

            shards.push(pools);
            addresses.push(servers);
            banlist.push(HashMap::new());
        }

        assert_eq!(shards.len(), addresses.len());
        let address_len = addresses.len();

        ConnectionPool {
            databases: shards,
            addresses: addresses,
            round_robin: rand::random::<usize>() % address_len, // Start at a random replica
            banlist: Arc::new(RwLock::new(banlist)),
            stats: stats,
        }
    }
/// Construct the connection pool from the configuration.
Construct the connection pool from the configuration.
Construct the connection pool from the configuration .
pub async fn from_config(
        client_server_map: ClientServerMap,
        stats: Reporter,
    ) -> ConnectionPool {
        let config = get_config();
        let mut shards = Vec::new();
        let mut addresses = Vec::new();
        let mut banlist = Vec::new();
        let mut address_id = 0;
        let mut shard_ids = config
            .shards
            .clone()
            .into_keys()
            .map(|x| x.to_string())
            .collect::<Vec<String>>();
        shard_ids.sort_by_key(|k| k.parse::<i64>().unwrap());
        for shard_idx in shard_ids {
            let shard = &config.shards[&shard_idx];
            let mut pools = Vec::new();
            let mut servers = Vec::new();
            let mut replica_number = 0;
            for server in shard.servers.iter() {
                let role = match server.2.as_ref() {
                    "primary" => Role::Primary,
                    "replica" => Role::Replica,
                    _ => {
                        error!("Config error: server role can be 'primary' or 'replica', have: '{}'. Defaulting to 'replica'.", server.2);
                        Role::Replica
                    }
                };
                let address = Address {
                    id: address_id,
                    host: server.0.clone(),
                    port: server.1.to_string(),
                    role: role,
                    replica_number,
                    shard: shard_idx.parse::<usize>().unwrap(),
                };
                address_id += 1;
                if role == Role::Replica {
                    replica_number += 1;
                }
                let manager = ServerPool::new(
                    address.clone(),
                    config.user.clone(),
                    &shard.database,
                    client_server_map.clone(),
                    stats.clone(),
                );
                let pool = Pool::builder()
                    .max_size(config.general.pool_size)
                    .connection_timeout(std::time::Duration::from_millis(
                        config.general.connect_timeout,
                    ))
                    .test_on_check_out(false)
                    .build(manager)
                    .await
                    .unwrap();
                pools.push(pool);
                servers.push(address);
            }
            shards.push(pools);
            addresses.push(servers);
            banlist.push(HashMap::new());
        }
        assert_eq!(shards.len(), addresses.len());
        let address_len = addresses.len();
        ConnectionPool {
            databases: shards,
            addresses: addresses,
            round_robin: rand::random::<usize>() % address_len, // Start at a random replica
            banlist: Arc::new(RwLock::new(banlist)),
            stats: stats,
        }
    }
pub async fn from_config ( client_server_map : ClientServerMap , stats : Reporter , ) -> ConnectionPool { let config = get_config ( ) ; let mut shards = Vec :: new ( ) ; let mut addresses = Vec :: new ( ) ; let mut banlist = Vec :: new ( ) ; let mut address_id = 0 ; let mut shard_ids = config . shards . clone ( ) . into_keys ( ) . map ( | x | x . to_string ( ) ) . collect :: < Vec < String > > ( ) ; shard_ids . sort_by_key ( | k | k . parse :: < i64 > ( ) . unwrap ( ) ) ; for shard_idx in shard_ids { let shard = & config . shards [ & shard_idx ] ; let mut pools = Vec :: new ( ) ; let mut servers = Vec :: new ( ) ; let mut replica_number = 0 ; for server in shard . servers . iter ( ) { let role = match server . 2 . as_ref ( ) { " " => Role :: Primary , " " => Role :: Replica , _ => { error ! ( " " server 2 ) ; Role :: Replica } } ; let address = Address { id : address_id , host : server . 0 . clone ( ) , port : server . 1 . to_string ( ) , role : role , replica_number , shard : shard_idx . parse :: < usize > ( ) . unwrap ( ) , } ; address_id += 1 ; if role == Role :: Replica { replica_number += 1 ; } let manager = ServerPool :: new ( address . clone ( ) , config . user . clone ( ) , & shard . database , client_server_map . clone ( ) , stats . clone ( ) , ) ; let pool = Pool :: builder ( ) . max_size ( config . general . pool_size ) . connection_timeout ( std :: time :: Duration :: from_millis ( config . general . connect_timeout , ) ) . test_on_check_out ( false ) . build ( manager ) . await . unwrap ( ) ; pools . push ( pool ) ; servers . push ( address ) ; } shards . push ( pools ) ; addresses . push ( servers ) ; banlist . push ( HashMap :: new ( ) ) ; } assert_eq ! ( shards len ( ) addresses len ( ) ) ; let address_len = addresses . len ( ) ; ConnectionPool { databases : shards , addresses : addresses , round_robin : rand :: random :: < usize > ( ) % address_len , // Start at a random replica banlist : Arc :: new ( RwLock :: new ( banlist ) ) , stats : stats , } }
Construct the connection pool from the configuration.
Construct the connection pool from the configuration .
client_server_map
ClientServerMap
stats
Reporter
client_server_map
ClientServerMap
stats
Reporter