redis/aio/
connection.rs

1use super::AsyncDNSResolver;
2use super::RedisRuntime;
3
4use crate::connection::{ConnectionAddr, ConnectionInfo};
5use crate::io::tcp::TcpSettings;
6#[cfg(feature = "aio")]
7use crate::types::RedisResult;
8
9use futures_util::future::select_ok;
10
11pub(crate) async fn connect_simple<T: RedisRuntime>(
12    connection_info: &ConnectionInfo,
13    dns_resolver: &dyn AsyncDNSResolver,
14    tcp_settings: &TcpSettings,
15) -> RedisResult<T> {
16    Ok(match connection_info.addr {
17        ConnectionAddr::Tcp(ref host, port) => {
18            let socket_addrs = dns_resolver.resolve(host, port).await?;
19            select_ok(socket_addrs.map(|addr| Box::pin(<T>::connect_tcp(addr, tcp_settings))))
20                .await?
21                .0
22        }
23
24        #[cfg(any(feature = "tls-native-tls", feature = "tls-rustls"))]
25        ConnectionAddr::TcpTls {
26            ref host,
27            port,
28            insecure,
29            ref tls_params,
30        } => {
31            let socket_addrs = dns_resolver.resolve(host, port).await?;
32            select_ok(socket_addrs.map(|socket_addr| {
33                Box::pin(<T>::connect_tcp_tls(
34                    host,
35                    socket_addr,
36                    insecure,
37                    tls_params,
38                    tcp_settings,
39                ))
40            }))
41            .await?
42            .0
43        }
44
45        #[cfg(not(any(feature = "tls-native-tls", feature = "tls-rustls")))]
46        ConnectionAddr::TcpTls { .. } => {
47            fail!((
48                crate::types::ErrorKind::InvalidClientConfig,
49                "Cannot connect to TCP with TLS without the tls feature"
50            ));
51        }
52
53        #[cfg(unix)]
54        ConnectionAddr::Unix(ref path) => <T>::connect_unix(path).await?,
55
56        #[cfg(not(unix))]
57        ConnectionAddr::Unix(_) => {
58            fail!((
59                crate::types::ErrorKind::InvalidClientConfig,
60                "Cannot connect to unix sockets \
61                 on this platform",
62            ))
63        }
64    })
65}
66
67#[cfg(test)]
68mod tests {
69    #[cfg(feature = "cluster-async")]
70    use crate::cluster_async;
71
72    use super::super::*;
73
74    #[test]
75    fn test_is_sync() {
76        const fn assert_sync<T: Sync>() {}
77
78        assert_sync::<MultiplexedConnection>();
79        assert_sync::<PubSub>();
80        assert_sync::<Monitor>();
81        #[cfg(feature = "connection-manager")]
82        assert_sync::<ConnectionManager>();
83        #[cfg(feature = "cluster-async")]
84        assert_sync::<cluster_async::ClusterConnection>();
85    }
86
87    #[test]
88    fn test_is_send() {
89        const fn assert_send<T: Send>() {}
90
91        assert_send::<MultiplexedConnection>();
92        assert_send::<PubSub>();
93        assert_send::<Monitor>();
94        #[cfg(feature = "connection-manager")]
95        assert_send::<ConnectionManager>();
96        #[cfg(feature = "cluster-async")]
97        assert_send::<cluster_async::ClusterConnection>();
98    }
99}