Skip to content

Commit 6ade21a

Browse files
committed
feat(server): change default dispatcher
- Deprecates the `no_proto` configuration on `Server`. It is always enabled. - Deprecates all pieces related to tokio-proto. - Makes the tokio-proto crate optional, and the `server-proto` feature can be used to completely remove the dependency. It is enabled by default.
1 parent 0892cb2 commit 6ade21a

File tree

15 files changed

+124
-118
lines changed

15 files changed

+124
-118
lines changed

.travis.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ matrix:
88
env: FEATURES="--features nightly"
99
- rust: beta
1010
- rust: stable
11+
env: HYPER_DOCS=1
1112
- rust: stable
12-
env: HYPER_NO_PROTO=1
13+
env: FEATURES="--no-default-features"
1314
- rust: stable
1415
env: FEATURES="--features compat"
1516
- rust: 1.17.0
@@ -37,7 +38,7 @@ addons:
3738

3839

3940
after_success:
40-
- '[ $TRAVIS_RUST_VERSION = stable ] &&
41+
- '[ "$HYPER_DOCS" = "1" ] &&
4142
LOCAL="~/.local" && export PATH=$LOCAL/bin:$PATH &&
4243
wget https://github.com/SimonKagstrom/kcov/archive/master.tar.gz &&
4344
tar xzf master.tar.gz && mkdir kcov-master/build && cd kcov-master/build &&
@@ -51,7 +52,7 @@ after_success:
5152
fi;
5253
done &&
5354
kcov --coveralls-id=$TRAVIS_JOB_ID --merge target/cov target/cov/*'
54-
- '[ $TRAVIS_PULL_REQUEST = false ] && [ $TRAVIS_RUST_VERSION = stable ] &&
55+
- '[ $TRAVIS_PULL_REQUEST = false ] && [ "$HYPER_DOCS" = "1" ] &&
5556
{ [ "$TRAVIS_TAG" != "" ] || [ "$TRAVIS_BRANCH" == "master" ]; } &&
5657
./.travis/docs.sh'
5758

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ percent-encoding = "1.0"
3232
relay = "0.1"
3333
time = "0.1"
3434
tokio-core = "0.1.6"
35-
tokio-proto = "0.1"
35+
tokio-proto = { version = "0.1", optional = true }
3636
tokio-service = "0.1"
3737
tokio-io = "0.1"
3838
unicase = "2.0"
@@ -48,4 +48,4 @@ default = ["server-proto"]
4848
nightly = []
4949
raw_status = []
5050
compat = [ "http" ]
51-
server-proto = []
51+
server-proto = ["tokio-proto"]

examples/hello.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ fn main() {
1919
.with_body(PHRASE))
2020
}));
2121

22-
let mut server = Http::new().bind(&addr, new_service).unwrap();
23-
server.no_proto();
22+
let server = Http::new().bind(&addr, new_service).unwrap();
2423
println!("Listening on http://{} with 1 thread.", server.local_addr().unwrap());
2524
server.run().unwrap();
2625
}

examples/params.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ fn main() {
9999
pretty_env_logger::init().unwrap();
100100
let addr = "127.0.0.1:1337".parse().unwrap();
101101

102-
let mut server = Http::new().bind(&addr, || Ok(ParamExample)).unwrap();
103-
server.no_proto();
102+
let server = Http::new().bind(&addr, || Ok(ParamExample)).unwrap();
104103
println!("Listening on http://{} with 1 thread.", server.local_addr().unwrap());
105104
server.run().unwrap();
106105
}

examples/send_file.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ fn main() {
135135
pretty_env_logger::init().unwrap();
136136
let addr = "127.0.0.1:1337".parse().unwrap();
137137

138-
let mut server = Http::new().bind(&addr, || Ok(ResponseExamples)).unwrap();
139-
server.no_proto();
138+
let server = Http::new().bind(&addr, || Ok(ResponseExamples)).unwrap();
140139
println!("Listening on http://{} with 1 thread.", server.local_addr().unwrap());
141140
server.run().unwrap();
142141
}

examples/server.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ fn main() {
4747
pretty_env_logger::init().unwrap();
4848
let addr = "127.0.0.1:1337".parse().unwrap();
4949

50-
let mut server = Http::new().bind(&addr, || Ok(Echo)).unwrap();
51-
server.no_proto();
50+
let server = Http::new().bind(&addr, || Ok(Echo)).unwrap();
5251
println!("Listening on http://{} with 1 thread.", server.local_addr().unwrap());
5352
server.run().unwrap();
5453
}

src/client/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ where C: Connect,
198198
let pooled = pool.pooled(pool_key, tx);
199199
let conn = proto::Conn::<_, _, proto::ClientTransaction, _>::new(io, pooled.clone());
200200
let dispatch = proto::dispatch::Dispatcher::new(proto::dispatch::Client::new(rx), conn);
201-
handle.spawn(dispatch.map_err(|err| error!("no_proto error: {}", err)));
201+
handle.spawn(dispatch.map_err(|err| error!("client connection error: {}", err)));
202202
pooled
203203
})
204204
};

src/lib.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extern crate relay;
3232
extern crate time;
3333
extern crate tokio_core as tokio;
3434
#[macro_use] extern crate tokio_io;
35+
#[cfg(feature = "tokio-proto")]
3536
extern crate tokio_proto;
3637
extern crate tokio_service;
3738
extern crate unicase;
@@ -55,17 +56,13 @@ pub use proto::RawStatus;
5556

5657
macro_rules! feat_server_proto {
5758
($($i:item)*) => ($(
58-
#[cfg_attr(
59-
not(feature = "server-proto"),
60-
deprecated(
61-
since="0.11.7",
62-
note="server-proto was recently added to default features, but you have disabled default features. A future version will remove these types if the server-proto feature is not enabled."
63-
)
64-
)]
65-
#[cfg_attr(
66-
not(feature = "server-proto"),
67-
allow(deprecated)
59+
#[cfg(feature = "server-proto")]
60+
#[deprecated(
61+
since="0.11.11",
62+
note="All usage of the tokio-proto crate is going away."
6863
)]
64+
#[doc(hidden)]
65+
#[allow(deprecated)]
6966
$i
7067
)*)
7168
}

src/mock.rs

+9
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,23 @@ impl<T> AsyncIo<T> {
8787
}
8888

8989
impl AsyncIo<Buf> {
90+
#[cfg(feature = "tokio-proto")]
91+
//TODO: fix proto::conn::tests to not use tokio-proto API,
92+
//and then this cfg flag go away
9093
pub fn new_buf<T: Into<Vec<u8>>>(buf: T, bytes: usize) -> AsyncIo<Buf> {
9194
AsyncIo::new(Buf::wrap(buf.into()), bytes)
9295
}
9396

97+
#[cfg(feature = "tokio-proto")]
98+
//TODO: fix proto::conn::tests to not use tokio-proto API,
99+
//and then this cfg flag go away
94100
pub fn new_eof() -> AsyncIo<Buf> {
95101
AsyncIo::new(Buf::wrap(Vec::new().into()), 1)
96102
}
97103

104+
#[cfg(feature = "tokio-proto")]
105+
//TODO: fix proto::conn::tests to not use tokio-proto API,
106+
//and then this cfg flag go away
98107
pub fn flushed(&self) -> bool {
99108
self.flushed
100109
}

src/proto/body.rs

+58-38
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use bytes::Bytes;
22
use futures::{Async, AsyncSink, Future, Poll, Sink, StartSend, Stream};
33
use futures::sync::{mpsc, oneshot};
4+
#[cfg(feature = "tokio-proto")]
45
use tokio_proto;
56
use std::borrow::Cow;
67

78
use super::Chunk;
89

10+
#[cfg(feature = "tokio-proto")]
911
pub type TokioBody = tokio_proto::streaming::Body<Chunk, ::Error>;
1012
pub type BodySender = mpsc::Sender<Result<Chunk, ::Error>>;
1113

@@ -16,33 +18,36 @@ pub struct Body(Inner);
1618

1719
#[derive(Debug)]
1820
enum Inner {
21+
#[cfg(feature = "tokio-proto")]
1922
Tokio(TokioBody),
20-
Hyper {
21-
close_tx: oneshot::Sender<()>,
23+
Chan {
24+
close_tx: oneshot::Sender<bool>,
2225
rx: mpsc::Receiver<Result<Chunk, ::Error>>,
23-
}
26+
},
27+
Once(Option<Chunk>),
28+
Empty,
2429
}
2530

2631
//pub(crate)
2732
#[derive(Debug)]
2833
pub struct ChunkSender {
29-
close_rx: oneshot::Receiver<()>,
34+
close_rx: oneshot::Receiver<bool>,
35+
close_rx_check: bool,
3036
tx: BodySender,
3137
}
3238

3339
impl Body {
3440
/// Return an empty body stream
3541
#[inline]
3642
pub fn empty() -> Body {
37-
Body(Inner::Tokio(TokioBody::empty()))
43+
Body(Inner::Empty)
3844
}
3945

4046
/// Return a body stream with an associated sender half
4147
#[inline]
4248
pub fn pair() -> (mpsc::Sender<Result<Chunk, ::Error>>, Body) {
43-
let (tx, rx) = TokioBody::pair();
44-
let rx = Body(Inner::Tokio(rx));
45-
(tx, rx)
49+
let (tx, rx) = channel();
50+
(tx.tx, rx)
4651
}
4752
}
4853

@@ -60,13 +65,16 @@ impl Stream for Body {
6065
#[inline]
6166
fn poll(&mut self) -> Poll<Option<Chunk>, ::Error> {
6267
match self.0 {
68+
#[cfg(feature = "tokio-proto")]
6369
Inner::Tokio(ref mut rx) => rx.poll(),
64-
Inner::Hyper { ref mut rx, .. } => match rx.poll().expect("mpsc cannot error") {
70+
Inner::Chan { ref mut rx, .. } => match rx.poll().expect("mpsc cannot error") {
6571
Async::Ready(Some(Ok(chunk))) => Ok(Async::Ready(Some(chunk))),
6672
Async::Ready(Some(Err(err))) => Err(err),
6773
Async::Ready(None) => Ok(Async::Ready(None)),
6874
Async::NotReady => Ok(Async::NotReady),
6975
},
76+
Inner::Once(ref mut val) => Ok(Async::Ready(val.take())),
77+
Inner::Empty => Ok(Async::Ready(None)),
7078
}
7179
}
7280
}
@@ -78,9 +86,10 @@ pub fn channel() -> (ChunkSender, Body) {
7886

7987
let tx = ChunkSender {
8088
close_rx: close_rx,
89+
close_rx_check: true,
8190
tx: tx,
8291
};
83-
let rx = Body(Inner::Hyper {
92+
let rx = Body(Inner::Chan {
8493
close_tx: close_tx,
8594
rx: rx,
8695
});
@@ -90,9 +99,16 @@ pub fn channel() -> (ChunkSender, Body) {
9099

91100
impl ChunkSender {
92101
pub fn poll_ready(&mut self) -> Poll<(), ()> {
93-
match self.close_rx.poll() {
94-
Ok(Async::Ready(())) | Err(_) => return Err(()),
95-
Ok(Async::NotReady) => (),
102+
if self.close_rx_check {
103+
match self.close_rx.poll() {
104+
Ok(Async::Ready(true)) | Err(_) => return Err(()),
105+
Ok(Async::Ready(false)) => {
106+
// needed to allow converting into a plain mpsc::Receiver
107+
// if it has been, the tx will send false to disable this check
108+
self.close_rx_check = false;
109+
}
110+
Ok(Async::NotReady) => (),
111+
}
96112
}
97113

98114
self.tx.poll_ready().map_err(|_| ())
@@ -107,63 +123,67 @@ impl ChunkSender {
107123
}
108124
}
109125

110-
// deprecate soon, but can't really deprecate trait impls
111-
#[doc(hidden)]
112-
impl From<Body> for tokio_proto::streaming::Body<Chunk, ::Error> {
113-
#[inline]
114-
fn from(b: Body) -> tokio_proto::streaming::Body<Chunk, ::Error> {
115-
match b.0 {
116-
Inner::Tokio(b) => b,
117-
Inner::Hyper { close_tx, rx } => {
118-
warn!("converting hyper::Body into a tokio_proto Body is deprecated");
119-
::std::mem::forget(close_tx);
120-
rx.into()
126+
feat_server_proto! {
127+
impl From<Body> for tokio_proto::streaming::Body<Chunk, ::Error> {
128+
fn from(b: Body) -> tokio_proto::streaming::Body<Chunk, ::Error> {
129+
match b.0 {
130+
Inner::Tokio(b) => b,
131+
Inner::Chan { close_tx, rx } => {
132+
// disable knowing if the Rx gets dropped, since we cannot
133+
// pass this tx along.
134+
let _ = close_tx.send(false);
135+
rx.into()
136+
},
137+
Inner::Once(Some(chunk)) => TokioBody::from(chunk),
138+
Inner::Once(None) |
139+
Inner::Empty => TokioBody::empty(),
121140
}
122141
}
123142
}
124-
}
125143

126-
// deprecate soon, but can't really deprecate trait impls
127-
#[doc(hidden)]
128-
impl From<tokio_proto::streaming::Body<Chunk, ::Error>> for Body {
129-
#[inline]
130-
fn from(tokio_body: tokio_proto::streaming::Body<Chunk, ::Error>) -> Body {
131-
Body(Inner::Tokio(tokio_body))
144+
impl From<tokio_proto::streaming::Body<Chunk, ::Error>> for Body {
145+
fn from(tokio_body: tokio_proto::streaming::Body<Chunk, ::Error>) -> Body {
146+
Body(Inner::Tokio(tokio_body))
147+
}
132148
}
133149
}
134150

135151
impl From<mpsc::Receiver<Result<Chunk, ::Error>>> for Body {
136152
#[inline]
137153
fn from(src: mpsc::Receiver<Result<Chunk, ::Error>>) -> Body {
138-
TokioBody::from(src).into()
154+
let (tx, _) = oneshot::channel();
155+
Body(Inner::Chan {
156+
close_tx: tx,
157+
rx: src,
158+
})
139159
}
140160
}
141161

142162
impl From<Chunk> for Body {
143163
#[inline]
144164
fn from (chunk: Chunk) -> Body {
145-
TokioBody::from(chunk).into()
165+
Body(Inner::Once(Some(chunk)))
146166
}
147167
}
148168

149169
impl From<Bytes> for Body {
150170
#[inline]
151171
fn from (bytes: Bytes) -> Body {
152-
Body::from(TokioBody::from(Chunk::from(bytes)))
172+
Body::from(Chunk::from(bytes))
153173
}
154174
}
155175

156176
impl From<Vec<u8>> for Body {
157177
#[inline]
158178
fn from (vec: Vec<u8>) -> Body {
159-
Body::from(TokioBody::from(Chunk::from(vec)))
179+
Body::from(Chunk::from(vec))
160180
}
161181
}
162182

163183
impl From<&'static [u8]> for Body {
164184
#[inline]
165185
fn from (slice: &'static [u8]) -> Body {
166-
Body::from(TokioBody::from(Chunk::from(slice)))
186+
Body::from(Chunk::from(slice))
167187
}
168188
}
169189

@@ -180,14 +200,14 @@ impl From<Cow<'static, [u8]>> for Body {
180200
impl From<String> for Body {
181201
#[inline]
182202
fn from (s: String) -> Body {
183-
Body::from(TokioBody::from(Chunk::from(s.into_bytes())))
203+
Body::from(Chunk::from(s.into_bytes()))
184204
}
185205
}
186206

187207
impl From<&'static str> for Body {
188208
#[inline]
189209
fn from(slice: &'static str) -> Body {
190-
Body::from(TokioBody::from(Chunk::from(slice.as_bytes())))
210+
Body::from(Chunk::from(slice.as_bytes()))
191211
}
192212
}
193213

0 commit comments

Comments
 (0)