redis/
r2d2.rs

1#[cfg(feature = "sentinel")]
2use crate::sentinel::LockedSentinelClient;
3use crate::types::closed_connection_error;
4use crate::{ConnectionLike, RedisError};
5
6macro_rules! impl_manage_connection {
7    ($client:ty, $connection:ty) => {
8        impl r2d2::ManageConnection for $client {
9            type Connection = $connection;
10            type Error = RedisError;
11
12            fn connect(&self) -> Result<Self::Connection, Self::Error> {
13                self.get_connection()
14            }
15
16            fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> {
17                if conn.check_connection() {
18                    Ok(())
19                } else {
20                    Err(closed_connection_error())
21                }
22            }
23
24            fn has_broken(&self, conn: &mut Self::Connection) -> bool {
25                !conn.is_open()
26            }
27        }
28    };
29}
30
31impl_manage_connection!(crate::Client, crate::Connection);
32
33#[cfg(feature = "cluster")]
34impl_manage_connection!(
35    crate::cluster::ClusterClient,
36    crate::cluster::ClusterConnection
37);
38
39#[cfg(feature = "sentinel")]
40impl_manage_connection!(LockedSentinelClient, crate::Connection);