aws_smithy_runtime/client/dns/
tokio.rs

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