diff --git a/mobc-redis/src/lib.rs b/mobc-redis/src/lib.rs index 332ca0a..3192df2 100644 --- a/mobc-redis/src/lib.rs +++ b/mobc-redis/src/lib.rs @@ -1,18 +1,23 @@ -pub use redis; pub use mobc; +pub use redis; use mobc::async_trait; use mobc::Manager; use redis::aio::Connection; use redis::{Client, ErrorKind}; +use std::sync::atomic::{AtomicU32, Ordering}; pub struct RedisConnectionManager { client: Client, + ping_count: AtomicU32, } impl RedisConnectionManager { pub fn new(c: Client) -> Self { - Self { client: c } + Self { + client: c, + ping_count: AtomicU32::new(0), + } } } @@ -27,12 +32,12 @@ impl Manager for RedisConnectionManager { } async fn check(&self, mut conn: Self::Connection) -> Result { - let pong: String = redis::cmd("PING").query_async(&mut conn).await?; - if pong.as_str() != "PONG" { + let ping = self.ping_count.fetch_add(1, Ordering::SeqCst); + let ping = format!("PONG {}", ping); + let pong: String = redis::cmd("PING").arg(&ping).query_async(&mut conn).await?; + if pong != ping { return Err((ErrorKind::ResponseError, "pong response error").into()); } Ok(conn) } } - -