return err when unable to connect to redis

This commit is contained in:
Aravinth Manivannan 2021-06-13 12:51:22 +05:30
parent 3db009977a
commit 52b7ebe4ca
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
1 changed files with 6 additions and 10 deletions

View File

@ -79,11 +79,7 @@ impl RedisConnection {
pub async fn ping(&self) -> bool { pub async fn ping(&self) -> bool {
if let Ok(redis::Value::Status(v)) = self.exec(&mut redis::cmd("PING")).await { if let Ok(redis::Value::Status(v)) = self.exec(&mut redis::cmd("PING")).await {
if v == "PONG" { v == "PONG"
true
} else {
false
}
} else { } else {
false false
} }
@ -108,7 +104,7 @@ pub struct Redis {
impl Redis { impl Redis {
/// create new [Redis]. Will try to connect to Redis instance specified in [RedisConfig] /// create new [Redis]. Will try to connect to Redis instance specified in [RedisConfig]
pub async fn new(redis: RedisConfig) -> CaptchaResult<Self> { pub async fn new(redis: RedisConfig) -> CaptchaResult<Self> {
let (_client, connection) = Self::connect(redis).await; let (_client, connection) = Self::connect(redis).await?;
let master = Self { let master = Self {
_client, _client,
connection, connection,
@ -123,19 +119,19 @@ impl Redis {
self.connection.get_client() self.connection.get_client()
} }
async fn connect(redis: RedisConfig) -> (RedisClient, RedisConnection) { async fn connect(redis: RedisConfig) -> CaptchaResult<(RedisClient, RedisConnection)> {
let redis = redis.connect(); let redis = redis.connect();
let client = match &redis { let client = match &redis {
RedisClient::Single(c) => { RedisClient::Single(c) => {
let con = c.get_async_connection().await.unwrap(); let con = c.get_async_connection().await?;
RedisConnection::Single(Rc::new(RefCell::new(con))) RedisConnection::Single(Rc::new(RefCell::new(con)))
} }
RedisClient::Cluster(c) => { RedisClient::Cluster(c) => {
let con = c.get_connection().unwrap(); let con = c.get_connection()?;
RedisConnection::Cluster(Rc::new(RefCell::new(con))) RedisConnection::Cluster(Rc::new(RefCell::new(con)))
} }
}; };
(redis, client) Ok((redis, client))
} }
} }