feat(rt): make tokio runtime optional

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.
This commit is contained in:
Sean McArthur
2018-04-23 16:56:26 -07:00
committed by GitHub
parent 62a5c1188a
commit d127201ef2
21 changed files with 541 additions and 410 deletions

View File

@@ -9,23 +9,22 @@
//! higher-level [Server](super) API.
use std::fmt;
use std::net::SocketAddr;
#[cfg(feature = "runtime")] use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
#[cfg(feature = "runtime")] use std::time::Duration;
use bytes::Bytes;
use futures::{Async, Future, Poll, Stream};
use futures::future::{Either, Executor};
use tokio_io::{AsyncRead, AsyncWrite};
//TODO: change these tokio:: to sub-crates
use tokio::reactor::Handle;
#[cfg(feature = "runtime")] use tokio_reactor::Handle;
use common::Exec;
use proto;
use body::{Body, Payload};
use service::{NewService, Service};
pub use super::tcp::AddrIncoming;
#[cfg(feature = "runtime")] pub use super::tcp::AddrIncoming;
/// A lower-level configuration of the HTTP protocol.
///
@@ -190,22 +189,23 @@ impl Http {
/// # Example
///
/// ```
/// # extern crate futures;
/// # extern crate hyper;
/// # extern crate tokio;
/// # extern crate tokio_io;
/// # use futures::Future;
/// # #[cfg(feature = "runtime")]
/// # extern crate tokio;
/// # use hyper::{Body, Request, Response};
/// # use hyper::service::Service;
/// # use hyper::server::conn::Http;
/// # use tokio_io::{AsyncRead, AsyncWrite};
/// # use tokio::reactor::Handle;
/// # #[cfg(feature = "runtime")]
/// # fn run<I, S>(some_io: I, some_service: S)
/// # where
/// # I: AsyncRead + AsyncWrite + Send + 'static,
/// # S: Service<ReqBody=Body, ResBody=Body> + Send + 'static,
/// # S::Future: Send
/// # {
/// # use hyper::rt::Future;
/// # use tokio::reactor::Handle;
/// let http = Http::new();
/// let conn = http.serve_connection(some_io, some_service);
///
@@ -213,7 +213,7 @@ impl Http {
/// eprintln!("server connection error: {}", e);
/// });
///
/// tokio::spawn(fut);
/// hyper::rt::spawn(fut);
/// # }
/// # fn main() {}
/// ```
@@ -252,6 +252,7 @@ impl Http {
/// to accept connections. Each connection will be processed with the
/// `new_service` object provided, creating a new service per
/// connection.
#[cfg(feature = "runtime")]
pub fn serve_addr<S, Bd>(&self, addr: &SocketAddr, new_service: S) -> ::Result<Serve<AddrIncoming, S>>
where
S: NewService<ReqBody=Body, ResBody=Bd>,
@@ -271,6 +272,7 @@ impl Http {
/// to accept connections. Each connection will be processed with the
/// `new_service` object provided, creating a new service per
/// connection.
#[cfg(feature = "runtime")]
pub fn serve_addr_handle<S, Bd>(&self, addr: &SocketAddr, handle: &Handle, new_service: S) -> ::Result<Serve<AddrIncoming, S>>
where
S: NewService<ReqBody=Body, ResBody=Bd>,
@@ -465,6 +467,7 @@ where
// ===== impl SpawnAll =====
#[cfg(feature = "runtime")]
impl<S> SpawnAll<AddrIncoming, S> {
pub(super) fn local_addr(&self) -> SocketAddr {
self.serve.incoming.local_addr()

View File

@@ -17,15 +17,14 @@
//! ## Example
//!
//! ```no_run
//! extern crate futures;
//! extern crate hyper;
//! extern crate tokio;
//!
//! use futures::Future;
//! use hyper::{Body, Response, Server};
//! use hyper::service::service_fn_ok;
//!
//! # #[cfg(feature = "runtime")]
//! fn main() {
//! # use hyper::rt::Future;
//! // Construct our SocketAddr to listen on...
//! let addr = ([127, 0, 0, 1], 3000).into();
//!
@@ -41,18 +40,20 @@
//! .serve(new_service);
//!
//! // Finally, spawn `server` onto an Executor...
//! tokio::run(server.map_err(|e| {
//! hyper::rt::run(server.map_err(|e| {
//! eprintln!("server error: {}", e);
//! }));
//! }
//! # #[cfg(not(feature = "runtime"))]
//! # fn main() {}
//! ```
pub mod conn;
mod tcp;
#[cfg(feature = "runtime")] mod tcp;
use std::fmt;
use std::net::SocketAddr;
use std::time::Duration;
#[cfg(feature = "runtime")] use std::net::SocketAddr;
#[cfg(feature = "runtime")] use std::time::Duration;
use futures::{Future, Stream, Poll};
use tokio_io::{AsyncRead, AsyncWrite};
@@ -62,8 +63,7 @@ use service::{NewService, Service};
// Renamed `Http` as `Http_` for now so that people upgrading don't see an
// error that `hyper::server::Http` is private...
use self::conn::{Http as Http_, SpawnAll};
//use self::hyper_service::HyperService;
use self::tcp::{AddrIncoming};
#[cfg(feature = "runtime")] use self::tcp::{AddrIncoming};
/// A listening HTTP server.
///
@@ -94,6 +94,7 @@ impl<I> Server<I, ()> {
}
}
#[cfg(feature = "runtime")]
impl Server<AddrIncoming, ()> {
/// Binds to the provided address, and returns a [`Builder`](Builder).
///
@@ -116,6 +117,7 @@ impl Server<AddrIncoming, ()> {
}
}
#[cfg(feature = "runtime")]
impl<S> Server<AddrIncoming, S> {
/// Returns the local address that this server is bound to.
pub fn local_addr(&self) -> SocketAddr {
@@ -176,7 +178,11 @@ impl<I> Builder<I> {
///
/// # Example
///
/// ```rust
/// ```
/// # extern crate hyper;
/// # fn main() {}
/// # #[cfg(feature = "runtime")]
/// # fn run() {
/// use hyper::{Body, Response, Server};
/// use hyper::service::service_fn_ok;
///
@@ -195,6 +201,7 @@ impl<I> Builder<I> {
/// .serve(new_service);
///
/// // Finally, spawn `server` onto an Executor...
/// # }
/// ```
pub fn serve<S, B>(self, new_service: S) -> Server<I, S>
where
@@ -215,6 +222,7 @@ impl<I> Builder<I> {
}
}
#[cfg(feature = "runtime")]
impl Builder<AddrIncoming> {
/// Set whether TCP keepalive messages are enabled on accepted connections.
///

View File

@@ -5,9 +5,8 @@ use std::time::Duration;
use futures::{Async, Future, Poll, Stream};
use futures_timer::Delay;
//TODO: change to tokio_tcp::net::TcpListener
use tokio::net::TcpListener;
use tokio::reactor::Handle;
use tokio_tcp::TcpListener;
use tokio_reactor::Handle;
use self::addr_stream::AddrStream;
@@ -170,7 +169,7 @@ mod addr_stream {
use std::net::SocketAddr;
use bytes::{Buf, BufMut};
use futures::Poll;
use tokio::net::TcpStream;
use tokio_tcp::TcpStream;
use tokio_io::{AsyncRead, AsyncWrite};