add TokioAsyncResolver::tokio apis for ease of use

This commit is contained in:
Benjamin Fry
2020-01-11 06:03:01 -08:00
parent d7a5617d2b
commit cac8ffe6a6
5 changed files with 213 additions and 172 deletions

View File

@@ -16,7 +16,6 @@ use proto::error::ProtoResult;
use proto::op::Query;
use proto::rr::domain::TryParseIp;
use proto::rr::{IntoName, Name, Record, RecordType};
use proto::tcp::Connect;
use proto::xfer::{DnsRequestOptions, RetryDnsHandle};
use proto::DnsHandle;
@@ -34,8 +33,7 @@ use crate::name_server::{
use crate::name_server::{TokioConnection, TokioConnectionProvider};
use crate::Hosts;
// TODO: Consider renaming to ResolverAsync
/// A handle for resolving DNS records.
/// An asynchronous resolver for DNS generic over async Runtimes.
///
/// Creating a `AsyncResolver` returns a new handle and a future that should
/// be spawned on an executor to drive the background work. The lookup methods
@@ -106,10 +104,40 @@ pub async fn $p(&self, query: $t) -> Result<$l, ResolveError> {
};
}
impl<R: RuntimeProvider> AsyncResolver<GenericConnection, GenericConnectionProvider<R>>
where
<<R as RuntimeProvider>::Tcp as Connect>::Transport: Unpin,
{
#[cfg(feature = "tokio-runtime")]
impl TokioAsyncResolver {
/// Construct a new Tokio based `AsyncResolver` with the provided configuration.
///
/// # Arguments
///
/// * `config` - configuration, name_servers, etc. for the Resolver
/// * `options` - basic lookup options for the resolver
///
/// # Returns
///
/// A tuple containing the new `AsyncResolver` and a future that drives the
/// background task that runs resolutions for the `AsyncResolver`. See the
/// documentation for `AsyncResolver` for more information on how to use
/// the background future.
pub async fn tokio(
config: ResolverConfig,
options: ResolverOpts,
) -> Result<Self, ResolveError> {
use tokio::runtime::Handle;
Self::new(config, options, Handle::current()).await
}
/// Constructs a new Tokio based Resolver with the system configuration.
///
/// This will use `/etc/resolv.conf` on Unix OSes and the registry on Windows.
#[cfg(any(unix, target_os = "windows"))]
pub async fn tokio_from_system_conf() -> Result<Self, ResolveError> {
use tokio::runtime::Handle;
Self::from_system_conf(Handle::current()).await
}
}
impl<R: RuntimeProvider> AsyncResolver<GenericConnection, GenericConnectionProvider<R>> {
/// Construct a new `AsyncResolver` with the provided configuration.
///
/// # Arguments
@@ -180,10 +208,7 @@ impl<C: DnsHandle, P: ConnectionProvider<Conn = C>> AsyncResolver<C, P> {
///
/// # Returns
///
/// A tuple containing the new `AsyncResolver` and a future that drives the
/// background task that runs resolutions for the `AsyncResolver`. See the
/// documentation for `AsyncResolver` for more information on how to use
/// the background future.
/// A new `AsyncResolver` that should be used for resolutions, or an error.
pub(crate) async fn with_cache_with_provider(
config: ResolverConfig,
options: ResolverOpts,