dns.rs |
DNS Resolution used by the `HttpConnector`.
This module contains:
- A [`GaiResolver`](GaiResolver) that is the default resolver for the
`HttpConnector`.
- The `Name` type used as an argument to custom resolvers.
# Resolvers are `Service`s
A resolver is just a
`Service<Name, Response = impl Iterator<Item = SocketAddr>>`.
A simple resolver that ignores the name and always returns a specific
address:
```rust,ignore
use std::{convert::Infallible, iter, net::SocketAddr};
let resolver = tower::service_fn(|_name| async {
Ok::<_, Infallible>(iter::once(SocketAddr::from(([127, 0, 0, 1], 8080))))
});
``` |
12219 |
http.rs |
#[cfg(feature = "runtime")]
impl HttpConnector<TokioThreadpoolGaiResolver> {
/// Construct a new HttpConnector using the `TokioThreadpoolGaiResolver`.
///
/// This resolver **requires** the threadpool runtime to be used.
pub fn new_with_tokio_threadpool_resolver() -> Self {
HttpConnector::new_with_resolver(TokioThreadpoolGaiResolver::new())
}
}
|
32458 |
mod.rs |
Connectors used by the `Client`.
This module contains:
- A default [`HttpConnector`][] that does DNS resolution and establishes
connections over TCP.
- Types to build custom connectors.
# Connectors
A "connector" is a [`Service`][] that takes a [`Uri`][] destination, and
its `Response` is some type implementing [`AsyncRead`][], [`AsyncWrite`][],
and [`Connection`][].
## Custom Connectors
A simple connector that ignores the `Uri` destination and always returns
a TCP connection to the same address could be written like this:
```rust,ignore
let connector = tower::service_fn(|_dst| async {
tokio::net::TcpStream::connect("127.0.0.1:1337")
})
```
Or, fully written out:
```
# #[cfg(feature = "runtime")]
# mod rt {
use std::{future::Future, net::SocketAddr, pin::Pin, task::{self, Poll}};
use hyper::{service::Service, Uri};
use tokio::net::TcpStream;
#[derive(Clone)]
struct LocalConnector;
impl Service<Uri> for LocalConnector {
type Response = TcpStream;
type Error = std::io::Error;
// We can't "name" an `async` generated future.
type Future = Pin<Box<
dyn Future<Output = Result<Self::Response, Self::Error>> + Send
>>;
fn poll_ready(&mut self, _: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>> {
// This connector is always ready, but others might not be.
Poll::Ready(Ok(()))
}
fn call(&mut self, _: Uri) -> Self::Future {
Box::pin(TcpStream::connect(SocketAddr::from(([127, 0, 0, 1], 1337))))
}
}
# }
```
It's worth noting that for `TcpStream`s, the [`HttpConnector`][] is a
better starting place to extend from.
Using either of the above connector examples, it can be used with the
`Client` like this:
```
# #[cfg(feature = "runtime")]
# fn rt () {
# let connector = hyper::client::HttpConnector::new();
// let connector = ...
let client = hyper::Client::builder()
.build::<_, hyper::Body>(connector);
# }
```
[`HttpConnector`]: HttpConnector
[`Service`]: crate::service::Service
[`Uri`]: ::http::Uri
[`AsyncRead`]: tokio::io::AsyncRead
[`AsyncWrite`]: tokio::io::AsyncWrite
[`Connection`]: Connection |
11592 |