
* all tests updated for new background * background in Resolver now supported, wip * clean up client impls * client passing all tests * cleanup review comments, remove try_locks * remove mutexes on NameServers in NameServerPool * refactored ConnectionProvider * remove pinutils dependency * remove the now unused custom *LookupFutures * remove unused types * remove all deprecated interfaces from Client * rename ClientFuture to AsyncClient * all clippy warnings and SecureClient working * cleanup clippy warnnings * Everything working with Background removed * fix infinite loop * Client refactored without background * rebased onto tokio 0.2 * remove Clone and Option from background connects * cleanup from review * fix clippy warnings * spawn in ConnectionProvider * fix no-default-features test * only expose testing functions during testing * fix copyright typo * revert lookup_ip interface change * limit tokio features to least required set * rename tokio-compat to tokio-runtime * use async fns where possible on AsyncResolver * fix lifetime issue
76 lines
2.5 KiB
Rust
76 lines
2.5 KiB
Rust
#![recursion_limit = "128"]
|
|
|
|
//! This example shows how to create a resolver that uses the tokio multithreaded runtime. This is how
|
|
//! you might integrate the resolver into a more complex application.
|
|
|
|
extern crate env_logger;
|
|
extern crate futures;
|
|
extern crate tokio;
|
|
extern crate trust_dns_resolver;
|
|
|
|
#[cfg(feature = "tokio-runtime")]
|
|
fn main() {
|
|
use tokio::runtime::Runtime;
|
|
use trust_dns_resolver::AsyncResolver;
|
|
|
|
env_logger::init();
|
|
|
|
// Set up the standard tokio runtime (multithreaded by default).
|
|
let mut runtime = Runtime::new().expect("Failed to create runtime");
|
|
|
|
let resolver = {
|
|
// To make this independent, if targeting macOS, BSD, Linux, or Windows, we can use the system's configuration:
|
|
#[cfg(any(unix, windows))]
|
|
{
|
|
// use the system resolver configuration
|
|
AsyncResolver::from_system_conf(runtime.handle().clone())
|
|
}
|
|
|
|
// For other operating systems, we can use one of the preconfigured definitions
|
|
#[cfg(not(any(unix, windows)))]
|
|
{
|
|
// Directly reference the config types
|
|
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
|
|
|
// Get a new resolver with the google nameservers as the upstream recursive resolvers
|
|
AsyncResolver::new(
|
|
ResolverConfig::google(),
|
|
ResolverOpts::default(),
|
|
runtime.handle().clone(),
|
|
)
|
|
}
|
|
};
|
|
|
|
// The resolver background task needs to be created in the runtime so it can
|
|
// connect to the reactor.
|
|
let resolver = runtime
|
|
.block_on(resolver)
|
|
.expect("failed to create resolver");
|
|
|
|
// Create some futures representing name lookups.
|
|
let names = &["www.google.com", "www.reddit.com", "www.wikipedia.org"];
|
|
let mut futures = names
|
|
.iter()
|
|
.map(|name| (name, resolver.lookup_ip(*name)))
|
|
.collect::<Vec<_>>();
|
|
|
|
// Go through the list of resolution operations and wait for them to complete.
|
|
for (name, lookup) in futures.drain(..) {
|
|
let ips = runtime
|
|
.block_on(lookup)
|
|
.expect("Failed completing lookup future")
|
|
.iter()
|
|
.collect::<Vec<_>>();
|
|
println!("{} resolved to {:?}", name, ips);
|
|
}
|
|
|
|
// Drop the resolver, which means that the runtime will become idle.
|
|
drop(futures);
|
|
drop(resolver);
|
|
}
|
|
|
|
#[cfg(not(feature = "tokio-runtime"))]
|
|
fn main() {
|
|
println!("tokio-runtime feature must be enabled")
|
|
}
|