Update examples to use new Tokio (#316)

The old `tokio-core` crate is deprecated in favour of the `tokio` crate,
which also provides the new multithreaded Tokio runtime. Although `h2`
is generic over the runtime, the examples do depend on `tokio`. 

This branch update the example code to use the new `tokio` library
rather than `tokio-core`. It also updates the examples in `RustDoc`.

For the most part, this was pretty trivial --- simply replacing
`handle.spawn(...)` with `tokio::spawn(...)` and `core.run(...)` with
`tokio::run(...)`. There were a couple of cases where error and item
types from spawned futures had to be mapped to `(), ()`. Alternatively,
this could have been avoided by using the single-threaded runtime and
calling `block_on` instead to await the value of those futures, but I
thought it was better to reduce the amount of tokio-specific code in the
examples.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
This commit is contained in:
Eliza Weisman
2018-09-25 14:33:49 -07:00
committed by GitHub
parent 12e0d26945
commit 00ca534c4a
6 changed files with 38 additions and 57 deletions

View File

@@ -62,7 +62,7 @@ serde = "1.0.0"
serde_json = "1.0.0" serde_json = "1.0.0"
# Akamai example # Akamai example
tokio-core = "0.1" tokio = "0.1.8"
env_logger = { version = "0.5.3", default-features = false } env_logger = { version = "0.5.3", default-features = false }
rustls = "0.12" rustls = "0.12"
tokio-rustls = "0.5.0" tokio-rustls = "0.5.0"

View File

@@ -3,7 +3,7 @@ extern crate futures;
extern crate h2; extern crate h2;
extern crate http; extern crate http;
extern crate rustls; extern crate rustls;
extern crate tokio_core; extern crate tokio;
extern crate tokio_rustls; extern crate tokio_rustls;
extern crate webpki; extern crate webpki;
extern crate webpki_roots; extern crate webpki_roots;
@@ -13,8 +13,7 @@ use h2::client;
use futures::*; use futures::*;
use http::{Method, Request}; use http::{Method, Request};
use tokio_core::net::TcpStream; use tokio::net::TcpStream;
use tokio_core::reactor;
use rustls::Session; use rustls::Session;
use tokio_rustls::ClientConfigExt; use tokio_rustls::ClientConfigExt;
@@ -44,13 +43,10 @@ pub fn main() {
println!("ADDR: {:?}", addr); println!("ADDR: {:?}", addr);
let mut core = reactor::Core::new().unwrap(); let tcp = TcpStream::connect(&addr);
let handle = core.handle();
let tcp = TcpStream::connect(&addr, &handle);
let dns_name = DNSNameRef::try_from_ascii_str("http2.akamai.com").unwrap(); let dns_name = DNSNameRef::try_from_ascii_str("http2.akamai.com").unwrap();
let tcp = tcp.then(|res| { let tcp = tcp.then(move |res| {
let tcp = res.unwrap(); let tcp = res.unwrap();
tls_client_config tls_client_config
.connect_async(dns_name, tcp) .connect_async(dns_name, tcp)
@@ -87,7 +83,9 @@ pub fn main() {
h2.join(stream) h2.join(stream)
}) })
}); })
.map_err(|e| eprintln!("ERROR: {:?}", e))
.map(|((), ())| ());
core.run(tcp).unwrap(); tokio::run(tcp);
} }

View File

