zero4rs 2.0.0

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
use redis::cluster::ClusterClient;

use crate::core::redis_rs::redis_client_cluster::ReidsClientCluster;
use crate::core::redis_rs::redis_client_sentinel::ReidsClientSentinel;
use crate::core::redis_rs::redis_client_standalone::ReidsClientStandalone;
use crate::core::redis_rs::ReidsClient;
use crate::core::redis_rs::Schema;

pub async fn create_client(schema: &Schema) -> Result<ReidsClient, anyhow::Error> {
    if schema.is_standalone() {
        Ok(ReidsClient {
            schema: schema.to_owned(),
            client_standalone: Some(ReidsClientStandalone {
                client: create_pool_with_standalone(schema).await?,
            }),
            client_cluster: None,
            client_sentinel: None,
        })
    } else if schema.is_cluster() {
        Ok(ReidsClient {
            schema: schema.to_owned(),
            client_standalone: None,
            client_cluster: Some(ReidsClientCluster {
                client: create_pool_with_cluster(schema).await?,
            }),
            client_sentinel: None,
        })
    } else if schema.is_sentinel() {
        Ok(ReidsClient {
            schema: schema.to_owned(),
            client_standalone: None,
            client_cluster: None,
            client_sentinel: Some(ReidsClientSentinel {
                client: create_pool_with_sentinel(schema).await?,
            }),
        })
    } else {
        todo!()
    }
}

async fn create_pool_with_standalone(schema: &Schema) -> Result<redis::Client, anyhow::Error> {
    let client = match redis::Client::open(schema.connection_string_standalone()) {
        Ok(client) => {
            log::info!("RedisClient-Standalone: server={}", schema);
            client
        }
        Err(e) => {
            log::error!("RedisClient-Standalone: server={}, error={:?}", schema, e);
            return Err(e.into());
        }
    };

    Ok(client)
}

async fn create_pool_with_cluster(schema: &Schema) -> Result<ClusterClient, anyhow::Error> {
    let client = match ClusterClient::new(schema.connection_servers()) {
        Ok(client) => {
            log::info!("RedisClient-Cluster: server={}", schema);
            client
        }
        Err(e) => {
            log::error!("RedisClient-Cluster: server={}, error={:?}", schema, e);
            return Err(e.into());
        }
    };

    Ok(client)
}

#[allow(dead_code)]
async fn create_pool_with_sentinel(schema: &Schema) -> Result<redis::Client, anyhow::Error> {
    log::info!(
        "RedisClient-Sentinel: master_name={}, username={:?}, password={:?}, servers={:?}",
        &schema.master_name(),
        schema.username(),
        schema.passwd(),
        schema.connection_servers()
    );

    let connection_info = redis::sentinel::SentinelNodeConnectionInfo {
        tls_mode: Some(redis::TlsMode::Insecure),
        redis_connection_info: Some(redis::RedisConnectionInfo {
            db: schema.db,
            username: schema.username(),
            password: schema.passwd(),
        }),
    };

    let client = match redis::sentinel::Sentinel::build(schema.connection_servers()) {
        Ok(mut sentinel) => {
            match sentinel
                .async_master_for(&schema.master_name(), Some(&connection_info))
                .await
            {
                Ok(client) => {
                    log::info!(
                        "RedisClient-Sentinel: master_name={}, server={}",
                        &schema.master_name(),
                        schema
                    );

                    client
                }
                Err(e) => {
                    log::error!(
                        "RedisClient-Sentinel: master_name={}, server={}, error={:?}",
                        &schema.master_name(),
                        schema,
                        e
                    );

                    return Err(e.into());
                }
            }
        }
        Err(e) => {
            log::error!("RedisClient-Sentinel: server={}, error={:?}", schema, e);
            return Err(e.into());
        }
    };

    Ok(client)
}