Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending unique ping data to ensure response is related to request #55

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions mobc-redis/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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),
}
}
}

Expand All @@ -27,12 +32,12 @@ impl Manager for RedisConnectionManager {
}

async fn check(&self, mut conn: Self::Connection) -> Result<Self::Connection, Self::Error> {
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)
}
}