@@ -2,7 +2,7 @@ extern crate env_logger;
extern crate futures; extern crate futures;
extern crate h2; extern crate h2;
extern crate http; extern crate http;
extern crate tokio_core; extern crate tokio;
use h2::client; use h2::client;
use h2::RecvStream; use h2::RecvStream;
@@ -10,8 +10,7 @@ use h2::RecvStream;
use futures::*; use futures::*;
use http::*; use http::*;
use tokio_core::net::TcpStream; use tokio::net::TcpStream;
use tokio_core::reactor;
struct Process { struct Process {
body: RecvStream, body: RecvStream,
@@ -47,10 +46,7 @@ impl Future for Process {
pub fn main() { pub fn main() {
let _ = env_logger::try_init(); let _ = env_logger::try_init();
let mut core = reactor::Core::new().unwrap(); let tcp = TcpStream::connect(&"127.0.0.1:5928".parse().unwrap());
let handle = core.handle();
let tcp = TcpStream::connect(&"127.0.0.1:5928".parse().unwrap(), &handle);
let tcp = tcp.then(|res| { let tcp = tcp.then(|res| {
let tcp = res.unwrap(); let tcp = res.unwrap();
@@ -74,7 +70,7 @@ pub fn main() {
stream.send_trailers(trailers).unwrap(); stream.send_trailers(trailers).unwrap();
// Spawn a task to run the conn... // Spawn a task to run the conn...
handle.spawn(h2.map_err(|e| println!("GOT ERR={:?}", e))); tokio::spawn(h2.map_err(|e| println!("GOT ERR={:?}", e)));
response response
.and_then(|response| { .and_then(|response| {
@@ -93,5 +89,5 @@ pub fn main() {
}) })
}); });
core.run(tcp).unwrap(); tokio::run(tcp);
} }

View File

@@ -3,7 +3,7 @@ extern crate env_logger;
extern crate futures; extern crate futures;
extern crate h2; extern crate h2;
extern crate http; extern crate http;
extern crate tokio_core; extern crate tokio;
use h2::server; use h2::server;
@@ -11,20 +11,16 @@ use bytes::*;
use futures::*; use futures::*;
use http::*; use http::*;
use tokio_core::net::TcpListener; use tokio::net::TcpListener;
use tokio_core::reactor;
pub fn main() { pub fn main() {
let _ = env_logger::try_init(); let _ = env_logger::try_init();
let mut core = reactor::Core::new().unwrap(); let listener = TcpListener::bind(&"127.0.0.1:5928".parse().unwrap()).unwrap();
let handle = core.handle();
let listener = TcpListener::bind(&"127.0.0.1:5928".parse().unwrap(), &handle).unwrap();
println!("listening on {:?}", listener.local_addr()); println!("listening on {:?}", listener.local_addr());
let server = listener.incoming().for_each(move |(socket, _)| { let server = listener.incoming().for_each(move |socket| {
// let socket = io_dump::Dump::to_stdout(socket); // let socket = io_dump::Dump::to_stdout(socket);
let connection = server::handshake(socket) let connection = server::handshake(socket)
@@ -64,9 +60,10 @@ pub fn main() {
Ok(()) Ok(())
}); });
handle.spawn(connection); tokio::spawn(Box::new(connection));
Ok(()) Ok(())
}); })
.map_err(|e| eprintln!("accept error: {}", e));
core.run(server).unwrap(); tokio::run(server);
} }

View File

@@ -68,27 +68,21 @@
//! extern crate futures; //! extern crate futures;
//! extern crate h2; //! extern crate h2;
//! extern crate http; //! extern crate http;
//! extern crate tokio_core; //! extern crate tokio;
//! //!
//! use h2::client; //! use h2::client;
//! //!
//! use futures::*; //! use futures::*;
//! # use futures::future::ok;
//! use http::*; //! use http::*;
//! //!
//! use tokio_core::net::TcpStream; //! use tokio::net::TcpStream;
//! use tokio_core::reactor;
//! //!
//! pub fn main() { //! pub fn main() {
//! let mut core = reactor::Core::new().unwrap();
//! let handle = core.handle();
//!
//! let addr = "127.0.0.1:5928".parse().unwrap(); //! let addr = "127.0.0.1:5928".parse().unwrap();
//! //!
//! core.run({ //! tokio::run(
//! # let _ =
//! // Establish TCP connection to the server. //! // Establish TCP connection to the server.
//! TcpStream::connect(&addr, &handle) //! TcpStream::connect(&addr)
//! .map_err(|_| { //! .map_err(|_| {
//! panic!("failed to establish TCP connection") //! panic!("failed to establish TCP connection")
//! }) //! })
@@ -98,7 +92,7 @@
//! .map_err(|_| panic!("HTTP/2.0 connection failed")); //! .map_err(|_| panic!("HTTP/2.0 connection failed"));
//! //!
//! // Spawn a new task to drive the connection state //! // Spawn a new task to drive the connection state
//! handle.spawn(connection); //! tokio::spawn(connection);
//! //!
//! // Wait until the `SendRequest` handle has available //! // Wait until the `SendRequest` handle has available
//! // capacity. //! // capacity.
@@ -139,9 +133,8 @@
//! }) //! })
//! }) //! })
//! }) //! })
//! # ; //! .map_err(|e| panic!("failed to perform HTTP/2.0 request: {:?}", e))
//! # ok::<_, ()>(()) //! )
//! }).ok().expect("failed to perform HTTP/2.0 request");
//! } //! }
//! ``` //! ```
//! //!

View File

@@ -67,27 +67,23 @@
//! extern crate futures; //! extern crate futures;
//! extern crate h2; //! extern crate h2;
//! extern crate http; //! extern crate http;
//! extern crate tokio_core; //! extern crate tokio;
//! //!
//! use futures::{Future, Stream}; //! use futures::{Future, Stream};
//! # use futures::future::ok; //! # use futures::future::ok;
//! use h2::server; //! use h2::server;
//! use http::{Response, StatusCode}; //! use http::{Response, StatusCode};
//! use tokio_core::reactor; //! use tokio::net::TcpListener;
//! use tokio_core::net::TcpListener;
//! //!
//! pub fn main () { //! pub fn main () {
//! let mut core = reactor::Core::new().unwrap();
//! let handle = core.handle();
//!
//! let addr = "127.0.0.1:5928".parse().unwrap(); //! let addr = "127.0.0.1:5928".parse().unwrap();
//! let listener = TcpListener::bind(&addr, &handle).unwrap(); //! let listener = TcpListener::bind(&addr,).unwrap();
//! //!
//! core.run({ //! tokio::run({
//! // Accept all incoming TCP connections. //! // Accept all incoming TCP connections.
//! listener.incoming().for_each(move |(socket, _)| { //! listener.incoming().for_each(move |socket| {
//! // Spawn a new task to process each connection. //! // Spawn a new task to process each connection.
//! handle.spawn({ //! tokio::spawn({
//! // Start the HTTP/2.0 connection handshake //! // Start the HTTP/2.0 connection handshake
//! server::handshake(socket) //! server::handshake(socket)
//! .and_then(|h2| { //! .and_then(|h2| {
@@ -114,8 +110,9 @@
//! //!
//! Ok(()) //! Ok(())
//! }) //! })
//! # .select(ok(())) //! .map_err(|e| panic!("failed to run HTTP/2.0 server: {:?}", e))
//! }).ok().expect("failed to run HTTP/2.0 server"); //! # .select(ok(())).map(|_|()).map_err(|_|())
//! });
//! } //! }
//! ``` //! ```
//! //!