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

add pool for pubsub connections #159

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
41 changes: 41 additions & 0 deletions redis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,50 @@ pub use bb8;
pub use redis;

use async_trait::async_trait;
use redis::aio::PubSub;
use redis::{aio::Connection, ErrorKind};
use redis::{Client, IntoConnectionInfo, RedisError};

/// A `bb8::ManageConnection` for `redis::aio::PubSub`
#[derive(Clone, Debug)]
pub struct RedisPubSubConnectionManager {
client: Client,
}

impl RedisPubSubConnectionManager {
/// Create a new `RedisConnectionPubSubManager`.
/// See `redis::Client::open` for a description of the parameter types.
pub fn new<T: IntoConnectionInfo>(info: T) -> Result<Self, RedisError> {
Ok(Self {
client: Client::open(info.into_connection_info()?)?,
})
}
}

#[async_trait]
impl bb8::ManageConnection for RedisPubSubConnectionManager {
type Connection = PubSub;
type Error = RedisError;

async fn connect(&self) -> Result<Self::Connection, Self::Error> {
Ok(self.client.get_async_connection().await?.into_pubsub())
}

async fn is_valid(&self, _conn: &mut Self::Connection) -> Result<(), Self::Error> {
// TODO:
Copy link
Author

Choose a reason for hiding this comment

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

Something needs to be done here, but not sure what would be best for a PubSub.

Copy link
Owner

Choose a reason for hiding this comment

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

Maybe punsubscribe() with an empty pattern?

Copy link
Author

Choose a reason for hiding this comment

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

Good idea. Made that change, just trying to validate a few things.

Ok(())
// 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
}
}

/// A `bb8::ManageConnection` for `redis::Client::get_async_connection`.
#[derive(Clone, Debug)]
pub struct RedisConnectionManager {
Expand Down