use crate::{
clients::{Pipeline, RedisClient},
error::RedisError,
interfaces::{self, *},
modules::inner::RedisClientInner,
protocol::command::{RedisCommand, RouterCommand},
runtime::{oneshot_channel, RefCount},
types::Server,
};
use std::{collections::HashMap, fmt, fmt::Formatter};
#[derive(Clone)]
#[cfg_attr(docsrs, doc(cfg(feature = "replicas")))]
pub struct Replicas {
inner: RefCount<RedisClientInner>,
}
impl fmt::Debug for Replicas {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Replicas").field("id", &self.inner.id).finish()
}
}
#[doc(hidden)]
impl From<&RefCount<RedisClientInner>> for Replicas {
fn from(inner: &RefCount<RedisClientInner>) -> Self {
Replicas { inner: inner.clone() }
}
}
impl ClientLike for Replicas {
#[doc(hidden)]
fn inner(&self) -> &RefCount<RedisClientInner> {
&self.inner
}
#[doc(hidden)]
fn change_command(&self, command: &mut RedisCommand) {
command.use_replica = true;
}
}
#[cfg(feature = "i-redis-json")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-redis-json")))]
impl RedisJsonInterface for Replicas {}
#[cfg(feature = "i-time-series")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-time-series")))]
impl TimeSeriesInterface for Replicas {}
#[cfg(feature = "i-cluster")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-cluster")))]
impl ClusterInterface for Replicas {}
#[cfg(feature = "i-config")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-config")))]
impl ConfigInterface for Replicas {}
#[cfg(feature = "i-geo")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-geo")))]
impl GeoInterface for Replicas {}
#[cfg(feature = "i-hashes")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-hashes")))]
impl HashesInterface for Replicas {}
#[cfg(feature = "i-hyperloglog")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-hyperloglog")))]
impl HyperloglogInterface for Replicas {}
#[cfg(feature = "i-keys")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-keys")))]
impl KeysInterface for Replicas {}
#[cfg(feature = "i-scripts")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-scripts")))]
impl LuaInterface for Replicas {}
#[cfg(feature = "i-lists")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-lists")))]
impl ListInterface for Replicas {}
#[cfg(feature = "i-memory")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-memory")))]
impl MemoryInterface for Replicas {}
#[cfg(feature = "i-server")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-server")))]
impl ServerInterface for Replicas {}
#[cfg(feature = "i-slowlog")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-slowlog")))]
impl SlowlogInterface for Replicas {}
#[cfg(feature = "i-sets")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-sets")))]
impl SetsInterface for Replicas {}
#[cfg(feature = "i-sorted-sets")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-sorted-sets")))]
impl SortedSetsInterface for Replicas {}
#[cfg(feature = "i-streams")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-streams")))]
impl StreamsInterface for Replicas {}
#[cfg(feature = "i-scripts")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-scripts")))]
impl FunctionInterface for Replicas {}
#[cfg(feature = "i-redisearch")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-redisearch")))]
impl RediSearchInterface for Replicas {}
impl Replicas {
pub fn nodes(&self) -> HashMap<Server, Server> {
self.inner.server_state.read().replicas.clone()
}
pub fn pipeline(&self) -> Pipeline<Replicas> {
Pipeline::from(self.clone())
}
pub fn client(&self) -> RedisClient {
RedisClient::from(&self.inner)
}
pub async fn sync(&self, reset: bool) -> Result<(), RedisError> {
let (tx, rx) = oneshot_channel();
let cmd = RouterCommand::SyncReplicas { tx, reset };
interfaces::send_to_router(&self.inner, cmd)?;
rx.await?
}
}