-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconnection.rs
114 lines (103 loc) · 3.69 KB
/
connection.rs
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use crate::{
client::{Config, PreparedCommand, ServerConfig},
commands::InternalPubSubCommands,
resp::{Command, RespBuf},
ClusterConnection, Error, Future, Result, RetryReason, SentinelConnection,
StandaloneConnection,
};
use serde::de::DeserializeOwned;
use smallvec::SmallVec;
use std::future::IntoFuture;
#[allow(clippy::large_enum_variant)]
pub enum Connection {
Standalone(StandaloneConnection),
Sentinel(SentinelConnection),
Cluster(ClusterConnection),
}
impl Connection {
#[inline]
pub async fn connect(config: Config) -> Result<Self> {
match &config.server {
ServerConfig::Standalone { host, port } => Ok(Connection::Standalone(
StandaloneConnection::connect(host, *port, &config).await?,
)),
ServerConfig::Sentinel(sentinel_config) => Ok(Connection::Sentinel(
SentinelConnection::connect(sentinel_config, &config).await?,
)),
ServerConfig::Cluster(cluster_config) => Ok(Connection::Cluster(
ClusterConnection::connect(cluster_config, &config).await?,
)),
}
}
#[inline]
pub async fn write(&mut self, command: &Command) -> Result<()> {
match self {
Connection::Standalone(connection) => connection.write(command).await,
Connection::Sentinel(connection) => connection.write(command).await,
Connection::Cluster(connection) => connection.write(command).await,
}
}
#[inline]
pub async fn write_batch(
&mut self,
commands: SmallVec<[&mut Command; 10]>,
retry_reasons: &[RetryReason],
) -> Result<()> {
match self {
Connection::Standalone(connection) => {
connection.write_batch(commands, retry_reasons).await
}
Connection::Sentinel(connection) => {
connection.write_batch(commands, retry_reasons).await
}
Connection::Cluster(connection) => {
connection.write_batch(commands, retry_reasons).await
}
}
}
#[inline]
pub async fn read(&mut self) -> Option<Result<RespBuf>> {
match self {
Connection::Standalone(connection) => connection.read().await,
Connection::Sentinel(connection) => connection.read().await,
Connection::Cluster(connection) => connection.read().await,
}
}
#[inline]
pub async fn reconnect(&mut self) -> Result<()> {
match self {
Connection::Standalone(connection) => connection.reconnect().await,
Connection::Sentinel(connection) => connection.reconnect().await,
Connection::Cluster(connection) => connection.reconnect().await,
}
}
#[inline]
pub async fn send(&mut self, command: &Command) -> Result<RespBuf> {
self.write(command).await?;
self.read()
.await
.ok_or_else(|| Error::Client("Disconnected by peer".to_owned()))?
}
pub(crate) fn tag(&self) -> &str {
match self {
Connection::Standalone(connection) => connection.tag(),
Connection::Sentinel(connection) => connection.tag(),
Connection::Cluster(connection) => connection.tag(),
}
}
}
impl<'a, R> IntoFuture for PreparedCommand<'a, &'a mut Connection, R>
where
R: DeserializeOwned + Send + 'a,
{
type Output = Result<R>;
type IntoFuture = Future<'a, R>;
#[inline]
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
let result = self.executor.send(&self.command).await?;
result.to()
})
}
}
impl<'a> InternalPubSubCommands<'a> for &'a mut Connection {}