aws_smithy_runtime/client/
dns.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Built-in DNS resolver implementations.
7
8#[cfg(all(feature = "rt-tokio", not(target_family = "wasm")))]
9mod tokio {
10    use aws_smithy_runtime_api::client::dns::{DnsFuture, ResolveDns, ResolveDnsError};
11    use std::io::{Error as IoError, ErrorKind as IoErrorKind};
12    use std::net::ToSocketAddrs;
13
14    /// DNS resolver that uses `tokio::spawn_blocking` to resolve DNS using the standard library.
15    ///
16    /// This implementation isn't available for WASM targets.
17    #[non_exhaustive]
18    #[derive(Debug, Default)]
19    pub struct TokioDnsResolver;
20
21    impl TokioDnsResolver {
22        /// Creates a new Tokio DNS resolver
23        pub fn new() -> Self {
24            Self
25        }
26    }
27
28    impl ResolveDns for TokioDnsResolver {
29        fn resolve_dns<'a>(&'a self, name: &'a str) -> DnsFuture<'a> {
30            let name = name.to_string();
31            DnsFuture::new(async move {
32                let result = tokio::task::spawn_blocking(move || (name, 0).to_socket_addrs()).await;
33                match result {
34                    Err(join_failure) => Err(ResolveDnsError::new(IoError::new(
35                        IoErrorKind::Other,
36                        join_failure,
37                    ))),
38                    Ok(Ok(dns_result)) => {
39                        Ok(dns_result.into_iter().map(|addr| addr.ip()).collect())
40                    }
41                    Ok(Err(dns_failure)) => Err(ResolveDnsError::new(dns_failure)),
42                }
43            })
44        }
45    }
46}
47
48#[cfg(all(feature = "rt-tokio", not(target_family = "wasm")))]
49pub use self::tokio::TokioDnsResolver;