Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ResponseFuture implement Sync #2653

Merged
merged 2 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use super::pool::{
#[cfg(feature = "tcp")]
use super::HttpConnector;
use crate::body::{Body, HttpBody};
use crate::common::{exec::BoxSendFuture, lazy as hyper_lazy, task, Future, Lazy, Pin, Poll};
use crate::common::{exec::BoxSendFuture, sync_wrapper::SyncWrapper, lazy as hyper_lazy, task, Future, Lazy, Pin, Poll};
use crate::rt::Executor;

/// A Client to make outgoing HTTP requests.
Expand All @@ -45,7 +45,7 @@ struct Config {
/// This is returned by `Client::request` (and `Client::get`).
#[must_use = "futures do nothing unless polled"]
pub struct ResponseFuture {
inner: Pin<Box<dyn Future<Output = crate::Result<Response<Body>>> + Send>>,
inner: SyncWrapper<Pin<Box<dyn Future<Output = crate::Result<Response<Body>>> + Send>>>,
}

// ===== impl Client =====
Expand Down Expand Up @@ -168,9 +168,9 @@ where
Version::HTTP_10 => {
if is_http_connect {
warn!("CONNECT is not allowed for HTTP/1.0");
return ResponseFuture::new(Box::pin(future::err(
return ResponseFuture::new(future::err(
crate::Error::new_user_unsupported_request_method(),
)));
));
}
}
Version::HTTP_2 => (),
Expand All @@ -181,11 +181,11 @@ where
let pool_key = match extract_domain(req.uri_mut(), is_http_connect) {
Ok(s) => s,
Err(err) => {
return ResponseFuture::new(Box::pin(future::err(err)));
return ResponseFuture::new(future::err(err));
}
};

ResponseFuture::new(Box::pin(self.clone().retryably_send_request(req, pool_key)))
ResponseFuture::new(self.clone().retryably_send_request(req, pool_key))
}

async fn retryably_send_request(
Expand Down Expand Up @@ -580,8 +580,13 @@ impl<C, B> fmt::Debug for Client<C, B> {
// ===== impl ResponseFuture =====

impl ResponseFuture {
fn new(fut: Pin<Box<dyn Future<Output = crate::Result<Response<Body>>> + Send>>) -> Self {
Self { inner: fut }
fn new<F>(value: F) -> Self
where
F: Future<Output = crate::Result<Response<Body>>> + Send + 'static,
{
Self {
inner: SyncWrapper::new(Box::pin(value))
}
}

fn error_version(ver: Version) -> Self {
Expand All @@ -602,7 +607,7 @@ impl Future for ResponseFuture {
type Output = crate::Result<Response<Body>>;

fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.inner).poll(cx)
self.inner.get_mut().as_mut().poll(cx)
}
}

Expand Down Expand Up @@ -1276,6 +1281,12 @@ impl fmt::Debug for Builder {
mod unit_tests {
use super::*;

#[test]
fn response_future_is_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<ResponseFuture>();
}

#[test]
fn set_relative_uri_with_implicit_path() {
let mut uri = "http://hyper.rs".parse().unwrap();
Expand Down
5 changes: 4 additions & 1 deletion src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ pub(crate) mod io;
#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))]
mod lazy;
mod never;
#[cfg(feature = "stream")]
#[cfg(any(
feature = "stream",
all(feature = "client", any(feature = "http1", feature = "http2"))
))]
pub(crate) mod sync_wrapper;
pub(crate) mod task;
pub(crate) mod watch;
Expand Down