use std::future::Future;
use std::sync::Arc;
use redis_tower_commands::Ping;
use redis_tower_core::{
Command, ConnectionConfig, Frame, ProtocolVersion, RedisConnection, RedisError,
};
use redis_tower_protocol::helpers::{array, bulk};
use tower_service::Service;
use crate::auto_pipeline::{AutoPipelineConfig, AutoPipelineReconnectConfig, AutoPipelineService};
use crate::cache_layer::CacheService;
use crate::cache_state::CacheStatistics;
use crate::caching::{CachedClientConfig, connect_resp3, force_resp3};
use crate::circuit_breaker::{RedisCircuitBreakerClient, RedisCircuitBreakerConfig};
use crate::command_adapter::CommandAdapter;
use crate::credentials::{
CredentialReauthenticationHandle, StreamingCredentialProvider,
spawn_credential_reauthentication,
};
use crate::pipeline::PipelineExecutor;
use crate::reconnect::{
AddrConnectionFactory, ConnectionEventBus, ConnectionFactory, Resp3AddrConnectionFactory,
UrlConnectionFactory,
};
use crate::retry::{RetryClient, RetryPolicy};
use crate::transaction::TransactionExecutor;
#[derive(Clone)]
pub struct MultiplexedClient<S = AutoPipelineService> {
inner: CommandAdapter<S>,
}
#[derive(Clone)]
pub struct CachedMultiplexedClient {
inner: MultiplexedClient<CacheService<AutoPipelineService>>,
}
impl MultiplexedClient<AutoPipelineService> {
pub fn spawn_credential_reauthentication(
&self,
provider: Arc<dyn StreamingCredentialProvider>,
) -> CredentialReauthenticationHandle {
let client = self.clone();
spawn_credential_reauthentication(provider, move |credentials| {
let client = client.clone();
async move { client.execute(credentials.auth_command()).await }
})
}
pub async fn from_factory_with_maintenance(
factory: impl ConnectionFactory,
config: AutoPipelineConfig,
reconnect: AutoPipelineReconnectConfig,
) -> Result<(Self, crate::MaintenanceListenerHandle), RedisError> {
let (service, handle) =
AutoPipelineService::with_factory_and_maintenance(factory, config, reconnect).await?;
Ok((
Self {
inner: CommandAdapter::new(service),
},
handle,
))
}
pub async fn from_factory_with_maintenance_and_events(
factory: impl ConnectionFactory,
config: AutoPipelineConfig,
reconnect: AutoPipelineReconnectConfig,
events: ConnectionEventBus,
) -> Result<(Self, crate::MaintenanceListenerHandle), RedisError> {
let (service, handle) = AutoPipelineService::with_factory_and_maintenance_and_events(
factory, config, reconnect, events,
)
.await?;
Ok((
Self {
inner: CommandAdapter::new(service),
},
handle,
))
}
pub async fn connect(addr: &str) -> Result<Self, RedisError> {
let conn = RedisConnection::connect(addr).await?;
Ok(Self::from_connection(conn))
}
pub async fn connect_with_connection_config(
addr: &str,
config: &redis_tower_core::ConnectionConfig,
) -> Result<Self, RedisError> {
let conn = RedisConnection::connect_with_config(addr, config).await?;
Ok(Self::from_connection(conn))
}
pub async fn connect_url(url: &str) -> Result<Self, RedisError> {
let conn = RedisConnection::connect_url(url).await?;
Ok(Self::from_connection(conn))
}
pub async fn connect_url_with_connection_config(
url: &str,
config: &redis_tower_core::ConnectionConfig,
) -> Result<Self, RedisError> {
let conn = RedisConnection::connect_url_with_config(url, config).await?;
Ok(Self::from_connection(conn))
}
#[cfg(any(feature = "tls-rustls", feature = "tls-native-tls"))]
pub async fn connect_url_with_tls(
url: &str,
tls_config: &redis_tower_core::tls::TlsConfig,
) -> Result<Self, RedisError> {
let conn = RedisConnection::connect_url_with_tls(url, tls_config).await?;
Ok(Self::from_connection(conn))
}
#[cfg(any(feature = "tls-rustls", feature = "tls-native-tls"))]
pub async fn connect_url_with_tls_and_connection_config(
url: &str,
tls_config: &redis_tower_core::tls::TlsConfig,
connection_config: &redis_tower_core::ConnectionConfig,
) -> Result<Self, RedisError> {
let conn =
RedisConnection::connect_url_with_tls_and_config(url, tls_config, connection_config)
.await?;
Ok(Self::from_connection(conn))
}
pub async fn connect_resp3(addr: &str) -> Result<Self, RedisError> {
let conn = RedisConnection::connect_resp3(addr).await?;
Ok(Self::from_connection(conn))
}
pub fn connect_lazy(addr: impl Into<String>) -> Self {
Self::from_lazy_factory(
AddrConnectionFactory::new(addr),
AutoPipelineConfig::default(),
AutoPipelineReconnectConfig::default(),
)
}
pub fn connect_url_lazy(url: impl Into<String>) -> Self {
Self::from_lazy_factory(
UrlConnectionFactory::new(url),
AutoPipelineConfig::default(),
AutoPipelineReconnectConfig::default(),
)
}
pub fn from_connection(conn: RedisConnection) -> Self {
Self::from_connection_with_config(conn, AutoPipelineConfig::default())
}
pub fn from_connection_with_config(conn: RedisConnection, config: AutoPipelineConfig) -> Self {
Self {
inner: CommandAdapter::new(AutoPipelineService::new(conn, config)),
}
}
pub fn from_connection_with_events(
conn: RedisConnection,
config: AutoPipelineConfig,
events: ConnectionEventBus,
) -> Self {
Self {
inner: CommandAdapter::new(AutoPipelineService::new_with_events(conn, config, events)),
}
}
pub async fn from_factory(
factory: impl ConnectionFactory,
config: AutoPipelineConfig,
reconnect: AutoPipelineReconnectConfig,
) -> Result<Self, RedisError> {
let svc = AutoPipelineService::with_factory(factory, config, reconnect).await?;
Ok(Self {
inner: CommandAdapter::new(svc),
})
}
pub async fn from_factory_with_events(
factory: impl ConnectionFactory,
config: AutoPipelineConfig,
reconnect: AutoPipelineReconnectConfig,
events: ConnectionEventBus,
) -> Result<Self, RedisError> {
let svc = AutoPipelineService::with_factory_and_events(factory, config, reconnect, events)
.await?;
Ok(Self {
inner: CommandAdapter::new(svc),
})
}
pub fn from_lazy_factory(
factory: impl ConnectionFactory,
config: AutoPipelineConfig,
reconnect: AutoPipelineReconnectConfig,
) -> Self {
let svc = AutoPipelineService::with_lazy_factory(factory, config, reconnect);
Self {
inner: CommandAdapter::new(svc),
}
}
pub fn from_lazy_factory_with_events(
factory: impl ConnectionFactory,
config: AutoPipelineConfig,
reconnect: AutoPipelineReconnectConfig,
events: ConnectionEventBus,
) -> Self {
let svc =
AutoPipelineService::with_lazy_factory_and_events(factory, config, reconnect, events);
Self {
inner: CommandAdapter::new(svc),
}
}
pub fn is_connection_healthy(&self) -> bool {
self.inner.inner().is_connection_healthy()
}
pub fn subscribe_connection_health(&self) -> tokio::sync::watch::Receiver<bool> {
self.inner.inner().subscribe_connection_health()
}
pub fn queue_depth(&self) -> usize {
self.inner.inner().queue_depth()
}
pub async fn shutdown(self) {
self.inner.into_inner().shutdown().await;
}
pub fn retry(&self, policy: RetryPolicy) -> RetryClient<Self> {
RetryClient::new(self.clone(), policy)
}
}
impl CachedMultiplexedClient {
pub async fn connect(addr: &str) -> Result<Self, RedisError> {
Self::connect_with_config(addr, CachedClientConfig::default()).await
}
pub async fn connect_with_config(
addr: &str,
cache_config: CachedClientConfig,
) -> Result<Self, RedisError> {
Self::connect_with_pipeline_config(addr, cache_config, AutoPipelineConfig::default()).await
}
pub async fn connect_with_connection_config(
addr: &str,
connection_config: &ConnectionConfig,
cache_config: CachedClientConfig,
) -> Result<Self, RedisError> {
let factory =
Resp3AddrConnectionFactory::new(addr).with_connection_config(connection_config.clone());
Self::from_factory(factory, cache_config).await
}
pub async fn connect_url(url: &str) -> Result<Self, RedisError> {
Self::connect_url_with_config(url, CachedClientConfig::default()).await
}
pub async fn connect_url_with_config(
url: &str,
cache_config: CachedClientConfig,
) -> Result<Self, RedisError> {
Self::connect_url_with_connection_config(url, &ConnectionConfig::new(), cache_config).await
}
pub async fn connect_url_with_connection_config(
url: &str,
connection_config: &ConnectionConfig,
cache_config: CachedClientConfig,
) -> Result<Self, RedisError> {
let connection_config = connection_config
.clone()
.with_protocol(ProtocolVersion::Resp3);
let factory = UrlConnectionFactory::new(url).with_connection_config(connection_config);
Self::from_factory(factory, cache_config).await
}
pub async fn connect_with_pipeline_config(
addr: &str,
cache_config: CachedClientConfig,
pipeline_config: AutoPipelineConfig,
) -> Result<Self, RedisError> {
let factory = Resp3AddrConnectionFactory::new(addr);
Self::from_factory_with_pipeline_config(factory, cache_config, pipeline_config).await
}
pub async fn from_factory(
factory: impl ConnectionFactory,
cache_config: CachedClientConfig,
) -> Result<Self, RedisError> {
Self::from_factory_with_pipeline_config(
factory,
cache_config,
AutoPipelineConfig::default(),
)
.await
}
pub async fn from_factory_with_pipeline_config(
factory: impl ConnectionFactory,
cache_config: CachedClientConfig,
pipeline_config: AutoPipelineConfig,
) -> Result<Self, RedisError> {
Self::from_shared_factory_with_pipeline_config(
Arc::new(factory),
cache_config,
pipeline_config,
)
.await
}
async fn from_shared_factory_with_pipeline_config(
factory: Arc<dyn ConnectionFactory>,
cache_config: CachedClientConfig,
pipeline_config: AutoPipelineConfig,
) -> Result<Self, RedisError> {
let conn = connect_resp3(factory.as_ref()).await?;
Self::from_connection_with_shared_factory_and_pipeline_config(
conn,
factory,
cache_config,
pipeline_config,
)
.await
}
pub async fn from_connection_with_factory(
conn: RedisConnection,
receiver_factory: impl ConnectionFactory,
cache_config: CachedClientConfig,
) -> Result<Self, RedisError> {
Self::from_connection_with_factory_and_pipeline_config(
conn,
receiver_factory,
cache_config,
AutoPipelineConfig::default(),
)
.await
}
pub async fn from_connection_with_factory_and_pipeline_config(
conn: RedisConnection,
receiver_factory: impl ConnectionFactory,
cache_config: CachedClientConfig,
pipeline_config: AutoPipelineConfig,
) -> Result<Self, RedisError> {
Self::from_connection_with_shared_factory_and_pipeline_config(
conn,
Arc::new(receiver_factory),
cache_config,
pipeline_config,
)
.await
}
async fn from_connection_with_shared_factory_and_pipeline_config(
conn: RedisConnection,
receiver_factory: Arc<dyn ConnectionFactory>,
cache_config: CachedClientConfig,
pipeline_config: AutoPipelineConfig,
) -> Result<Self, RedisError> {
let conn = force_resp3(conn).await?;
let pipeline = AutoPipelineService::new(conn, pipeline_config);
let cache = CacheService::with_tracking(pipeline, receiver_factory, &cache_config).await?;
Ok(Self {
inner: MultiplexedClient::from_layered(cache),
})
}
pub fn queue_depth(&self) -> usize {
self.inner.inner.inner().queue_depth()
}
pub async fn cache_size(&self) -> usize {
self.inner.inner.inner().cache_size().await
}
pub async fn clear_cache(&self) {
self.inner.inner.inner().clear_cache().await;
}
pub async fn is_caching_healthy(&self) -> bool {
self.inner.inner.inner().is_caching_healthy().await
}
pub async fn cache_statistics(&self) -> CacheStatistics {
self.inner.inner.inner().cache_statistics().await
}
pub async fn execute<Cmd: Command>(&self, cmd: Cmd) -> Result<Cmd::Response, RedisError> {
self.inner.execute(cmd).await
}
pub async fn health_check(&self) -> Result<(), RedisError> {
self.inner.health_check().await
}
pub fn with_circuit_breaker(
self,
config: RedisCircuitBreakerConfig,
) -> RedisCircuitBreakerClient<CacheService<AutoPipelineService>> {
self.inner.with_circuit_breaker(config)
}
pub fn retry(&self, policy: RetryPolicy) -> RetryClient<Self> {
RetryClient::new(self.clone(), policy)
}
pub async fn shutdown(self) {
self.inner.inner.into_inner().shutdown().await;
}
}
impl<S> MultiplexedClient<S>
where
S: Service<Frame, Response = Frame, Error = RedisError> + Clone,
S::Future: Send + 'static,
{
pub fn with_circuit_breaker(
self,
config: RedisCircuitBreakerConfig,
) -> RedisCircuitBreakerClient<S> {
RedisCircuitBreakerClient::new(self.inner.into_inner(), config)
}
pub fn from_layered(service: S) -> Self {
Self {
inner: CommandAdapter::new(service),
}
}
pub async fn execute<Cmd: Command>(&self, cmd: Cmd) -> Result<Cmd::Response, RedisError> {
let deadline = cmd.deadline();
let mut svc = self.inner.clone();
let operation = async move {
std::future::poll_fn(|cx| {
<CommandAdapter<S> as Service<Cmd>>::poll_ready(&mut svc, cx)
})
.await?;
Service::call(&mut svc, cmd).await
};
match deadline {
Some(deadline) => tokio::time::timeout_at(deadline, operation)
.await
.map_err(|_elapsed| RedisError::CommandTimeout)?,
None => operation.await,
}
}
pub async fn health_check(&self) -> Result<(), RedisError> {
self.execute(Ping::new()).await?;
Ok(())
}
}
impl PipelineExecutor for MultiplexedClient<AutoPipelineService> {
fn execute_pipeline(
&mut self,
frames: Vec<Frame>,
) -> impl Future<Output = Result<Vec<Frame>, RedisError>> + Send {
let mut svc = self.inner.clone().into_inner();
async move { svc.call_pipeline(frames).await }
}
}
impl PipelineExecutor for CachedMultiplexedClient {
fn execute_pipeline(
&mut self,
frames: Vec<Frame>,
) -> impl Future<Output = Result<Vec<Frame>, RedisError>> + Send {
let mut svc = self.inner.inner.clone().into_inner();
async move { svc.call_pipeline(frames).await }
}
}
impl TransactionExecutor for MultiplexedClient<AutoPipelineService> {
const SUPPORTS_TRANSACTION_RETRY: bool = false;
fn execute_transaction(
&mut self,
watch_frames: Vec<Frame>,
command_frames: Vec<Frame>,
) -> impl Future<Output = Result<Option<Vec<Frame>>, RedisError>> + Send {
let mut frames = watch_frames;
frames.push(array(vec![bulk("MULTI")]));
frames.extend(command_frames);
frames.push(array(vec![bulk("EXEC")]));
let mut svc = self.inner.clone().into_inner();
async move {
let mut responses = svc.call_pipeline(frames).await?;
let exec = responses.pop().ok_or(RedisError::UnexpectedResponse {
expected: "EXEC response",
actual: "empty pipeline response".to_string(),
})?;
match exec {
Frame::Array(Some(results)) => Ok(Some(results)),
Frame::Array(None) | Frame::Null => Ok(None),
Frame::Error(e) => Err(RedisError::Redis(String::from_utf8_lossy(&e).into_owned())),
other => Err(RedisError::UnexpectedResponse {
expected: "array or null",
actual: format!("{other:?}"),
}),
}
}
}
}
impl TransactionExecutor for CachedMultiplexedClient {
const SUPPORTS_TRANSACTION_RETRY: bool = false;
fn execute_transaction(
&mut self,
watch_frames: Vec<Frame>,
command_frames: Vec<Frame>,
) -> impl Future<Output = Result<Option<Vec<Frame>>, RedisError>> + Send {
let mut frames = watch_frames;
frames.push(array(vec![bulk("MULTI")]));
frames.extend(command_frames);
frames.push(array(vec![bulk("EXEC")]));
let mut svc = self.inner.inner.clone().into_inner();
async move {
let mut responses = svc.call_pipeline(frames).await?;
let exec = responses.pop().ok_or(RedisError::UnexpectedResponse {
expected: "EXEC response",
actual: "empty pipeline response".to_string(),
})?;
match exec {
Frame::Array(Some(results)) => Ok(Some(results)),
Frame::Array(None) | Frame::Null => Ok(None),
Frame::Error(e) => Err(RedisError::Redis(String::from_utf8_lossy(&e).into_owned())),
other => Err(RedisError::UnexpectedResponse {
expected: "array or null",
actual: format!("{other:?}"),
}),
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use bytes::Bytes;
use redis_tower_commands::Get;
use redis_tower_core::WithDeadline;
use std::collections::VecDeque;
use std::sync::Arc;
use std::sync::Mutex as StdMutex;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::task::{Context, Poll};
use std::time::Duration;
use tower_layer::Layer;
#[derive(Clone)]
struct MockFrameService {
reply: Frame,
}
impl Service<Frame> for MockFrameService {
type Response = Frame;
type Error = RedisError;
type Future = std::future::Ready<Result<Frame, RedisError>>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), RedisError>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, _req: Frame) -> Self::Future {
std::future::ready(Ok(self.reply.clone()))
}
}
#[derive(Clone)]
struct NeverReadyFrameService {
calls: Arc<AtomicUsize>,
}
impl Service<Frame> for NeverReadyFrameService {
type Response = Frame;
type Error = RedisError;
type Future = std::future::Ready<Result<Frame, RedisError>>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), RedisError>> {
Poll::Pending
}
fn call(&mut self, _req: Frame) -> Self::Future {
self.calls.fetch_add(1, Ordering::Relaxed);
std::future::ready(Ok(Frame::Null))
}
}
#[test]
fn multiplexed_client_is_transaction_executor() {
fn assert_txn_executor<T: TransactionExecutor>() {}
assert_txn_executor::<MultiplexedClient>();
}
#[test]
fn multiplexed_client_is_pipeline_executor() {
fn assert_pipeline_executor<T: PipelineExecutor>() {}
assert_pipeline_executor::<MultiplexedClient>();
}
#[test]
fn cached_multiplexed_client_preserves_shared_executor_surface() {
fn assert_clone_send_sync<T: Clone + Send + Sync>() {}
fn assert_pipeline_executor<T: PipelineExecutor>() {}
fn assert_transaction_executor<T: TransactionExecutor>() {}
fn assert_redis_executor<T: crate::RedisExecutor>() {}
assert_clone_send_sync::<CachedMultiplexedClient>();
assert_pipeline_executor::<CachedMultiplexedClient>();
assert_transaction_executor::<CachedMultiplexedClient>();
assert_redis_executor::<CachedMultiplexedClient>();
}
#[cfg(unix)]
struct QueuedConnectionFactory {
connections: StdMutex<VecDeque<RedisConnection>>,
calls: Arc<AtomicUsize>,
}
#[cfg(unix)]
impl ConnectionFactory for QueuedConnectionFactory {
fn connect(
&self,
) -> std::pin::Pin<Box<dyn Future<Output = Result<RedisConnection, RedisError>> + Send>>
{
self.calls.fetch_add(1, Ordering::SeqCst);
let connection = self
.connections
.lock()
.unwrap()
.pop_front()
.ok_or(RedisError::ConnectionClosed);
Box::pin(async move { connection })
}
}
#[cfg(unix)]
#[tokio::test]
async fn cached_factory_shares_setup_and_forces_resp3_on_both_connections() {
use futures::{SinkExt, StreamExt};
use tokio_util::codec::Framed;
let (data_client, data_server) = tokio::net::UnixStream::pair().unwrap();
let (receiver_client, receiver_server) = tokio::net::UnixStream::pair().unwrap();
let data_connection =
RedisConnection::from_stream(redis_tower_core::RedisStream::Unix(data_client));
let receiver_connection =
RedisConnection::from_stream(redis_tower_core::RedisStream::Unix(receiver_client));
let calls = Arc::new(AtomicUsize::new(0));
let factory: Arc<dyn ConnectionFactory> = Arc::new(QueuedConnectionFactory {
connections: StdMutex::new(VecDeque::from([data_connection, receiver_connection])),
calls: Arc::clone(&calls),
});
let data_task = tokio::spawn(async move {
let mut framed = Framed::new(
redis_tower_core::RedisStream::Unix(data_server),
redis_tower_core::RespCodec::new(),
);
assert_eq!(
framed.next().await.unwrap().unwrap(),
array(vec![bulk("HELLO"), bulk("3")])
);
framed
.send(Frame::SimpleString(Bytes::from_static(b"OK")))
.await
.unwrap();
assert_eq!(
framed.next().await.unwrap().unwrap(),
array(vec![bulk("CLIENT"), bulk("TRACKING"), bulk("OFF")])
);
assert_eq!(
framed.next().await.unwrap().unwrap(),
array(vec![
bulk("CLIENT"),
bulk("TRACKING"),
bulk("ON"),
bulk("REDIRECT"),
bulk("42"),
bulk("BCAST"),
bulk("NOLOOP"),
])
);
framed
.send(Frame::SimpleString(Bytes::from_static(b"OK")))
.await
.unwrap();
framed
.send(Frame::SimpleString(Bytes::from_static(b"OK")))
.await
.unwrap();
futures::future::pending::<()>().await;
});
let receiver_task = tokio::spawn(async move {
let mut framed = Framed::new(
redis_tower_core::RedisStream::Unix(receiver_server),
redis_tower_core::RespCodec::new(),
);
assert_eq!(
framed.next().await.unwrap().unwrap(),
array(vec![bulk("HELLO"), bulk("3")])
);
framed
.send(Frame::SimpleString(Bytes::from_static(b"OK")))
.await
.unwrap();
assert_eq!(
framed.next().await.unwrap().unwrap(),
array(vec![bulk("CLIENT"), bulk("ID")])
);
framed.send(Frame::Integer(42)).await.unwrap();
futures::future::pending::<()>().await;
});
let client = CachedMultiplexedClient::from_factory(factory, CachedClientConfig::default())
.await
.unwrap();
assert_eq!(calls.load(Ordering::SeqCst), 2);
assert!(client.is_caching_healthy().await);
client.shutdown().await;
data_task.abort();
receiver_task.abort();
}
#[cfg(unix)]
#[tokio::test]
async fn pipeline_executor_preserves_binary_frames_and_response_order() {
use futures::{SinkExt, StreamExt};
use tokio_util::codec::Framed;
let (client_stream, server_stream) = tokio::net::UnixStream::pair().unwrap();
let conn = RedisConnection::from_stream(redis_tower_core::RedisStream::Unix(client_stream));
let mut client = MultiplexedClient::from_connection(conn);
let binary_key = Bytes::from_static(b"key:\0\xff");
let binary_value = Bytes::from_static(b"value:\0\x80\xff");
let requests = vec![
array(vec![
bulk("SET"),
Frame::BulkString(Some(binary_key.clone())),
Frame::BulkString(Some(binary_value.clone())),
]),
array(vec![
bulk("GET"),
Frame::BulkString(Some(binary_key.clone())),
]),
];
let expected_requests = requests.clone();
let responses = vec![
Frame::SimpleString(Bytes::from_static(b"OK")),
Frame::BulkString(Some(binary_value)),
];
let expected_responses = responses.clone();
let server = tokio::spawn(async move {
let mut framed = Framed::new(
redis_tower_core::RedisStream::Unix(server_stream),
redis_tower_core::RespCodec::new(),
);
for expected in expected_requests {
assert_eq!(framed.next().await.unwrap().unwrap(), expected);
}
for response in responses {
framed.send(response).await.unwrap();
}
});
let actual = PipelineExecutor::execute_pipeline(&mut client, requests)
.await
.unwrap();
assert_eq!(actual, expected_responses);
server.await.unwrap();
client.shutdown().await;
}
#[cfg(unix)]
#[tokio::test]
async fn cancelled_pipeline_executor_request_is_pruned_before_wire() {
use tokio::io::AsyncReadExt;
let (client_stream, mut server_stream) = tokio::net::UnixStream::pair().unwrap();
let conn = RedisConnection::from_stream(redis_tower_core::RedisStream::Unix(client_stream));
let batch_window = Duration::from_millis(200);
let mut client = MultiplexedClient::from_connection_with_config(
conn,
AutoPipelineConfig {
batch_window,
..AutoPipelineConfig::default()
},
);
let request = array(vec![
bulk("SET"),
bulk("cancelled-pipeline"),
bulk("must-not-land"),
]);
assert!(
tokio::time::timeout(
Duration::from_millis(25),
PipelineExecutor::execute_pipeline(&mut client, vec![request]),
)
.await
.is_err(),
"pipeline unexpectedly completed without a server response"
);
let mut bytes = [0u8; 128];
assert!(
tokio::time::timeout(
batch_window + Duration::from_millis(100),
server_stream.read(&mut bytes),
)
.await
.is_err(),
"a cancelled multiplexed pipeline reached the Redis socket"
);
client.shutdown().await;
}
#[cfg(unix)]
#[tokio::test]
async fn cached_pipeline_and_transaction_writes_cannot_leave_stale_reads() {
use futures::{SinkExt, StreamExt};
use redis_tower_commands::Set;
use tokio_util::codec::Framed;
use crate::cache_layer::CacheConfig;
use crate::{Pipeline, Transaction};
let (client_stream, server_stream) = tokio::net::UnixStream::pair().unwrap();
let conn = RedisConnection::from_stream(redis_tower_core::RedisStream::Unix(client_stream));
let pipeline = AutoPipelineService::new(conn, AutoPipelineConfig::default());
let cache = CacheService::new(pipeline, CacheConfig::default());
let mut client = CachedMultiplexedClient {
inner: MultiplexedClient::from_layered(cache),
};
assert_eq!(client.queue_depth(), 0);
let get = array(vec![bulk("GET"), bulk("key")]);
let set_pipeline = array(vec![bulk("SET"), bulk("key"), bulk("pipeline")]);
let set_transaction = array(vec![bulk("SET"), bulk("key"), bulk("transaction")]);
let server = tokio::spawn(async move {
let mut framed = Framed::new(
redis_tower_core::RedisStream::Unix(server_stream),
redis_tower_core::RespCodec::new(),
);
assert_eq!(framed.next().await.unwrap().unwrap(), get);
framed
.send(Frame::BulkString(Some(Bytes::from_static(b"initial"))))
.await
.unwrap();
assert_eq!(framed.next().await.unwrap().unwrap(), set_pipeline);
framed
.send(Frame::SimpleString(Bytes::from_static(b"OK")))
.await
.unwrap();
assert_eq!(
framed.next().await.unwrap().unwrap(),
array(vec![bulk("GET"), bulk("key")])
);
framed
.send(Frame::BulkString(Some(Bytes::from_static(b"pipeline"))))
.await
.unwrap();
assert_eq!(
framed.next().await.unwrap().unwrap(),
array(vec![bulk("MULTI")])
);
assert_eq!(framed.next().await.unwrap().unwrap(), set_transaction);
assert_eq!(
framed.next().await.unwrap().unwrap(),
array(vec![bulk("EXEC")])
);
framed
.send(Frame::SimpleString(Bytes::from_static(b"OK")))
.await
.unwrap();
framed
.send(Frame::SimpleString(Bytes::from_static(b"QUEUED")))
.await
.unwrap();
framed
.send(Frame::Array(Some(vec![Frame::SimpleString(
Bytes::from_static(b"OK"),
)])))
.await
.unwrap();
assert_eq!(
framed.next().await.unwrap().unwrap(),
array(vec![bulk("GET"), bulk("key")])
);
framed
.send(Frame::BulkString(Some(Bytes::from_static(b"transaction"))))
.await
.unwrap();
});
let initial: Option<Bytes> = client.execute(Get::new("key")).await.unwrap();
assert_eq!(initial, Some(Bytes::from_static(b"initial")));
let cached: Option<Bytes> = client.execute(Get::new("key")).await.unwrap();
assert_eq!(cached, initial);
assert_eq!(
client.queue_depth(),
0,
"a cache hit must release its readiness reservation"
);
assert_eq!(client.cache_size().await, 1);
assert!(client.is_caching_healthy().await);
let stats = client.cache_statistics().await;
assert_eq!(stats.hits, 1);
assert_eq!(stats.misses, 1);
Pipeline::new()
.push(Set::new("key", "pipeline"))
.execute(&mut client)
.await
.unwrap();
let after_pipeline: Option<Bytes> = client.execute(Get::new("key")).await.unwrap();
assert_eq!(after_pipeline, Some(Bytes::from_static(b"pipeline")));
let _ = Transaction::new()
.push(Set::new("key", "transaction"))
.execute(&mut client)
.await
.unwrap();
let after_transaction: Option<Bytes> = client.execute(Get::new("key")).await.unwrap();
assert_eq!(after_transaction, Some(Bytes::from_static(b"transaction")));
server.await.unwrap();
client.clear_cache().await;
assert_eq!(client.cache_size().await, 0);
client.shutdown().await;
}
#[tokio::test]
async fn from_layered_routes_execute_through_injected_service() {
let inner = MockFrameService {
reply: Frame::BulkString(Some(Bytes::from("layered"))),
};
let client = MultiplexedClient::from_layered(inner);
let client2 = client.clone();
let val: Option<Bytes> = client2.execute(Get::new("k")).await.unwrap();
assert_eq!(val, Some(Bytes::from("layered")));
}
#[tokio::test]
async fn from_layered_composes_a_real_tower_layer() {
use crate::command_timeout::CommandTimeoutLayer;
let inner = CommandTimeoutLayer::new(Duration::from_secs(5)).layer(MockFrameService {
reply: Frame::BulkString(Some(Bytes::from("through-timeout"))),
});
let client = MultiplexedClient::from_layered(inner);
let val: Option<Bytes> = client.execute(Get::new("k")).await.unwrap();
assert_eq!(val, Some(Bytes::from("through-timeout")));
}
#[tokio::test]
async fn typed_deadline_bounds_readiness_above_frame_layers() {
let calls = Arc::new(AtomicUsize::new(0));
let client = MultiplexedClient::from_layered(NeverReadyFrameService {
calls: Arc::clone(&calls),
});
let result = client
.execute(WithDeadline::after(
Get::new("k"),
Duration::from_millis(20),
))
.await;
assert!(matches!(result, Err(RedisError::CommandTimeout)));
assert_eq!(calls.load(Ordering::Relaxed), 0);
}
}