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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use std::time::Duration;
#[cfg(feature = "aio")]
use std::pin::Pin;
use crate::{
connection::{connect, Connection, ConnectionInfo, ConnectionLike, IntoConnectionInfo},
types::{RedisResult, Value},
};
#[derive(Debug, Clone)]
pub struct Client {
connection_info: ConnectionInfo,
}
impl Client {
pub fn open<T: IntoConnectionInfo>(params: T) -> RedisResult<Client> {
Ok(Client {
connection_info: params.into_connection_info()?,
})
}
pub fn get_connection(&self) -> RedisResult<Connection> {
Ok(connect(&self.connection_info, None)?)
}
pub fn get_connection_with_timeout(&self, timeout: Duration) -> RedisResult<Connection> {
Ok(connect(&self.connection_info, Some(timeout))?)
}
pub fn get_connection_info(&self) -> &ConnectionInfo {
&self.connection_info
}
}
#[cfg(feature = "aio")]
#[cfg_attr(docsrs, doc(cfg(feature = "aio")))]
impl Client {
#[cfg(any(feature = "tokio-comp", feature = "async-std-comp"))]
pub async fn get_async_connection(&self) -> RedisResult<crate::aio::Connection> {
let con = match Runtime::locate() {
#[cfg(feature = "tokio-comp")]
Runtime::Tokio => {
self.get_simple_async_connection::<crate::aio::tokio::Tokio>()
.await?
}
#[cfg(feature = "async-std-comp")]
Runtime::AsyncStd => {
self.get_simple_async_connection::<crate::aio::async_std::AsyncStd>()
.await?
}
};
crate::aio::Connection::new(&self.connection_info, con).await
}
#[cfg(feature = "tokio-comp")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-comp")))]
pub async fn get_tokio_connection(&self) -> RedisResult<crate::aio::Connection> {
use crate::aio::RedisRuntime;
Ok(
crate::aio::connect::<crate::aio::tokio::Tokio>(&self.connection_info)
.await?
.map(RedisRuntime::boxed),
)
}
#[cfg(feature = "async-std-comp")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-std-comp")))]
pub async fn get_async_std_connection(&self) -> RedisResult<crate::aio::Connection> {
use crate::aio::RedisRuntime;
Ok(
crate::aio::connect::<crate::aio::async_std::AsyncStd>(&self.connection_info)
.await?
.map(RedisRuntime::boxed),
)
}
#[cfg(any(feature = "tokio-comp", feature = "async-std-comp"))]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "tokio-comp", feature = "async-std-comp")))
)]
pub async fn get_multiplexed_async_connection(
&self,
) -> RedisResult<crate::aio::MultiplexedConnection> {
match Runtime::locate() {
#[cfg(feature = "tokio-comp")]
Runtime::Tokio => self.get_multiplexed_tokio_connection().await,
#[cfg(feature = "async-std-comp")]
Runtime::AsyncStd => self.get_multiplexed_async_std_connection().await,
}
}
#[cfg(feature = "tokio-comp")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-comp")))]
pub async fn get_multiplexed_tokio_connection(
&self,
) -> RedisResult<crate::aio::MultiplexedConnection> {
self.get_multiplexed_async_connection_inner::<crate::aio::tokio::Tokio>()
.await
}
#[cfg(feature = "async-std-comp")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-std-comp")))]
pub async fn get_multiplexed_async_std_connection(
&self,
) -> RedisResult<crate::aio::MultiplexedConnection> {
self.get_multiplexed_async_connection_inner::<crate::aio::async_std::AsyncStd>()
.await
}
#[cfg(feature = "tokio-comp")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-comp")))]
pub async fn create_multiplexed_tokio_connection(
&self,
) -> RedisResult<(
crate::aio::MultiplexedConnection,
impl std::future::Future<Output = ()>,
)> {
self.create_multiplexed_async_connection_inner::<crate::aio::tokio::Tokio>()
.await
}
#[cfg(feature = "async-std-comp")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-std-comp")))]
pub async fn create_multiplexed_async_std_connection(
&self,
) -> RedisResult<(
crate::aio::MultiplexedConnection,
impl std::future::Future<Output = ()>,
)> {
self.create_multiplexed_async_connection_inner::<crate::aio::async_std::AsyncStd>()
.await
}
#[cfg(feature = "connection-manager")]
#[cfg_attr(docsrs, doc(cfg(feature = "connection-manager")))]
pub async fn get_tokio_connection_manager(&self) -> RedisResult<crate::aio::ConnectionManager> {
Ok(crate::aio::ConnectionManager::new(self.clone()).await?)
}
async fn get_multiplexed_async_connection_inner<T>(
&self,
) -> RedisResult<crate::aio::MultiplexedConnection>
where
T: crate::aio::RedisRuntime,
{
let (connection, driver) = self
.create_multiplexed_async_connection_inner::<T>()
.await?;
T::spawn(driver);
Ok(connection)
}
async fn create_multiplexed_async_connection_inner<T>(
&self,
) -> RedisResult<(
crate::aio::MultiplexedConnection,
impl std::future::Future<Output = ()>,
)>
where
T: crate::aio::RedisRuntime,
{
let con = self.get_simple_async_connection::<T>().await?;
crate::aio::MultiplexedConnection::new(&self.connection_info, con).await
}
async fn get_simple_async_connection<T>(
&self,
) -> RedisResult<Pin<Box<dyn crate::aio::AsyncStream + Send + Sync>>>
where
T: crate::aio::RedisRuntime,
{
Ok(crate::aio::connect_simple::<T>(&self.connection_info)
.await?
.boxed())
}
#[cfg(feature = "connection-manager")]
pub(crate) fn connection_info(&self) -> &ConnectionInfo {
&self.connection_info
}
}
#[cfg(feature = "aio")]
use crate::aio::Runtime;
impl ConnectionLike for Client {
fn req_packed_command(&mut self, cmd: &[u8]) -> RedisResult<Value> {
self.get_connection()?.req_packed_command(cmd)
}
fn req_packed_commands(
&mut self,
cmd: &[u8],
offset: usize,
count: usize,
) -> RedisResult<Vec<Value>> {
self.get_connection()?
.req_packed_commands(cmd, offset, count)
}
fn get_db(&self) -> i64 {
self.connection_info.db
}
fn check_connection(&mut self) -> bool {
if let Ok(mut conn) = self.get_connection() {
conn.check_connection()
} else {
false
}
}
fn is_open(&self) -> bool {
if let Ok(conn) = self.get_connection() {
conn.is_open()
} else {
false
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn regression_293_parse_ipv6_with_interface() {
assert!(Client::open(("fe80::cafe:beef%eno1", 6379)).is_ok());
}
}