-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstandalone_connection.rs
245 lines (209 loc) · 7.8 KB
/
standalone_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use crate::{
client::{Config, PreparedCommand},
commands::{
ClusterCommands, ConnectionCommands, HelloOptions, SentinelCommands, ServerCommands,
},
resp::{BufferDecoder, Command, CommandEncoder, RespBuf},
tcp_connect, Error, Future, Result, RetryReason, TcpStreamReader, TcpStreamWriter,
};
#[cfg(feature = "tls")]
use crate::{tcp_tls_connect, TcpTlsStreamReader, TcpTlsStreamWriter};
use bytes::BytesMut;
use futures_util::{SinkExt, StreamExt};
use log::{debug, log_enabled, Level};
use serde::de::DeserializeOwned;
use smallvec::SmallVec;
use std::future::IntoFuture;
use tokio::io::AsyncWriteExt;
use tokio_util::codec::{Encoder, FramedRead, FramedWrite};
pub(crate) enum Streams {
Tcp(
FramedRead<TcpStreamReader, BufferDecoder>,
FramedWrite<TcpStreamWriter, CommandEncoder>,
),
#[cfg(feature = "tls")]
TcpTls(
FramedRead<TcpTlsStreamReader, BufferDecoder>,
FramedWrite<TcpTlsStreamWriter, CommandEncoder>,
),
}
impl Streams {
pub async fn connect(host: &str, port: u16, config: &Config) -> Result<Self> {
#[cfg(feature = "tls")]
if let Some(tls_config) = &config.tls_config {
let (reader, writer) =
tcp_tls_connect(host, port, tls_config, config.connect_timeout).await?;
let framed_read = FramedRead::new(reader, BufferDecoder);
let framed_write = FramedWrite::new(writer, CommandEncoder);
Ok(Streams::TcpTls(framed_read, framed_write))
} else {
Self::connect_non_secure(host, port, config).await
}
#[cfg(not(feature = "tls"))]
Self::connect_non_secure(host, port, config).await
}
pub async fn connect_non_secure(host: &str, port: u16, config: &Config) -> Result<Self> {
let (reader, writer) = tcp_connect(host, port, config).await?;
let framed_read = FramedRead::new(reader, BufferDecoder);
let framed_write = FramedWrite::new(writer, CommandEncoder);
Ok(Streams::Tcp(framed_read, framed_write))
}
}
pub struct StandaloneConnection {
host: String,
port: u16,
config: Config,
streams: Streams,
buffer: BytesMut,
version: String,
tag: String,
}
impl StandaloneConnection {
pub async fn connect(host: &str, port: u16, config: &Config) -> Result<Self> {
let streams = Streams::connect(host, port, config).await?;
let mut connection = Self {
host: host.to_owned(),
port,
config: config.clone(),
streams,
buffer: BytesMut::new(),
version: String::new(),
tag: if config.connection_name.is_empty() {
format!("{}:{}", host, port)
} else {
format!("{}:{}:{}", config.connection_name, host, port)
},
};
connection.post_connect().await?;
Ok(connection)
}
pub async fn write(&mut self, command: &Command) -> Result<()> {
if log_enabled!(Level::Debug) {
debug!("[{}] Sending {command:?}", self.tag);
}
match &mut self.streams {
Streams::Tcp(_, framed_write) => framed_write.send(command).await,
#[cfg(feature = "tls")]
Streams::TcpTls(_, framed_write) => framed_write.send(command).await,
}
}
pub async fn write_batch(
&mut self,
commands: SmallVec<[&mut Command; 10]>,
_retry_reasons: &[RetryReason],
) -> Result<()> {
self.buffer.clear();
let command_encoder = match &mut self.streams {
Streams::Tcp(_, framed_write) => framed_write.encoder_mut(),
#[cfg(feature = "tls")]
Streams::TcpTls(_, framed_write) => framed_write.encoder_mut(),
};
#[cfg(debug_assertions)]
let mut kill_connection = false;
for command in commands {
if log_enabled!(Level::Debug) {
debug!("[{}] Sending {command:?}", self.tag);
}
#[cfg(debug_assertions)]
if command.kill_connection_on_write > 0 {
kill_connection = true;
command.kill_connection_on_write -= 1;
}
command_encoder.encode(command, &mut self.buffer)?;
}
#[cfg(debug_assertions)]
if kill_connection {
let client_id = self.client_id().await?;
let mut config = self.config.clone();
"killer".clone_into(&mut config.connection_name);
let mut connection =
StandaloneConnection::connect(&self.host, self.port, &config).await?;
connection
.client_kill(crate::commands::ClientKillOptions::default().id(client_id))
.await?;
}
match &mut self.streams {
Streams::Tcp(_, framed_write) => framed_write.get_mut().write_all(&self.buffer).await?,
#[cfg(feature = "tls")]
Streams::TcpTls(_, framed_write) => {
framed_write.get_mut().write_all(&self.buffer).await?
}
}
Ok(())
}
pub async fn read(&mut self) -> Option<Result<RespBuf>> {
if let Some(result) = match &mut self.streams {
Streams::Tcp(framed_read, _) => framed_read.next().await,
#[cfg(feature = "tls")]
Streams::TcpTls(framed_read, _) => framed_read.next().await,
} {
if log_enabled!(Level::Debug) {
match &result {
Ok(bytes) => debug!("[{}] Received result {bytes}", self.tag),
Err(err) => debug!("[{}] Received result {err:?}", self.tag),
}
}
Some(result)
} else {
debug!("[{}] Socked is closed", self.tag);
None
}
}
pub async fn reconnect(&mut self) -> Result<()> {
self.streams = Streams::connect(&self.host, self.port, &self.config).await?;
self.post_connect().await?;
Ok(())
// TODO improve reconnection strategy with multiple retries
}
async fn post_connect(&mut self) -> Result<()> {
// RESP3
let mut hello_options = HelloOptions::new(3);
// authentication
if let Some(ref password) = self.config.password {
hello_options = hello_options.auth(
match &self.config.username {
Some(username) => username.clone(),
None => "default".to_owned(),
},
password.clone(),
);
}
// connection name
if !self.config.connection_name.is_empty() {
hello_options = hello_options.set_name(self.config.connection_name.clone());
}
let hello_result = self.hello(hello_options).await?;
self.version = hello_result.version;
// select database
if self.config.database != 0 {
self.select(self.config.database).await?;
}
Ok(())
}
pub fn get_version(&self) -> &str {
&self.version
}
pub(crate) fn tag(&self) -> &str {
&self.tag
}
}
impl<'a, R> IntoFuture for PreparedCommand<'a, &'a mut StandaloneConnection, R>
where
R: DeserializeOwned + Send + 'a,
{
type Output = Result<R>;
type IntoFuture = Future<'a, R>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
self.executor.write(&self.command).await?;
let resp_buf = self.executor.read().await.ok_or_else(|| {
Error::Client(format!("[{}] disconnected by peer", self.executor.tag()))
})??;
resp_buf.to()
})
}
}
impl<'a> ClusterCommands<'a> for &'a mut StandaloneConnection {}
impl<'a> ConnectionCommands<'a> for &'a mut StandaloneConnection {}
impl<'a> SentinelCommands<'a> for &'a mut StandaloneConnection {}
impl<'a> ServerCommands<'a> for &'a mut StandaloneConnection {}