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 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
35 changes: 35 additions & 0 deletions redis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,44 @@ 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> {
conn.punsubscribe("").await
}

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