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;
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, Clone)]
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::other(join_failure))),
35                    Ok(Ok(dns_result)) => {
36                        Ok(dns_result.into_iter().map(|addr| addr.ip()).collect())
37                    }
38                    Ok(Err(dns_failure)) => Err(ResolveDnsError::new(dns_failure)),
39                }
40            })
41        }
42    }
43}
44
45#[cfg(all(feature = "rt-tokio", not(target_family = "wasm")))]
46pub use self::tokio::TokioDnsResolver;