bb8_redis_cluster_async/
lib.rs

1pub use bb8;
2pub use redis;
3
4use async_trait::async_trait;
5use redis::cluster::ClusterClient;
6use redis::cluster_async::ClusterConnection;
7use redis::{ErrorKind, IntoConnectionInfo, RedisError};
8
9#[derive(Clone)]
10pub struct RedisConnectionManager {
11    client: ClusterClient,
12}
13
14impl std::fmt::Debug for RedisConnectionManager {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        f.debug_struct("RedisConnectionManager")
17            .field("client", &format!("pointer({:p})", &self.client))
18            .finish()
19    }
20}
21
22impl RedisConnectionManager {
23    pub fn new<T: IntoConnectionInfo>(infos: Vec<T>) -> Result<RedisConnectionManager, RedisError> {
24        Ok(RedisConnectionManager {
25            client: ClusterClient::new(infos.into_iter().collect())?,
26        })
27    }
28}
29
30#[async_trait]
31impl bb8::ManageConnection for RedisConnectionManager {
32    type Connection = ClusterConnection;
33    type Error = RedisError;
34
35    async fn connect(&self) -> Result<Self::Connection, Self::Error> {
36        self.client.get_async_connection().await
37    }
38
39    async fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> {
40        let pong: String = redis::cmd("PING").query_async(conn).await?;
41        match pong.as_str() {
42            "PONG" => Ok(()),
43            _ => Err((ErrorKind::ResponseError, "ping request").into()),
44        }
45    }
46
47    fn has_broken(&self, _: &mut Self::Connection) -> bool {
48        false
49    }
50}