rustis/client/
pooled_client_manager.rs

1use crate::{
2    client::{Client, Config, IntoConfig},
3    commands::ConnectionCommands,
4    Error, Result,
5};
6use bb8::ManageConnection;
7
8/// An object which manages a pool of clients, based on [bb8](https://docs.rs/bb8/latest/bb8/)
9pub struct PooledClientManager {
10    config: Config,
11}
12
13impl PooledClientManager {
14    pub fn new(config: impl IntoConfig) -> Result<Self> {
15        Ok(Self {
16            config: config.into_config()?,
17        })
18    }
19}
20
21impl ManageConnection for PooledClientManager {
22    type Connection = Client;
23    type Error = Error;
24
25    async fn connect(&self) -> Result<Client> {
26        let config = self.config.clone();
27        Client::connect(config).await
28    }
29
30    async fn is_valid(&self, client: &mut Client) -> Result<()> {
31        client.ping::<String>(Default::default()).await?;
32        Ok(())
33    }
34
35    fn has_broken(&self, _client: &mut Client) -> bool {
36        false
37    }
38}