use std::time::Duration;
use crate::{
cache_redis::{RedisBreakerConfig, RedisCacheConfig},
lock::{LockError, LockResult},
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RedisLockConfig {
pub redis: RedisCacheConfig,
pub namespace: String,
pub command_timeout: Duration,
pub breaker: RedisBreakerConfig,
pub require_cluster_hash_tag: bool,
}
impl Default for RedisLockConfig {
fn default() -> Self {
Self {
redis: RedisCacheConfig::default(),
namespace: "rs-zero:lock".to_string(),
command_timeout: Duration::from_secs(2),
breaker: RedisBreakerConfig::default(),
require_cluster_hash_tag: true,
}
}
}
impl RedisLockConfig {
pub fn go_zero_defaults() -> Self {
Self {
breaker: RedisBreakerConfig::go_zero_defaults(),
..Self::default()
}
}
pub fn validate(&self) -> LockResult<()> {
self.redis.validate()?;
if self.namespace.trim().is_empty() {
return Err(LockError::InvalidConfig(
"redis lock namespace is required".to_string(),
));
}
if self.command_timeout.is_zero() {
return Err(LockError::InvalidConfig(
"redis lock command_timeout must be greater than zero".to_string(),
));
}
Ok(())
}
}