redis/aio/
connection.rs

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