use serde::Deserialize;
use snafu::Snafu;
use std::time::Duration;
use tibba_config::Config;
use tibba_error::Error as BaseError;
use tibba_util::parse_uri;
use validator::Validate;
#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("category: {category}, {message}"))]
Common { category: String, message: String },
#[snafu(display("{source}"))]
SingleBuild { source: deadpool_redis::BuildError },
#[snafu(display("{source}"))]
ClusterBuild {
source: deadpool_redis::cluster::CreatePoolError,
},
#[snafu(display("category: {category}, {source}"))]
Redis {
category: String,
source: deadpool_redis::redis::RedisError,
},
#[snafu(display("{source}"))]
Compression { source: tibba_util::Error },
#[snafu(display("category: {category}, {source}"))]
Url {
category: String,
source: url::ParseError,
},
#[snafu(display("category: {category}, {source}"))]
Validate {
category: String,
source: validator::ValidationErrors,
},
}
type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone, Default, Validate)]
pub struct RedisConfig {
#[validate(length(min = 1))]
pub nodes: Vec<String>,
pub pool_size: u32,
pub connection_timeout: Duration,
pub wait_timeout: Duration,
pub recycle_timeout: Duration,
pub password: Option<String>,
}
fn default_pool_size() -> u32 {
10
}
#[derive(Deserialize, Debug, Clone)]
struct RedisParams {
#[serde(default = "default_pool_size")]
pool_size: u32,
#[serde(default)]
#[serde(with = "humantime_serde")]
connection_timeout: Option<Duration>,
#[serde(default)]
#[serde(with = "humantime_serde")]
wait_timeout: Option<Duration>,
#[serde(default)]
#[serde(with = "humantime_serde")]
recycle_timeout: Option<Duration>,
password: Option<String>,
}
fn new_redis_config(config: &Config) -> Result<RedisConfig> {
let uri = config.get_string("uri").map_err(|e| Error::Common {
category: "config".to_string(),
message: e.to_string(),
})?;
let parsed = parse_uri::<RedisParams>(&uri).map_err(|e| Error::Common {
category: "redis".to_string(),
message: e.to_string(),
})?;
let nodes = parsed
.host_strings()
.iter()
.map(|item| format!("redis://{item}"))
.collect();
let query = parsed.query;
let redis_config = RedisConfig {
nodes,
pool_size: query.pool_size,
connection_timeout: query.connection_timeout.unwrap_or(Duration::from_secs(3)),
wait_timeout: query.wait_timeout.unwrap_or(Duration::from_secs(3)),
recycle_timeout: query.recycle_timeout.unwrap_or(Duration::from_secs(60)),
password: query.password,
};
redis_config.validate().map_err(|e| Error::Validate {
category: "redis".to_string(),
source: e,
})?;
Ok(redis_config)
}
impl From<Error> for BaseError {
fn from(val: Error) -> Self {
let err = match val {
Error::Common { category, message } => {
BaseError::new(message).with_sub_category(&category)
}
Error::SingleBuild { source } => BaseError::new(source)
.with_sub_category("single_build")
.with_status(500)
.with_exception(true),
Error::ClusterBuild { source } => BaseError::new(source)
.with_sub_category("cluster_build")
.with_status(500)
.with_exception(true),
Error::Redis { category, source } => BaseError::new(source)
.with_sub_category(&category)
.with_status(500)
.with_exception(true),
Error::Compression { source } => BaseError::new(source)
.with_sub_category("compression")
.with_exception(true),
Error::Url { category, source } => BaseError::new(source)
.with_sub_category(&category)
.with_status(500)
.with_exception(true),
Error::Validate { category, source } => {
BaseError::new(source).with_sub_category(&category)
}
};
err.with_category("cache")
}
}
mod cache;
mod pool;
mod ttl_lru_store;
mod two_level_store;
pub use cache::*;
pub use pool::*;
pub use ttl_lru_store::*;
pub use two_level_store::*;