get redis client from url

This commit is contained in:
Aravinth Manivannan 2021-06-08 21:22:12 +05:30
parent a50a21d33b
commit 05dd9bbbb6
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
2 changed files with 40 additions and 14 deletions

View file

@ -136,15 +136,16 @@ pub mod tests {
use super::*; use super::*;
use crate::defense::{Level, LevelBuilder}; use crate::defense::{Level, LevelBuilder};
use crate::master::embedded::counter::tests::get_mcaptcha; use crate::master::embedded::counter::tests::get_mcaptcha;
use crate::master::redis::master::{Master, Redis}; use crate::master::redis::master::{Master, Redis, RedisClient};
pub async fn connect(redis: &Redis) -> RedisConnection { pub async fn connect(redis: &Redis) -> RedisConnection {
let redis = redis.connect();
match &redis { match &redis {
Redis::Single(c) => { RedisClient::Single(c) => {
let con = c.get_async_connection().await.unwrap(); let con = c.get_async_connection().await.unwrap();
RedisConnection::Single(Rc::new(RefCell::new(con))) RedisConnection::Single(Rc::new(RefCell::new(con)))
} }
Redis::Cluster(c) => { RedisClient::Cluster(c) => {
let con = c.get_connection().unwrap(); let con = c.get_connection().unwrap();
RedisConnection::Cluster(Rc::new(RefCell::new(con))) RedisConnection::Cluster(Rc::new(RefCell::new(con)))
} }
@ -153,11 +154,12 @@ pub mod tests {
const CAPTCHA_NAME: &str = "REDIS_CAPTCHA_TEST"; const CAPTCHA_NAME: &str = "REDIS_CAPTCHA_TEST";
const DURATION: usize = 10; const DURATION: usize = 10;
const REDIS_URL: &str = "redis://127.0.1.1/";
#[actix_rt::test] #[actix_rt::test]
async fn redis_master_works() { async fn redis_master_works() {
let client = redis::Client::open("redis://127.0.0.1/").unwrap(); let client = redis::Client::open("redis://127.0.0.1/").unwrap();
let r = connect(&Redis::Single(client)).await; let r = connect(&Redis::Single(REDIS_URL.into())).await;
{ {
let _ = r.delete_captcha(CAPTCHA_NAME).await; let _ = r.delete_captcha(CAPTCHA_NAME).await;
} }

View file

@ -40,35 +40,58 @@ use super::connection::RedisConnection;
#[derive(Clone)] #[derive(Clone)]
pub enum Redis { pub enum Redis {
Single(String),
Cluster(Vec<String>),
}
impl Redis {
pub fn connect(&self) -> RedisClient {
match self {
Self::Single(url) => {
let client = Client::open("redis://127.0.0.1/").unwrap();
RedisClient::Single(client)
}
Self::Cluster(nodes) => {
let cluster_client = ClusterClient::open(nodes.to_owned()).unwrap();
RedisClient::Cluster(cluster_client)
}
}
}
}
#[derive(Clone)]
pub enum RedisClient {
Single(Client), Single(Client),
Cluster(ClusterClient), Cluster(ClusterClient),
} }
pub struct Master { pub struct Master {
pub redis: Redis, pub redis: RedisClient,
pub con: Rc<RedisConnection>, pub con: Rc<RedisConnection>,
} }
impl Master { impl Master {
pub async fn new(redis: Redis) -> CaptchaResult<Self> { pub async fn new(redis: Redis) -> CaptchaResult<Self> {
let con = Self::connect(&redis).await; let (redis, con) = Self::connect(redis).await;
con.is_module_loaded().await?; con.is_module_loaded().await?;
let con = Rc::new(con); let con = Rc::new(con);
let master = Self { redis, con }; let master = Self { redis, con };
Ok(master) Ok(master)
} }
async fn connect(redis: &Redis) -> RedisConnection { async fn connect(redis: Redis) -> (RedisClient, RedisConnection) {
match &redis { let redis = redis.connect();
Redis::Single(c) => { let client = match &redis {
RedisClient::Single(c) => {
let con = c.get_async_connection().await.unwrap(); let con = c.get_async_connection().await.unwrap();
RedisConnection::Single(Rc::new(RefCell::new(con))) RedisConnection::Single(Rc::new(RefCell::new(con)))
} }
Redis::Cluster(c) => { RedisClient::Cluster(c) => {
let con = c.get_connection().unwrap(); let con = c.get_connection().unwrap();
RedisConnection::Cluster(Rc::new(RefCell::new(con))) RedisConnection::Cluster(Rc::new(RefCell::new(con)))
} }
} };
(redis, client)
} }
} }
@ -122,12 +145,13 @@ mod tests {
const CAPTCHA_NAME: &str = "REDIS_MASTER_CAPTCHA_TEST"; const CAPTCHA_NAME: &str = "REDIS_MASTER_CAPTCHA_TEST";
const DURATION: usize = 10; const DURATION: usize = 10;
const REDIS_URL: &str = "redis://127.0.1.1/";
#[actix_rt::test] #[actix_rt::test]
async fn redis_master_works() { async fn redis_master_works() {
let master = Master::new(Redis::Single(REDIS_URL.into())).await;
let client = redis::Client::open("redis://127.0.0.1/").unwrap(); let client = redis::Client::open("redis://127.0.0.1/").unwrap();
let master = Master::new(Redis::Single(client)).await; let r = connect(&Redis::Single(REDIS_URL.into())).await;
let client = redis::Client::open("redis://127.0.0.1/").unwrap();
let r = connect(&Redis::Single(client)).await;
assert!(master.is_ok()); assert!(master.is_ok());
let master = master.unwrap(); let master = master.unwrap();