Skip to content

Commit

Permalink
feat(rt): make tokio runtime optional
Browse files Browse the repository at this point in the history
A Cargo feature `runtime` is added, which is enabled by default, that
includes the following:

- The `client::HttpConnector`, which uses `tokio::net::TcpStream`.
- The `server::AddrStream`, which uses `tokio::net::TcpListener`.
- The `hyper::rt` module, which includes useful utilities to work with
  the runtime without needing to import `futures` or `tokio` explicity.

Disabling the feature removes many of these niceties, but allows people
to use hyper in environments that have an alternative runtime, without
needing to download an unused one.
  • Loading branch information
seanmonstar authored Apr 23, 2018
1 parent 62a5c11 commit d127201
Show file tree
Hide file tree
Showing 21 changed files with 518 additions and 387 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ matrix:
- rust: beta
- rust: stable
env: HYPER_DOCS=1
- rust: stable
env: FEATURES="--no-default-features"
- rust: 1.21.0

cache:
apt: true
directories:
- target/debug/deps
- target/debug/build

script:
- ./.travis/readme.py
Expand Down
84 changes: 79 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@ include = [

[dependencies]
bytes = "0.4.4"
futures = "0.1.17"
futures-cpupool = "0.1.6"
futures = "0.1.21"
futures-cpupool = { version = "0.1.6", optional = true }
futures-timer = "0.1.0"
http = "0.1.5"
httparse = "1.0"
h2 = "0.1.5"
iovec = "0.1"
log = "0.4"
net2 = "0.2.32"
net2 = { version = "0.2.32", optional = true }
time = "0.1"
tokio = "0.1.5"
tokio-executor = "0.1.0"
tokio = { version = "0.1.5", optional = true }
tokio-executor = { version = "0.1.0", optional = true }
tokio-io = "0.1"
tokio-reactor = { version = "0.1", optional = true }
tokio-tcp = { version = "0.1", optional = true }
want = "0.0.3"

[dev-dependencies]
Expand All @@ -44,4 +46,76 @@ spmc = "0.2"
url = "1.0"

[features]
default = ["runtime"]
nightly = []
runtime = [
"futures-cpupool",
"net2",
"tokio",
"tokio-executor",
"tokio-reactor",
"tokio-tcp",
]

[[example]]
name = "client"
path = "examples/client.rs"
required-features = ["runtime"]

[[example]]
name = "hello"
path = "examples/hello.rs"
required-features = ["runtime"]

[[example]]
name = "multi_server"
path = "examples/multi_server.rs"
required-features = ["runtime"]

[[example]]
name = "params"
path = "examples/params.rs"
required-features = ["runtime"]

[[example]]
name = "send_file"
path = "examples/send_file.rs"
required-features = ["runtime"]

[[example]]
name = "server"
path = "examples/server.rs"
required-features = ["runtime"]

[[example]]
name = "web_api"
path = "examples/web_api.rs"
required-features = ["runtime"]


[[bench]]
name = "end_to_end"
path = "benches/end_to_end.rs"
required-features = ["runtime"]

[[bench]]
name = "server"
path = "benches/server.rs"
required-features = ["runtime"]


[[test]]
name = "client"
path = "tests/client.rs"
required-features = ["runtime"]

[[test]]
name = "integration"
path = "tests/integration.rs"
required-features = ["runtime"]

[[test]]
name = "server"
path = "tests/server.rs"
required-features = ["runtime"]

11 changes: 3 additions & 8 deletions examples/client.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
//#![deny(warnings)]
extern crate futures;
#![deny(warnings)]
extern crate hyper;
extern crate tokio;

extern crate pretty_env_logger;

use std::env;
use std::io::{self, Write};

use futures::{Future, Stream};
use futures::future::lazy;

use hyper::{Body, Client, Request};
use hyper::rt::{self, Future, Stream};

fn main() {
pretty_env_logger::init();
Expand All @@ -30,7 +25,7 @@ fn main() {
return;
}

tokio::run(lazy(move || {
rt::run(rt::lazy(move || {
let client = Client::new();

let mut req = Request::new(Body::empty());
Expand Down
7 changes: 2 additions & 5 deletions examples/hello.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#![deny(warnings)]
extern crate hyper;
extern crate futures;
extern crate pretty_env_logger;
extern crate tokio;

use futures::Future;

use hyper::{Body, Response, Server};
use hyper::service::service_fn_ok;
use hyper::rt::{self, Future};

static PHRASE: &'static [u8] = b"Hello World!";

Expand All @@ -33,5 +30,5 @@ fn main() {

println!("Listening on http://{}", addr);

tokio::run(server);
rt::run(server);
}
12 changes: 4 additions & 8 deletions examples/multi_server.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
#![deny(warnings)]
extern crate hyper;
extern crate futures;
extern crate pretty_env_logger;
extern crate tokio;

use futures::{Future};
use futures::future::{lazy};

use hyper::{Body, Response, Server};
use hyper::service::service_fn_ok;
use hyper::rt::{self, Future};

static INDEX1: &'static [u8] = b"The 1st service!";
static INDEX2: &'static [u8] = b"The 2nd service!";
Expand All @@ -19,7 +15,7 @@ fn main() {
let addr1 = ([127, 0, 0, 1], 1337).into();
let addr2 = ([127, 0, 0, 1], 1338).into();

tokio::run(lazy(move || {
rt::run(rt::lazy(move || {
let srv1 = Server::bind(&addr1)
.serve(|| service_fn_ok(|_| Response::new(Body::from(INDEX1))))
.map_err(|e| eprintln!("server 1 error: {}", e));
Expand All @@ -30,8 +26,8 @@ fn main() {

println!("Listening on http://{} and http://{}", addr1, addr2);

tokio::spawn(srv1);
tokio::spawn(srv2);
rt::spawn(srv1);
rt::spawn(srv2);

Ok(())
}));
Expand Down
3 changes: 1 addition & 2 deletions examples/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
extern crate futures;
extern crate hyper;
extern crate pretty_env_logger;
extern crate tokio;
extern crate url;

use futures::{future, Future, Stream};
Expand Down Expand Up @@ -93,5 +92,5 @@ fn main() {
.serve(|| service_fn(param_example))
.map_err(|e| eprintln!("server error: {}", e));

tokio::run(server);
hyper::rt::run(server);
}
3 changes: 1 addition & 2 deletions examples/send_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
extern crate futures;
extern crate hyper;
extern crate pretty_env_logger;
extern crate tokio;

use futures::{future, Future};
use futures::sync::oneshot;
Expand All @@ -29,7 +28,7 @@ fn main() {

println!("Listening on http://{}", addr);

tokio::run(server);
hyper::rt::run(server);
}

type ResponseFuture = Box<Future<Item=Response<Body>, Error=io::Error> + Send>;
Expand Down
7 changes: 2 additions & 5 deletions examples/server.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#![deny(warnings)]
extern crate futures;
extern crate hyper;
extern crate pretty_env_logger;
extern crate tokio;

use futures::Future;

use hyper::{Body, Method, Request, Response, Server, StatusCode};
use hyper::service::service_fn_ok;
use hyper::rt::Future;

static INDEX: &'static [u8] = b"Try POST /echo";

Expand Down Expand Up @@ -40,5 +37,5 @@ fn main() {

println!("Listening on http://{}", addr);

tokio::run(server);
hyper::rt::run(server);
}
3 changes: 1 addition & 2 deletions examples/web_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
extern crate futures;
extern crate hyper;
extern crate pretty_env_logger;
extern crate tokio;

use futures::{future, Future, Stream};

Expand Down Expand Up @@ -68,7 +67,7 @@ fn main() {

let addr = "127.0.0.1:1337".parse().unwrap();

tokio::run(future::lazy(move || {
hyper::rt::run(future::lazy(move || {
// Share a `Client` with all `Service`s
let client = Client::new();

Expand Down
Loading

0 comments on commit d127201

Please sign in to comment.