Skip to content

Commit 8068aa0

Browse files
authoredJan 31, 2023
feat(client): http2 builder now requires an Executor (#3135)
This commit removes `common::Exec::Default` that just panics when used. We are removing `tokio`, leaving `Exec::Default` with no implementation and panics when `Exec::execute` is called. Since `Exec::Default` has no purpose, it is being removed and user should now provide an implementation of executor. Closes #3128 BREAKING CHANGE: `hyper::client::conn::Http2::Builder::new` now requires an executor argument.
1 parent 5bf1640 commit 8068aa0

File tree

7 files changed

+36
-57
lines changed

7 files changed

+36
-57
lines changed
 

‎benches/end_to_end.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,10 @@ impl Opts {
316316
let mut client = rt.block_on(async {
317317
if self.http2 {
318318
let io = tokio::net::TcpStream::connect(&addr).await.unwrap();
319-
let (tx, conn) = hyper::client::conn::http2::Builder::new()
319+
let (tx, conn) = hyper::client::conn::http2::Builder::new(support::TokioExecutor)
320320
.initial_stream_window_size(self.http2_stream_window)
321321
.initial_connection_window_size(self.http2_conn_window)
322322
.adaptive_window(self.http2_adaptive_window)
323-
.executor(support::TokioExecutor)
324323
.handshake(io)
325324
.await
326325
.unwrap();

‎src/client/conn/http2.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,18 @@ pub struct Builder {
5757
///
5858
/// This is a shortcut for `Builder::new().handshake(io)`.
5959
/// See [`client::conn`](crate::client::conn) for more.
60-
pub async fn handshake<T, B>(
60+
pub async fn handshake<E, T, B>(
61+
exec: E,
6162
io: T,
6263
) -> crate::Result<(SendRequest<B>, Connection<T, B>)>
6364
where
65+
E: Executor<BoxSendFuture> + Send + Sync + 'static,
6466
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
6567
B: Body + 'static,
6668
B::Data: Send,
6769
B::Error: Into<Box<dyn StdError + Send + Sync>>,
6870
{
69-
Builder::new().handshake(io).await
71+
Builder::new(exec).handshake(io).await
7072
}
7173

7274
// ===== impl SendRequest
@@ -244,23 +246,17 @@ where
244246
impl Builder {
245247
/// Creates a new connection builder.
246248
#[inline]
247-
pub fn new() -> Builder {
249+
pub fn new<E>(exec: E) -> Builder
250+
where
251+
E: Executor<BoxSendFuture> + Send + Sync + 'static,
252+
{
248253
Builder {
249-
exec: Exec::Default,
254+
exec: Exec::new(exec),
250255
timer: Time::Empty,
251256
h2_builder: Default::default(),
252257
}
253258
}
254259

255-
/// Provide an executor to execute background HTTP2 tasks.
256-
pub fn executor<E>(&mut self, exec: E) -> &mut Builder
257-
where
258-
E: Executor<BoxSendFuture> + Send + Sync + 'static,
259-
{
260-
self.exec = Exec::Executor(Arc::new(exec));
261-
self
262-
}
263-
264260
/// Provide a timer to execute background HTTP2 tasks.
265261
pub fn timer<M>(&mut self, timer: M) -> &mut Builder
266262
where

‎src/common/exec.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,25 @@ pub trait ConnStreamExec<F, B: Body>: Clone {
1616

1717
pub(crate) type BoxSendFuture = Pin<Box<dyn Future<Output = ()> + Send>>;
1818

19-
// Either the user provides an executor for background tasks, or we panic.
20-
// TODO: with the `runtime`feature, `Exec::Default` used `tokio::spawn`. With the
21-
// removal of the opt-in default runtime, this should be refactored.
19+
// Executor must be provided by the user
2220
#[derive(Clone)]
23-
pub(crate) enum Exec {
24-
Default,
25-
Executor(Arc<dyn Executor<BoxSendFuture> + Send + Sync>),
26-
}
21+
pub(crate) struct Exec(Arc<dyn Executor<BoxSendFuture> + Send + Sync>);
2722

2823
// ===== impl Exec =====
2924

3025
impl Exec {
26+
pub(crate) fn new<E>(exec: E) -> Self
27+
where
28+
E: Executor<BoxSendFuture> + Send + Sync + 'static,
29+
{
30+
Self(Arc::new(exec))
31+
}
32+
3133
pub(crate) fn execute<F>(&self, fut: F)
3234
where
3335
F: Future<Output = ()> + Send + 'static,
3436
{
35-
match *self {
36-
Exec::Default => {
37-
panic!("executor must be set");
38-
}
39-
Exec::Executor(ref e) => {
40-
e.execute(Box::pin(fut));
41-
}
42-
}
37+
self.0.execute(Box::pin(fut))
4338
}
4439
}
4540

‎src/ffi/client.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ ffi_fn! {
5555
#[cfg(feature = "http2")]
5656
{
5757
if options.http2 {
58-
return conn::http2::Builder::new()
59-
.executor(options.exec.clone())
58+
return conn::http2::Builder::new(options.exec.clone())
6059
.handshake::<_, crate::body::Incoming>(io)
6160
.await
6261
.map(|(tx, conn)| {

‎tests/client.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -1922,8 +1922,7 @@ mod conn {
19221922
});
19231923

19241924
let io = tcp_connect(&addr).await.expect("tcp connect");
1925-
let (mut client, conn) = conn::http2::Builder::new()
1926-
.executor(TokioExecutor)
1925+
let (mut client, conn) = conn::http2::Builder::new(TokioExecutor)
19271926
.handshake(io)
19281927
.await
19291928
.expect("http handshake");
@@ -1979,8 +1978,7 @@ mod conn {
19791978
});
19801979

19811980
let io = tcp_connect(&addr).await.expect("tcp connect");
1982-
let (_client, conn) = conn::http2::Builder::new()
1983-
.executor(TokioExecutor)
1981+
let (_client, conn) = conn::http2::Builder::new(TokioExecutor)
19841982
.timer(TokioTimer)
19851983
.keep_alive_interval(Duration::from_secs(1))
19861984
.keep_alive_timeout(Duration::from_secs(1))
@@ -2008,8 +2006,7 @@ mod conn {
20082006
});
20092007

20102008
let io = tcp_connect(&addr).await.expect("tcp connect");
2011-
let (mut client, conn) = conn::http2::Builder::new()
2012-
.executor(TokioExecutor)
2009+
let (mut client, conn) = conn::http2::Builder::new(TokioExecutor)
20132010
.timer(TokioTimer)
20142011
.keep_alive_interval(Duration::from_secs(1))
20152012
.keep_alive_timeout(Duration::from_secs(1))
@@ -2040,8 +2037,7 @@ mod conn {
20402037
});
20412038

20422039
let io = tcp_connect(&addr).await.expect("tcp connect");
2043-
let (mut client, conn) = conn::http2::Builder::new()
2044-
.executor(TokioExecutor)
2040+
let (mut client, conn) = conn::http2::Builder::new(TokioExecutor)
20452041
.timer(TokioTimer)
20462042
.keep_alive_interval(Duration::from_secs(1))
20472043
.keep_alive_timeout(Duration::from_secs(1))
@@ -2100,8 +2096,7 @@ mod conn {
21002096
});
21012097

21022098
let io = tcp_connect(&addr).await.expect("tcp connect");
2103-
let (mut client, conn) = conn::http2::Builder::new()
2104-
.executor(TokioExecutor)
2099+
let (mut client, conn) = conn::http2::Builder::new(TokioExecutor)
21052100
.timer(TokioTimer)
21062101
.keep_alive_interval(Duration::from_secs(1))
21072102
.keep_alive_timeout(Duration::from_secs(1))
@@ -2156,8 +2151,7 @@ mod conn {
21562151
});
21572152

21582153
let io = tcp_connect(&addr).await.expect("tcp connect");
2159-
let (mut client, conn) = conn::http2::Builder::new()
2160-
.executor(TokioExecutor)
2154+
let (mut client, conn) = conn::http2::Builder::new(TokioExecutor)
21612155
.handshake(io)
21622156
.await
21632157
.expect("http handshake");
@@ -2207,8 +2201,7 @@ mod conn {
22072201
});
22082202

22092203
let io = tcp_connect(&addr).await.expect("tcp connect");
2210-
let (mut client, conn) = conn::http2::Builder::new()
2211-
.executor(TokioExecutor)
2204+
let (mut client, conn) = conn::http2::Builder::new(TokioExecutor)
22122205
.handshake::<_, Empty<Bytes>>(io)
22132206
.await
22142207
.expect("http handshake");

‎tests/server.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -2389,8 +2389,7 @@ async fn http2_keep_alive_with_responsive_client() {
23892389
});
23902390

23912391
let tcp = connect_async(addr).await;
2392-
let (mut client, conn) = hyper::client::conn::http2::Builder::new()
2393-
.executor(TokioExecutor)
2392+
let (mut client, conn) = hyper::client::conn::http2::Builder::new(TokioExecutor)
23942393
.handshake(tcp)
23952394
.await
23962395
.expect("http handshake");
@@ -3017,8 +3016,7 @@ impl TestClient {
30173016
.unwrap();
30183017

30193018
if self.http2_only {
3020-
let (mut sender, conn) = hyper::client::conn::http2::Builder::new()
3021-
.executor(TokioExecutor)
3019+
let (mut sender, conn) = hyper::client::conn::http2::Builder::new(TokioExecutor)
30223020
.handshake(stream)
30233021
.await
30243022
.unwrap();

‎tests/support/mod.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,7 @@ async fn async_test(cfg: __TestConfig) {
427427
let stream = TcpStream::connect(addr).await.unwrap();
428428

429429
let res = if http2_only {
430-
let (mut sender, conn) = hyper::client::conn::http2::Builder::new()
431-
.executor(TokioExecutor)
430+
let (mut sender, conn) = hyper::client::conn::http2::Builder::new(TokioExecutor)
432431
.handshake(stream)
433432
.await
434433
.unwrap();
@@ -526,11 +525,11 @@ async fn naive_proxy(cfg: ProxyConfig) -> (SocketAddr, impl Future<Output = ()>)
526525
.unwrap();
527526

528527
let resp = if http2_only {
529-
let (mut sender, conn) = hyper::client::conn::http2::Builder::new()
530-
.executor(TokioExecutor)
531-
.handshake(stream)
532-
.await
533-
.unwrap();
528+
let (mut sender, conn) =
529+
hyper::client::conn::http2::Builder::new(TokioExecutor)
530+
.handshake(stream)
531+
.await
532+
.unwrap();
534533

535534
tokio::task::spawn(async move {
536535
if let Err(err) = conn.await {

0 commit comments

Comments
 (0)
Please sign in to comment.