-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There are a few breaking changes made to support this. Most notably, the an optional `SentinelConfig` struct has been added to the RedisConfig object. It seems like obfuscating the fields here might be a good idea, but that's not how we've been rolling thus far with the config objects. Also, the `RedisConnection` trait has been redone to take a `RedisConfig` instead of a DSN in order to build the connection. Sentinel needs it owing to the large number of potential config params needed for Sentinel, and I think this is probably a better approach anyway. The base redis tests (well, most of them) have also been updated to support testing against either base Redis or Sentinel. The `rstest` crate has been added in order to be able to parameterize the tests sanely. We also use this crate for tests in `redis-rs`, so I think it's a no-brainer to add here.
- Loading branch information
Showing
8 changed files
with
300 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use async_trait::async_trait; | ||
use redis::{ | ||
sentinel::{SentinelClient, SentinelNodeConnectionInfo, SentinelServerType}, | ||
ErrorKind, IntoConnectionInfo, RedisError, | ||
}; | ||
use tokio::sync::Mutex; | ||
|
||
// The mutex here is needed b/c there's currently | ||
// no way to get connections in the redis sentinel client | ||
// without a mutable reference to the underlying client. | ||
struct LockedSentinelClient(pub(crate) Mutex<SentinelClient>); | ||
|
||
/// ConnectionManager that implements `bb8::ManageConnection` and supports | ||
/// asynchronous Sentinel connections via `redis::sentinel::SentinelClient` | ||
pub struct RedisSentinelConnectionManager { | ||
client: LockedSentinelClient, | ||
} | ||
|
||
impl RedisSentinelConnectionManager { | ||
pub fn new<T: IntoConnectionInfo>( | ||
info: Vec<T>, | ||
service_name: String, | ||
node_connection_info: Option<SentinelNodeConnectionInfo>, | ||
) -> Result<RedisSentinelConnectionManager, RedisError> { | ||
Ok(RedisSentinelConnectionManager { | ||
client: LockedSentinelClient(Mutex::new(SentinelClient::build( | ||
info, | ||
service_name, | ||
node_connection_info, | ||
SentinelServerType::Master, | ||
)?)), | ||
}) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl bb8::ManageConnection for RedisSentinelConnectionManager { | ||
type Connection = redis::aio::MultiplexedConnection; | ||
type Error = RedisError; | ||
|
||
async fn connect(&self) -> Result<Self::Connection, Self::Error> { | ||
self.client.0.lock().await.get_async_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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.