Skip to content

Commit 049b513

Browse files
committed
feat(client): change GaiResolver to use a global blocking threadpool
BREAKING CHANGE: Calls to `GaiResolver::new` and `HttpConnector::new` no longer should pass an integer argument for the number of threads.
1 parent 2664cf5 commit 049b513

File tree

7 files changed

+29
-67
lines changed

7 files changed

+29
-67
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pin-utils = "=0.1.0-alpha.4"
3838
time = "0.1"
3939
tokio = { version = "=0.2.0-alpha.4", optional = true, default-features = false, features = ["rt-full"] }
4040
tower-service = "=0.3.0-alpha.1"
41-
tokio-executor = "=0.2.0-alpha.4"
41+
tokio-executor = { version = "=0.2.0-alpha.4", features = ["blocking"] }
4242
tokio-io = "=0.2.0-alpha.4"
4343
tokio-sync = "=0.2.0-alpha.4"
4444
tokio-net = { version = "=0.2.0-alpha.4", optional = true, features = ["tcp"] }

benches/end_to_end.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl Opts {
179179

180180
let addr = spawn_server(&mut rt, &self);
181181

182-
let connector = HttpConnector::new(1);
182+
let connector = HttpConnector::new();
183183
let client = hyper::Client::builder()
184184
.http2_only(self.http2)
185185
.http2_initial_stream_window_size(self.http2_stream_window)

src/client/connect/dns.rs

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use std::net::{
1515
};
1616
use std::str::FromStr;
1717

18-
use futures_util::{FutureExt, StreamExt};
1918
use tokio_sync::{mpsc, oneshot};
2019

2120
use crate::common::{Future, Never, Pin, Poll, Unpin, task};
@@ -39,10 +38,7 @@ pub struct Name {
3938
/// A resolver using blocking `getaddrinfo` calls in a threadpool.
4039
#[derive(Clone)]
4140
pub struct GaiResolver {
42-
tx: tokio_executor::threadpool::Sender,
43-
/// A handle to keep the threadpool alive until all `GaiResolver` clones
44-
/// have been dropped.
45-
_threadpool_keep_alive: ThreadPoolKeepAlive,
41+
_priv: (),
4642
}
4743

4844
#[derive(Clone)]
@@ -55,8 +51,7 @@ pub struct GaiAddrs {
5551

5652
/// A future to resole a name returned by `GaiResolver`.
5753
pub struct GaiFuture {
58-
rx: oneshot::Receiver<Result<IpAddrs, io::Error>>,
59-
_threadpool_keep_alive: ThreadPoolKeepAlive,
54+
inner: tokio_executor::blocking::Blocking<Result<IpAddrs, io::Error>>,
6055
}
6156

6257
impl Name {
@@ -108,40 +103,9 @@ impl Error for InvalidNameError {}
108103

109104
impl GaiResolver {
110105
/// Construct a new `GaiResolver`.
111-
///
112-
/// Takes number of DNS worker threads.
113-
pub fn new(threads: usize) -> Self {
114-
let pool = tokio_executor::threadpool::Builder::new()
115-
.name_prefix("hyper-dns-gai-resolver")
116-
// not for CPU tasks, so only spawn workers
117-
// in blocking mode
118-
.pool_size(1)
119-
.max_blocking(threads)
120-
.build();
121-
122-
let tx = pool.sender().clone();
123-
124-
// The pool will start to shutdown once `pool` is dropped,
125-
// so to keep it alive, we spawn a future onto the pool itself
126-
// that will only resolve once all `GaiResolver` requests
127-
// are finished.
128-
let (shutdown_tx, shutdown_rx) = mpsc::channel(1);
129-
130-
let on_shutdown = shutdown_rx
131-
.into_future()
132-
.map(move |(next, _rx)| {
133-
match next {
134-
Some(never) => match never {},
135-
None => (),
136-
}
137-
138-
drop(pool)
139-
});
140-
tx.spawn(on_shutdown).expect("can spawn on self");
141-
106+
pub fn new() -> Self {
142107
GaiResolver {
143-
tx,
144-
_threadpool_keep_alive: ThreadPoolKeepAlive(shutdown_tx),
108+
_priv: (),
145109
}
146110
}
147111
}
@@ -151,14 +115,14 @@ impl Resolve for GaiResolver {
151115
type Future = GaiFuture;
152116

153117
fn resolve(&self, name: Name) -> Self::Future {
154-
let (tx, rx) = oneshot::channel();
155-
self.tx.spawn(GaiBlocking {
156-
host: name.host,
157-
tx: Some(tx),
158-
}).expect("spawn GaiBlocking");
118+
let blocking = tokio_executor::blocking::run(move || {
119+
debug!("resolving host={:?}", name.host);
120+
(&*name.host, 0).to_socket_addrs()
121+
.map(|i| IpAddrs { iter: i })
122+
});
123+
159124
GaiFuture {
160-
rx,
161-
_threadpool_keep_alive: self._threadpool_keep_alive.clone(),
125+
inner: blocking,
162126
}
163127
}
164128
}
@@ -173,10 +137,9 @@ impl Future for GaiFuture {
173137
type Output = Result<GaiAddrs, io::Error>;
174138

175139
fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
176-
Pin::new(&mut self.rx).poll(cx).map(|res| match res {
177-
Ok(Ok(addrs)) => Ok(GaiAddrs { inner: addrs }),
178-
Ok(Err(err)) => Err(err),
179-
Err(_canceled) => unreachable!("GaiResolver threadpool shutdown"),
140+
Pin::new(&mut self.inner).poll(cx).map(|res| match res {
141+
Ok(addrs) => Ok(GaiAddrs { inner: addrs }),
142+
Err(err) => Err(err),
180143
})
181144
}
182145
}

src/client/connect/http.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,8 @@ impl HttpConnector {
7777
/// Construct a new HttpConnector.
7878
///
7979
/// Takes number of DNS worker threads.
80-
#[inline]
81-
pub fn new(threads: usize) -> HttpConnector {
82-
HttpConnector::new_with_resolver(GaiResolver::new(threads))
80+
pub fn new() -> HttpConnector {
81+
HttpConnector::new_with_resolver(GaiResolver::new())
8382
}
8483
}
8584

src/client/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ impl Builder {
10231023
B: Payload + Send,
10241024
B::Data: Send,
10251025
{
1026-
let mut connector = HttpConnector::new(4);
1026+
let mut connector = HttpConnector::new();
10271027
if self.pool_config.enabled {
10281028
connector.set_keepalive(self.pool_config.keep_alive_timeout);
10291029
}

tests/client.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ macro_rules! test {
213213
let addr = server.local_addr().expect("local_addr");
214214
let rt = $runtime;
215215

216-
let connector = ::hyper::client::HttpConnector::new(1);
216+
let connector = ::hyper::client::HttpConnector::new();
217217
let client = Client::builder()
218218
.set_host($set_host)
219219
.http1_title_case_headers($title_case_headers)
@@ -781,7 +781,7 @@ mod dispatch_impl {
781781
let mut rt = Runtime::new().unwrap();
782782
let (closes_tx, closes) = mpsc::channel(10);
783783
let client = Client::builder()
784-
.build(DebugConnector::with_http_and_closes(HttpConnector::new(1), closes_tx));
784+
.build(DebugConnector::with_http_and_closes(HttpConnector::new(), closes_tx));
785785

786786
let (tx1, rx1) = oneshot::channel();
787787

@@ -837,7 +837,7 @@ mod dispatch_impl {
837837

838838
let res = {
839839
let client = Client::builder()
840-
.build(DebugConnector::with_http_and_closes(HttpConnector::new(1), closes_tx));
840+
.build(DebugConnector::with_http_and_closes(HttpConnector::new(), closes_tx));
841841

842842
let req = Request::builder()
843843
.uri(&*format!("http://{}/a", addr))
@@ -889,7 +889,7 @@ mod dispatch_impl {
889889
});
890890

891891
let client = Client::builder()
892-
.build(DebugConnector::with_http_and_closes(HttpConnector::new(1), closes_tx));
892+
.build(DebugConnector::with_http_and_closes(HttpConnector::new(), closes_tx));
893893

894894
let req = Request::builder()
895895
.uri(&*format!("http://{}/a", addr))
@@ -948,7 +948,7 @@ mod dispatch_impl {
948948

949949
let res = {
950950
let client = Client::builder()
951-
.build(DebugConnector::with_http_and_closes(HttpConnector::new(1), closes_tx));
951+
.build(DebugConnector::with_http_and_closes(HttpConnector::new(), closes_tx));
952952

953953
let req = Request::builder()
954954
.uri(&*format!("http://{}/a", addr))
@@ -996,7 +996,7 @@ mod dispatch_impl {
996996

997997
let res = {
998998
let client = Client::builder()
999-
.build(DebugConnector::with_http_and_closes(HttpConnector::new(1), closes_tx));
999+
.build(DebugConnector::with_http_and_closes(HttpConnector::new(), closes_tx));
10001000

10011001
let req = Request::builder()
10021002
.uri(&*format!("http://{}/a", addr))
@@ -1046,7 +1046,7 @@ mod dispatch_impl {
10461046

10471047
let client = Client::builder()
10481048
.keep_alive(false)
1049-
.build(DebugConnector::with_http_and_closes(HttpConnector::new(1), closes_tx));
1049+
.build(DebugConnector::with_http_and_closes(HttpConnector::new(), closes_tx));
10501050

10511051
let req = Request::builder()
10521052
.uri(&*format!("http://{}/a", addr))
@@ -1090,7 +1090,7 @@ mod dispatch_impl {
10901090
});
10911091

10921092
let client = Client::builder()
1093-
.build(DebugConnector::with_http_and_closes(HttpConnector::new(1), closes_tx));
1093+
.build(DebugConnector::with_http_and_closes(HttpConnector::new(), closes_tx));
10941094

10951095
let req = Request::builder()
10961096
.uri(&*format!("http://{}/a", addr))
@@ -1527,7 +1527,7 @@ mod dispatch_impl {
15271527

15281528
impl DebugConnector {
15291529
fn new() -> DebugConnector {
1530-
let http = HttpConnector::new(1);
1530+
let http = HttpConnector::new();
15311531
let (tx, _) = mpsc::channel(10);
15321532
DebugConnector::with_http_and_closes(http, tx)
15331533
}

tests/support/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ pub fn __run_test(cfg: __TestConfig) {
313313
Version::HTTP_11
314314
};
315315

316-
let connector = HttpConnector::new(1);
316+
let connector = HttpConnector::new();
317317
let client = Client::builder()
318318
.keep_alive_timeout(Duration::from_secs(10))
319319
.http2_only(cfg.client_version == 2)

0 commit comments

Comments
 (0)