Skip to content

Commit

Permalink
http3 make request future sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2424 committed Jun 29, 2024
1 parent c4ebb07 commit 62279f8
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 63 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ tokio-socks = { version = "0.5.1", optional = true }
hickory-resolver = { version = "0.24", optional = true, features = ["tokio-runtime"] }

# HTTP/3 experimental support
h3 = { version = "0.0.5", optional = true }
h3-quinn = { version = "0.0.6", optional = true }
h3 = { git = "https://github.com/Ruben2424/h3.git", branch = "reqwest-sync", optional = true}
h3-quinn = {git = "https://github.com/Ruben2424/h3.git", branch = "reqwest-sync", optional = true}
quinn = { version = "0.11.1", default-features = false, features = ["rustls", "runtime-tokio"], optional = true }
slab = { version = "0.4.9", optional = true } # just to get minimal versions working with quinn
futures-channel = { version = "0.3", optional = true }
Expand Down
10 changes: 6 additions & 4 deletions src/async_impl/h3_client/connect.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use crate::async_impl::h3_client::dns::resolve;
use crate::dns::DynResolver;
use crate::dns::{resolve, DynResolver};
use crate::error::BoxError;
use bytes::Bytes;
use h3::client::SendRequest;
use h3_quinn::{Connection, OpenStreams};
use http::Uri;
use hyper_util::client::legacy::connect::dns::Name;
use quinn::crypto::rustls::QuicClientConfig;
use quinn::{ClientConfig, Endpoint, TransportConfig};
use std::net::{IpAddr, SocketAddr};
Expand Down Expand Up @@ -58,7 +56,11 @@ impl H3Connector {
// If the host is already an IP address, skip resolving.
vec![SocketAddr::new(addr, port)]
} else {
let addrs = resolve(&mut self.resolver, Name::from_str(host)?).await?;
let name = resolve::Name::from_str(host)?;


let addrs = self.resolver.resolver.resolve(name).await?;

let addrs = addrs.map(|mut addr| {
addr.set_port(port);
addr
Expand Down
43 changes: 0 additions & 43 deletions src/async_impl/h3_client/dns.rs

This file was deleted.

3 changes: 1 addition & 2 deletions src/async_impl/h3_client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![cfg(feature = "http3")]

pub(crate) mod connect;
pub(crate) mod dns;
mod pool;

use crate::async_impl::body::ResponseBody;
Expand Down Expand Up @@ -76,7 +75,7 @@ impl H3Client {
}

pub(crate) struct H3ResponseFuture {
inner: Pin<Box<dyn Future<Output = Result<Response<ResponseBody>, Error>> + Send>>,
inner: Pin<Box<dyn Future<Output = Result<Response<ResponseBody>, Error>> + Send + Sync>>,
}

impl Future for H3ResponseFuture {
Expand Down
2 changes: 1 addition & 1 deletion src/async_impl/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ impl RequestBuilder {
/// # Ok(())
/// # }
/// ```
pub fn send(self) -> impl Future<Output = Result<Response, crate::Error>> {
pub fn send(self) -> impl Future<Output = Result<Response, crate::Error>> + Sync {
match self.request {
Ok(req) => self.client.execute_request(req),
Err(err) => Pending::new_err(err),
Expand Down
6 changes: 3 additions & 3 deletions src/dns/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use std::task::{Context, Poll};
use crate::error::BoxError;

/// Alias for an `Iterator` trait object over `SocketAddr`.
pub type Addrs = Box<dyn Iterator<Item = SocketAddr> + Send>;
pub type Addrs = Box<dyn Iterator<Item = SocketAddr> + Send + Sync>;

/// Alias for the `Future` type returned by a DNS resolver.
pub type Resolving = Pin<Box<dyn Future<Output = Result<Addrs, BoxError>> + Send>>;
pub type Resolving = Pin<Box<dyn Future<Output = Result<Addrs, BoxError>> + Send + Sync>>;

/// Trait for customizing DNS resolution in reqwest.
pub trait Resolve: Send + Sync {
Expand Down Expand Up @@ -53,7 +53,7 @@ impl FromStr for Name {

#[derive(Clone)]
pub(crate) struct DynResolver {
resolver: Arc<dyn Resolve>,
pub(crate) resolver: Arc<dyn Resolve + Sync>,
}

impl DynResolver {
Expand Down
21 changes: 13 additions & 8 deletions tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,20 @@ async fn http3_request_full() {
});

let url = format!("https://{}/content-length", server.addr());
let res = reqwest::Client::builder()
let res_fut = reqwest::Client::builder()
.http3_prior_knowledge()
.danger_accept_invalid_certs(true)
.build()
.expect("client builder")
.post(url)
.version(http::Version::HTTP_3)
.body("hello")
.send()
.await
.expect("request");
.send();

fn assert_sync<T: Sync>(_: &T) {}
assert_sync(&res_fut);

let res = res_fut.await.expect("request");

assert_eq!(res.version(), http::Version::HTTP_3);
assert_eq!(res.status(), reqwest::StatusCode::OK);
Expand All @@ -121,14 +124,16 @@ async fn user_agent() {
});

let url = format!("http://{}/ua", server.addr());
let res = reqwest::Client::builder()
let res_fut = reqwest::Client::builder()
.user_agent("reqwest-test-agent")
.build()
.expect("client builder")
.get(&url)
.send()
.await
.expect("request");
.send();

fn assert_sync<T: Sync>(_: &T) {}
assert_sync(&res_fut);
let res = res_fut.await.expect("request");

assert_eq!(res.status(), reqwest::StatusCode::OK);
}
Expand Down

0 comments on commit 62279f8

Please sign in to comment.