Skip to content
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let ping = self.ping_count.fetch_add(1, Ordering::SeqCst);
let ping = self.ping_count.fetch_add(1, Ordering::Relaxed);

no need for SeqCst here

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)
}
}