1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use crate::{
  interfaces::*,
  modules::inner::RedisClientInner,
  runtime::RefCount,
  types::{ConnectionConfig, PerformanceConfig, ReconnectPolicy, SentinelConfig},
};
use std::fmt;

/// A struct for interacting directly with Sentinel nodes.
///
/// This struct **will not** communicate with Redis servers behind the sentinel interface, but rather with the
/// sentinel nodes themselves. Callers should use the [RedisClient](crate::clients::RedisClient) interface with a
/// [ServerConfig::Sentinel](crate::types::ServerConfig::Sentinel) for interacting with Redis services behind a
/// sentinel layer.
///
/// See the [sentinel API docs](https://redis.io/topics/sentinel#sentinel-api) for more information.
#[derive(Clone)]
#[cfg_attr(docsrs, doc(cfg(feature = "sentinel-client")))]
pub struct SentinelClient {
  inner: RefCount<RedisClientInner>,
}

impl ClientLike for SentinelClient {
  #[doc(hidden)]
  fn inner(&self) -> &RefCount<RedisClientInner> {
    &self.inner
  }
}

impl fmt::Debug for SentinelClient {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("SentinelClient")
      .field("id", &self.inner.id)
      .field("state", &self.state())
      .finish()
  }
}

#[doc(hidden)]
impl<'a> From<&'a RefCount<RedisClientInner>> for SentinelClient {
  fn from(inner: &'a RefCount<RedisClientInner>) -> Self {
    SentinelClient { inner: inner.clone() }
  }
}

impl EventInterface for SentinelClient {}
impl SentinelInterface for SentinelClient {}
impl MetricsInterface for SentinelClient {}
#[cfg(feature = "i-acl")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-acl")))]
impl AclInterface for SentinelClient {}
#[cfg(feature = "i-pubsub")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-pubsub")))]
impl PubsubInterface for SentinelClient {}
#[cfg(feature = "i-client")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-client")))]
impl ClientInterface for SentinelClient {}
impl AuthInterface for SentinelClient {}
#[cfg(feature = "i-server")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-server")))]
impl HeartbeatInterface for SentinelClient {}

impl SentinelClient {
  /// Create a new client instance without connecting to the sentinel node.
  ///
  /// See the [builder](crate::types::Builder) interface for more information.
  pub fn new(
    config: SentinelConfig,
    perf: Option<PerformanceConfig>,
    connection: Option<ConnectionConfig>,
    policy: Option<ReconnectPolicy>,
  ) -> SentinelClient {
    SentinelClient {
      inner: RedisClientInner::new(
        config.into(),
        perf.unwrap_or_default(),
        connection.unwrap_or_default(),
        policy,
      ),
    }
  }
}