#![allow(clippy::needless_doctest_main)]
#![deny(missing_docs, missing_debug_implementations)]
pub use bb8;
pub use redis_cluster_async;
use async_trait::async_trait;
use redis_cluster_async::{
redis::{ErrorKind, IntoConnectionInfo, RedisError},
Client, Connection,
};
#[derive(Clone)]
pub struct RedisConnectionManager {
client: Client,
}
impl std::fmt::Debug for RedisConnectionManager {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RedisConnectionManager")
.field("client", &format!("pointer({:p})", &self.client))
.finish()
}
}
impl RedisConnectionManager {
pub fn new<T: IntoConnectionInfo>(infos: Vec<T>) -> Result<RedisConnectionManager, RedisError> {
Ok(RedisConnectionManager {
client: Client::open(infos.into_iter().collect())?,
})
}
}
#[async_trait]
impl bb8::ManageConnection for RedisConnectionManager {
type Connection = Connection;
type Error = RedisError;
async fn connect(&self) -> Result<Self::Connection, Self::Error> {
self.client.get_connection().await
}
async fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> {
let pong: String = redis::cmd("PING").query_async(conn).await?;
match pong.as_str() {
"PONG" => Ok(()),
_ => Err((ErrorKind::ResponseError, "ping request").into()),
}
}
fn has_broken(&self, _: &mut Self::Connection) -> bool {
false
}
}