mobc_redis/
lib.rs

1pub use mobc;
2pub use redis;
3
4use mobc::async_trait;
5use mobc::Manager;
6use redis::aio::MultiplexedConnection as Connection;
7use redis::{Client, ErrorKind};
8
9pub struct RedisConnectionManager {
10    client: Client,
11}
12
13impl RedisConnectionManager {
14    pub fn new(c: Client) -> Self {
15        Self { client: c }
16    }
17}
18
19#[async_trait]
20impl Manager for RedisConnectionManager {
21    type Connection = Connection;
22    type Error = redis::RedisError;
23
24    async fn connect(&self) -> Result<Self::Connection, Self::Error> {
25        let c = self.client.get_multiplexed_async_connection().await?;
26        Ok(c)
27    }
28
29    async fn check(&self, mut conn: Self::Connection) -> Result<Self::Connection, Self::Error> {
30        let pong: String = redis::cmd("PING").query_async(&mut conn).await?;
31        if pong.as_str() != "PONG" {
32            return Err((ErrorKind::ResponseError, "pong response error").into());
33        }
34        Ok(conn)
35    }
36}