style(lib): run rustfmt and enforce in CI
This commit is contained in:
@@ -9,7 +9,10 @@
|
||||
#[cfg(feature = "stream")]
|
||||
use futures_core::Stream;
|
||||
|
||||
use crate::common::{Pin, task::{self, Poll}};
|
||||
use crate::common::{
|
||||
task::{self, Poll},
|
||||
Pin,
|
||||
};
|
||||
|
||||
/// Asynchronously accept incoming connections.
|
||||
pub trait Accept {
|
||||
@@ -19,8 +22,10 @@ pub trait Accept {
|
||||
type Error;
|
||||
|
||||
/// Poll to accept the next connection.
|
||||
fn poll_accept(self: Pin<&mut Self>, cx: &mut task::Context<'_>)
|
||||
-> Poll<Option<Result<Self::Conn, Self::Error>>>;
|
||||
fn poll_accept(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut task::Context<'_>,
|
||||
) -> Poll<Option<Result<Self::Conn, Self::Error>>>;
|
||||
}
|
||||
|
||||
/// Create an `Accept` with a polling function.
|
||||
@@ -54,12 +59,11 @@ where
|
||||
{
|
||||
type Conn = IO;
|
||||
type Error = E;
|
||||
fn poll_accept(self: Pin<&mut Self>, cx: &mut task::Context<'_>)
|
||||
-> Poll<Option<Result<Self::Conn, Self::Error>>>
|
||||
{
|
||||
unsafe {
|
||||
(self.get_unchecked_mut().0)(cx)
|
||||
}
|
||||
fn poll_accept(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut task::Context<'_>,
|
||||
) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
|
||||
unsafe { (self.get_unchecked_mut().0)(cx) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,13 +89,11 @@ where
|
||||
{
|
||||
type Conn = IO;
|
||||
type Error = E;
|
||||
fn poll_accept(self: Pin<&mut Self>, cx: &mut task::Context<'_>)
|
||||
-> Poll<Option<Result<Self::Conn, Self::Error>>>
|
||||
{
|
||||
unsafe {
|
||||
Pin::new_unchecked(&mut self.get_unchecked_mut().0)
|
||||
.poll_next(cx)
|
||||
}
|
||||
fn poll_accept(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut task::Context<'_>,
|
||||
) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
|
||||
unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0).poll_next(cx) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,29 +11,31 @@
|
||||
use std::error::Error as StdError;
|
||||
use std::fmt;
|
||||
use std::mem;
|
||||
#[cfg(feature = "tcp")] use std::net::SocketAddr;
|
||||
#[cfg(feature = "tcp")]
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use bytes::Bytes;
|
||||
use futures_core::Stream;
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
use pin_project::{pin_project, project};
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
|
||||
use super::Accept;
|
||||
use crate::body::{Body, Payload};
|
||||
use crate::common::exec::{Exec, H2Exec, NewSvcExec};
|
||||
use crate::common::io::Rewind;
|
||||
use crate::common::{Future, Pin, Poll, Unpin, task};
|
||||
use crate::common::{task, Future, Pin, Poll, Unpin};
|
||||
use crate::error::{Kind, Parse};
|
||||
use crate::proto;
|
||||
use crate::service::{MakeServiceRef, HttpService};
|
||||
use crate::service::{HttpService, MakeServiceRef};
|
||||
use crate::upgrade::Upgraded;
|
||||
use super::Accept;
|
||||
|
||||
pub(super) use self::spawn_all::NoopWatcher;
|
||||
use self::spawn_all::NewSvcTask;
|
||||
pub(super) use self::spawn_all::NoopWatcher;
|
||||
pub(super) use self::spawn_all::Watcher;
|
||||
pub(super) use self::upgrades::UpgradeableConnection;
|
||||
|
||||
#[cfg(feature = "tcp")] pub use super::tcp::{AddrIncoming, AddrStream};
|
||||
#[cfg(feature = "tcp")]
|
||||
pub use super::tcp::{AddrIncoming, AddrStream};
|
||||
|
||||
// Our defaults are chosen for the "majority" case, which usually are not
|
||||
// resource contrained, and so the spec default of 64kb can be too limiting
|
||||
@@ -161,7 +163,7 @@ impl<E> Unpin for Fallback<E> {}
|
||||
/// This allows taking apart a `Connection` at a later time, in order to
|
||||
/// reclaim the IO object, and additional related pieces.
|
||||
#[derive(Debug)]
|
||||
pub struct Parts<T, S> {
|
||||
pub struct Parts<T, S> {
|
||||
/// The original IO object used in the handshake.
|
||||
pub io: T,
|
||||
/// A buffer of bytes that have been read but not processed as HTTP.
|
||||
@@ -274,7 +276,10 @@ impl<E> Http<E> {
|
||||
/// Passing `None` will do nothing.
|
||||
///
|
||||
/// If not set, hyper will use a default.
|
||||
pub fn http2_initial_connection_window_size(&mut self, sz: impl Into<Option<u32>>) -> &mut Self {
|
||||
pub fn http2_initial_connection_window_size(
|
||||
&mut self,
|
||||
sz: impl Into<Option<u32>>,
|
||||
) -> &mut Self {
|
||||
if let Some(sz) = sz.into() {
|
||||
self.h2_builder.initial_connection_window_size(sz);
|
||||
}
|
||||
@@ -374,7 +379,7 @@ impl<E> Http<E> {
|
||||
/// ```
|
||||
pub fn serve_connection<S, I, Bd>(&self, io: I, service: S) -> Connection<I, S, E>
|
||||
where
|
||||
S: HttpService<Body, ResBody=Bd>,
|
||||
S: HttpService<Body, ResBody = Bd>,
|
||||
S::Error: Into<Box<dyn StdError + Send + Sync>>,
|
||||
Bd: Payload,
|
||||
Bd::Data: Unpin,
|
||||
@@ -402,7 +407,8 @@ impl<E> Http<E> {
|
||||
}
|
||||
ConnectionMode::H2Only => {
|
||||
let rewind_io = Rewind::new(io);
|
||||
let h2 = proto::h2::Server::new(rewind_io, service, &self.h2_builder, self.exec.clone());
|
||||
let h2 =
|
||||
proto::h2::Server::new(rewind_io, service, &self.h2_builder, self.exec.clone());
|
||||
ProtoServer::H2(h2)
|
||||
}
|
||||
};
|
||||
@@ -419,14 +425,10 @@ impl<E> Http<E> {
|
||||
|
||||
pub(super) fn serve<I, IO, IE, S, Bd>(&self, incoming: I, make_service: S) -> Serve<I, S, E>
|
||||
where
|
||||
I: Accept<Conn=IO, Error=IE>,
|
||||
I: Accept<Conn = IO, Error = IE>,
|
||||
IE: Into<Box<dyn StdError + Send + Sync>>,
|
||||
IO: AsyncRead + AsyncWrite + Unpin,
|
||||
S: MakeServiceRef<
|
||||
IO,
|
||||
Body,
|
||||
ResBody=Bd,
|
||||
>,
|
||||
S: MakeServiceRef<IO, Body, ResBody = Bd>,
|
||||
S::Error: Into<Box<dyn StdError + Send + Sync>>,
|
||||
Bd: Payload,
|
||||
E: H2Exec<<S::Service as HttpService<Body>>::Future, Bd>,
|
||||
@@ -439,12 +441,11 @@ impl<E> Http<E> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ===== impl Connection =====
|
||||
|
||||
impl<I, B, S, E> Connection<I, S, E>
|
||||
where
|
||||
S: HttpService<Body, ResBody=B>,
|
||||
S: HttpService<Body, ResBody = B>,
|
||||
S::Error: Into<Box<dyn StdError + Send + Sync>>,
|
||||
I: AsyncRead + AsyncWrite + Unpin,
|
||||
B: Payload + 'static,
|
||||
@@ -459,7 +460,7 @@ where
|
||||
match self.project().conn.as_mut().unwrap() {
|
||||
ProtoServer::H1(ref mut h1) => {
|
||||
h1.disable_keep_alive();
|
||||
},
|
||||
}
|
||||
ProtoServer::H2(ref mut h2) => {
|
||||
h2.graceful_shutdown();
|
||||
}
|
||||
@@ -476,7 +477,8 @@ where
|
||||
/// # Panics
|
||||
/// This method will panic if this connection is using an h2 protocol.
|
||||
pub fn into_parts(self) -> Parts<I, S> {
|
||||
self.try_into_parts().unwrap_or_else(|| panic!("h2 cannot into_inner"))
|
||||
self.try_into_parts()
|
||||
.unwrap_or_else(|| panic!("h2 cannot into_inner"))
|
||||
}
|
||||
|
||||
/// Return the inner IO object, and additional information, if available.
|
||||
@@ -492,7 +494,7 @@ where
|
||||
service: dispatch.into_service(),
|
||||
_inner: (),
|
||||
})
|
||||
},
|
||||
}
|
||||
ProtoServer::H2(_h2) => None,
|
||||
}
|
||||
}
|
||||
@@ -521,22 +523,20 @@ where
|
||||
};
|
||||
match ready!(polled) {
|
||||
Ok(x) => return Poll::Ready(Ok(x)),
|
||||
Err(e) => {
|
||||
match *e.kind() {
|
||||
Kind::Parse(Parse::VersionH2) if self.fallback.to_h2() => {
|
||||
self.upgrade_h2();
|
||||
continue;
|
||||
}
|
||||
_ => return Poll::Ready(Err(e)),
|
||||
Err(e) => match *e.kind() {
|
||||
Kind::Parse(Parse::VersionH2) if self.fallback.to_h2() => {
|
||||
self.upgrade_h2();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
_ => return Poll::Ready(Err(e)),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Prevent shutdown of the underlying IO object at the end of service the request,
|
||||
/// instead run `into_parts`. This is a convenience wrapper over `poll_without_shutdown`.
|
||||
pub fn without_shutdown(self) -> impl Future<Output=crate::Result<Parts<I, S>>>
|
||||
pub fn without_shutdown(self) -> impl Future<Output = crate::Result<Parts<I, S>>>
|
||||
where
|
||||
S: Unpin,
|
||||
S::Future: Unpin,
|
||||
@@ -554,9 +554,7 @@ where
|
||||
let conn = self.conn.take();
|
||||
|
||||
let (io, read_buf, dispatch) = match conn.unwrap() {
|
||||
ProtoServer::H1(h1) => {
|
||||
h1.into_inner()
|
||||
},
|
||||
ProtoServer::H1(h1) => h1.into_inner(),
|
||||
ProtoServer::H2(_h2) => {
|
||||
panic!("h2 cannot into_inner");
|
||||
}
|
||||
@@ -567,12 +565,7 @@ where
|
||||
Fallback::ToHttp2(ref builder, ref exec) => (builder, exec),
|
||||
Fallback::Http1Only => unreachable!("upgrade_h2 with Fallback::Http1Only"),
|
||||
};
|
||||
let h2 = proto::h2::Server::new(
|
||||
rewind_io,
|
||||
dispatch.into_service(),
|
||||
builder,
|
||||
exec.clone(),
|
||||
);
|
||||
let h2 = proto::h2::Server::new(rewind_io, dispatch.into_service(), builder, exec.clone());
|
||||
|
||||
debug_assert!(self.conn.is_none());
|
||||
self.conn = Some(ProtoServer::H2(h2));
|
||||
@@ -585,15 +578,13 @@ where
|
||||
where
|
||||
I: Send,
|
||||
{
|
||||
UpgradeableConnection {
|
||||
inner: self,
|
||||
}
|
||||
UpgradeableConnection { inner: self }
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, B, S, E> Future for Connection<I, S, E>
|
||||
where
|
||||
S: HttpService<Body, ResBody=B>,
|
||||
S: HttpService<Body, ResBody = B>,
|
||||
S::Error: Into<Box<dyn StdError + Send + Sync>>,
|
||||
I: AsyncRead + AsyncWrite + Unpin + 'static,
|
||||
B: Payload + 'static,
|
||||
@@ -614,16 +605,14 @@ where
|
||||
pending.manual();
|
||||
}
|
||||
return Poll::Ready(Ok(()));
|
||||
},
|
||||
Err(e) => {
|
||||
match *e.kind() {
|
||||
Kind::Parse(Parse::VersionH2) if self.fallback.to_h2() => {
|
||||
self.upgrade_h2();
|
||||
continue;
|
||||
}
|
||||
_ => return Poll::Ready(Err(e)),
|
||||
}
|
||||
}
|
||||
Err(e) => match *e.kind() {
|
||||
Kind::Parse(Parse::VersionH2) if self.fallback.to_h2() => {
|
||||
self.upgrade_h2();
|
||||
continue;
|
||||
}
|
||||
_ => return Poll::Ready(Err(e)),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -634,8 +623,7 @@ where
|
||||
S: HttpService<Body>,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("Connection")
|
||||
.finish()
|
||||
f.debug_struct("Connection").finish()
|
||||
}
|
||||
}
|
||||
// ===== impl Serve =====
|
||||
@@ -655,25 +643,23 @@ impl<I, S, E> Serve<I, S, E> {
|
||||
|
||||
/// Spawn all incoming connections onto the executor in `Http`.
|
||||
pub(super) fn spawn_all(self) -> SpawnAll<I, S, E> {
|
||||
SpawnAll {
|
||||
serve: self,
|
||||
}
|
||||
SpawnAll { serve: self }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
impl<I, IO, IE, S, B, E> Serve<I, S, E>
|
||||
where
|
||||
I: Accept<Conn=IO, Error=IE>,
|
||||
I: Accept<Conn = IO, Error = IE>,
|
||||
IO: AsyncRead + AsyncWrite + Unpin,
|
||||
IE: Into<Box<dyn StdError + Send + Sync>>,
|
||||
S: MakeServiceRef<IO, Body, ResBody=B>,
|
||||
S: MakeServiceRef<IO, Body, ResBody = B>,
|
||||
B: Payload,
|
||||
E: H2Exec<<S::Service as HttpService<Body>>::Future, B>,
|
||||
{
|
||||
fn poll_next_(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Option<crate::Result<Connecting<IO, S::Future, E>>>> {
|
||||
fn poll_next_(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut task::Context<'_>,
|
||||
) -> Poll<Option<crate::Result<Connecting<IO, S::Future, E>>>> {
|
||||
let me = self.project();
|
||||
match ready!(me.make_service.poll_ready_ref(cx)) {
|
||||
Ok(()) => (),
|
||||
@@ -700,10 +686,10 @@ where
|
||||
// deprecated
|
||||
impl<I, IO, IE, S, B, E> Stream for Serve<I, S, E>
|
||||
where
|
||||
I: Accept<Conn=IO, Error=IE>,
|
||||
I: Accept<Conn = IO, Error = IE>,
|
||||
IO: AsyncRead + AsyncWrite + Unpin,
|
||||
IE: Into<Box<dyn StdError + Send + Sync>>,
|
||||
S: MakeServiceRef<IO, Body, ResBody=B>,
|
||||
S: MakeServiceRef<IO, Body, ResBody = B>,
|
||||
B: Payload,
|
||||
E: H2Exec<<S::Service as HttpService<Body>>::Future, B>,
|
||||
{
|
||||
@@ -716,12 +702,11 @@ where
|
||||
|
||||
// ===== impl Connecting =====
|
||||
|
||||
|
||||
impl<I, F, S, FE, E, B> Future for Connecting<I, F, E>
|
||||
where
|
||||
I: AsyncRead + AsyncWrite + Unpin,
|
||||
F: Future<Output=Result<S, FE>>,
|
||||
S: HttpService<Body, ResBody=B>,
|
||||
F: Future<Output = Result<S, FE>>,
|
||||
S: HttpService<Body, ResBody = B>,
|
||||
B: Payload,
|
||||
B::Data: Unpin,
|
||||
E: H2Exec<S::Future, B>,
|
||||
@@ -753,18 +738,18 @@ impl<I, S, E> SpawnAll<I, S, E> {
|
||||
|
||||
impl<I, IO, IE, S, B, E> SpawnAll<I, S, E>
|
||||
where
|
||||
I: Accept<Conn=IO, Error=IE>,
|
||||
I: Accept<Conn = IO, Error = IE>,
|
||||
IE: Into<Box<dyn StdError + Send + Sync>>,
|
||||
IO: AsyncRead + AsyncWrite + Unpin + Send + 'static,
|
||||
S: MakeServiceRef<
|
||||
IO,
|
||||
Body,
|
||||
ResBody=B,
|
||||
>,
|
||||
S: MakeServiceRef<IO, Body, ResBody = B>,
|
||||
B: Payload,
|
||||
E: H2Exec<<S::Service as HttpService<Body>>::Future, B>,
|
||||
{
|
||||
pub(super) fn poll_watch<W>(self: Pin<&mut Self>, cx: &mut task::Context<'_>, watcher: &W) -> Poll<crate::Result<()>>
|
||||
pub(super) fn poll_watch<W>(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut task::Context<'_>,
|
||||
watcher: &W,
|
||||
) -> Poll<crate::Result<()>>
|
||||
where
|
||||
E: NewSvcExec<IO, S::Future, S::Service, E, W>,
|
||||
W: Watcher<IO, S::Service, E>,
|
||||
@@ -773,7 +758,12 @@ where
|
||||
loop {
|
||||
if let Some(connecting) = ready!(me.serve.as_mut().poll_next_(cx)?) {
|
||||
let fut = NewSvcTask::new(connecting, watcher.clone());
|
||||
me.serve.as_mut().project().protocol.exec.execute_new_svc(fut);
|
||||
me.serve
|
||||
.as_mut()
|
||||
.project()
|
||||
.protocol
|
||||
.exec
|
||||
.execute_new_svc(fut);
|
||||
} else {
|
||||
return Poll::Ready(Ok(()));
|
||||
}
|
||||
@@ -808,11 +798,11 @@ pub(crate) mod spawn_all {
|
||||
use std::error::Error as StdError;
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
|
||||
use super::{Connecting, UpgradeableConnection};
|
||||
use crate::body::{Body, Payload};
|
||||
use crate::common::exec::H2Exec;
|
||||
use crate::common::{Future, Pin, Poll, Unpin, task};
|
||||
use crate::common::{task, Future, Pin, Poll, Unpin};
|
||||
use crate::service::HttpService;
|
||||
use super::{Connecting, UpgradeableConnection};
|
||||
use pin_project::{pin_project, project};
|
||||
|
||||
// Used by `SpawnAll` to optionally watch a `Connection` future.
|
||||
@@ -881,9 +871,9 @@ pub(crate) mod spawn_all {
|
||||
impl<I, N, S, NE, B, E, W> Future for NewSvcTask<I, N, S, E, W>
|
||||
where
|
||||
I: AsyncRead + AsyncWrite + Unpin + Send + 'static,
|
||||
N: Future<Output=Result<S, NE>>,
|
||||
N: Future<Output = Result<S, NE>>,
|
||||
NE: Into<Box<dyn StdError + Send + Sync>>,
|
||||
S: HttpService<Body, ResBody=B>,
|
||||
S: HttpService<Body, ResBody = B>,
|
||||
B: Payload,
|
||||
B::Data: Unpin,
|
||||
E: H2Exec<S::Future, B>,
|
||||
@@ -914,15 +904,13 @@ pub(crate) mod spawn_all {
|
||||
};
|
||||
let connected = watcher.watch(conn.with_upgrades());
|
||||
State::Connected(connected)
|
||||
},
|
||||
}
|
||||
State::Connected(future) => {
|
||||
return future
|
||||
.poll(cx)
|
||||
.map(|res| {
|
||||
if let Err(err) = res {
|
||||
debug!("connection error: {}", err);
|
||||
}
|
||||
});
|
||||
return future.poll(cx).map(|res| {
|
||||
if let Err(err) = res {
|
||||
debug!("connection error: {}", err);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -951,7 +939,7 @@ mod upgrades {
|
||||
|
||||
impl<I, B, S, E> UpgradeableConnection<I, S, E>
|
||||
where
|
||||
S: HttpService<Body, ResBody=B>,
|
||||
S: HttpService<Body, ResBody = B>,
|
||||
S::Error: Into<Box<dyn StdError + Send + Sync>>,
|
||||
I: AsyncRead + AsyncWrite + Unpin,
|
||||
B: Payload + 'static,
|
||||
@@ -969,7 +957,7 @@ mod upgrades {
|
||||
|
||||
impl<I, B, S, E> Future for UpgradeableConnection<I, S, E>
|
||||
where
|
||||
S: HttpService<Body, ResBody=B>,
|
||||
S: HttpService<Body, ResBody = B>,
|
||||
S::Error: Into<Box<dyn StdError + Send + Sync>>,
|
||||
I: AsyncRead + AsyncWrite + Unpin + Send + 'static,
|
||||
B: Payload + 'static,
|
||||
@@ -991,19 +979,16 @@ mod upgrades {
|
||||
let (io, buf, _) = h1.into_inner();
|
||||
pending.fulfill(Upgraded::new(Box::new(io), buf));
|
||||
return Poll::Ready(Ok(()));
|
||||
},
|
||||
Err(e) => {
|
||||
match *e.kind() {
|
||||
Kind::Parse(Parse::VersionH2) if self.inner.fallback.to_h2() => {
|
||||
self.inner.upgrade_h2();
|
||||
continue;
|
||||
}
|
||||
_ => return Poll::Ready(Err(e)),
|
||||
}
|
||||
}
|
||||
Err(e) => match *e.kind() {
|
||||
Kind::Parse(Parse::VersionH2) if self.inner.fallback.to_h2() => {
|
||||
self.inner.upgrade_h2();
|
||||
continue;
|
||||
}
|
||||
_ => return Poll::Ready(Err(e)),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -51,27 +51,31 @@
|
||||
pub mod accept;
|
||||
pub mod conn;
|
||||
mod shutdown;
|
||||
#[cfg(feature = "tcp")] mod tcp;
|
||||
#[cfg(feature = "tcp")]
|
||||
mod tcp;
|
||||
|
||||
use std::error::Error as StdError;
|
||||
use std::fmt;
|
||||
#[cfg(feature = "tcp")] use std::net::{SocketAddr, TcpListener as StdTcpListener};
|
||||
#[cfg(feature = "tcp")]
|
||||
use std::net::{SocketAddr, TcpListener as StdTcpListener};
|
||||
|
||||
#[cfg(feature = "tcp")] use std::time::Duration;
|
||||
#[cfg(feature = "tcp")]
|
||||
use std::time::Duration;
|
||||
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
use pin_project::pin_project;
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
|
||||
use self::accept::Accept;
|
||||
use crate::body::{Body, Payload};
|
||||
use crate::common::exec::{Exec, H2Exec, NewSvcExec};
|
||||
use crate::common::{Future, Pin, Poll, Unpin, task};
|
||||
use crate::service::{MakeServiceRef, HttpService};
|
||||
use self::accept::Accept;
|
||||
use crate::common::{task, Future, Pin, Poll, Unpin};
|
||||
use crate::service::{HttpService, MakeServiceRef};
|
||||
// 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_, NoopWatcher, SpawnAll};
|
||||
use self::shutdown::{Graceful, GracefulWatcher};
|
||||
#[cfg(feature = "tcp")] use self::tcp::AddrIncoming;
|
||||
#[cfg(feature = "tcp")]
|
||||
use self::tcp::AddrIncoming;
|
||||
|
||||
/// A listening HTTP server that accepts connections in both HTTP1 and HTTP2 by default.
|
||||
///
|
||||
@@ -113,23 +117,20 @@ impl Server<AddrIncoming, ()> {
|
||||
/// This method will panic if binding to the address fails. For a method
|
||||
/// to bind to an address and return a `Result`, see `Server::try_bind`.
|
||||
pub fn bind(addr: &SocketAddr) -> Builder<AddrIncoming> {
|
||||
let incoming = AddrIncoming::new(addr)
|
||||
.unwrap_or_else(|e| {
|
||||
panic!("error binding to {}: {}", addr, e);
|
||||
});
|
||||
let incoming = AddrIncoming::new(addr).unwrap_or_else(|e| {
|
||||
panic!("error binding to {}: {}", addr, e);
|
||||
});
|
||||
Server::builder(incoming)
|
||||
}
|
||||
|
||||
/// Tries to bind to the provided address, and returns a [`Builder`](Builder).
|
||||
pub fn try_bind(addr: &SocketAddr) -> crate::Result<Builder<AddrIncoming>> {
|
||||
AddrIncoming::new(addr)
|
||||
.map(Server::builder)
|
||||
AddrIncoming::new(addr).map(Server::builder)
|
||||
}
|
||||
|
||||
/// Create a new instance from a `std::net::TcpListener` instance.
|
||||
pub fn from_tcp(listener: StdTcpListener) -> Result<Builder<AddrIncoming>, crate::Error> {
|
||||
AddrIncoming::from_std(listener)
|
||||
.map(Server::builder)
|
||||
AddrIncoming::from_std(listener).map(Server::builder)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,10 +144,10 @@ impl<S, E> Server<AddrIncoming, S, E> {
|
||||
|
||||
impl<I, IO, IE, S, E, B> Server<I, S, E>
|
||||
where
|
||||
I: Accept<Conn=IO, Error=IE>,
|
||||
I: Accept<Conn = IO, Error = IE>,
|
||||
IE: Into<Box<dyn StdError + Send + Sync>>,
|
||||
IO: AsyncRead + AsyncWrite + Unpin + Send + 'static,
|
||||
S: MakeServiceRef<IO, Body, ResBody=B>,
|
||||
S: MakeServiceRef<IO, Body, ResBody = B>,
|
||||
S::Error: Into<Box<dyn StdError + Send + Sync>>,
|
||||
B: Payload,
|
||||
B::Data: Unpin,
|
||||
@@ -191,7 +192,7 @@ where
|
||||
/// ```
|
||||
pub fn with_graceful_shutdown<F>(self, signal: F) -> Graceful<I, S, F, E>
|
||||
where
|
||||
F: Future<Output=()>
|
||||
F: Future<Output = ()>,
|
||||
{
|
||||
Graceful::new(self.spawn_all, signal)
|
||||
}
|
||||
@@ -199,10 +200,10 @@ where
|
||||
|
||||
impl<I, IO, IE, S, B, E> Future for Server<I, S, E>
|
||||
where
|
||||
I: Accept<Conn=IO, Error=IE>,
|
||||
I: Accept<Conn = IO, Error = IE>,
|
||||
IE: Into<Box<dyn StdError + Send + Sync>>,
|
||||
IO: AsyncRead + AsyncWrite + Unpin + Send + 'static,
|
||||
S: MakeServiceRef<IO, Body, ResBody=B>,
|
||||
S: MakeServiceRef<IO, Body, ResBody = B>,
|
||||
S::Error: Into<Box<dyn StdError + Send + Sync>>,
|
||||
B: Payload,
|
||||
B::Data: Unpin,
|
||||
@@ -231,10 +232,7 @@ impl<I, E> Builder<I, E> {
|
||||
///
|
||||
/// For a more convenient constructor, see [`Server::bind`](Server::bind).
|
||||
pub fn new(incoming: I, protocol: Http_<E>) -> Self {
|
||||
Builder {
|
||||
incoming,
|
||||
protocol,
|
||||
}
|
||||
Builder { incoming, protocol }
|
||||
}
|
||||
|
||||
/// Sets whether to use keep-alive for HTTP/1 connections.
|
||||
@@ -245,7 +243,6 @@ impl<I, E> Builder<I, E> {
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
/// Set whether HTTP/1 connections should support half-closures.
|
||||
///
|
||||
/// Clients can chose to shutdown their write-side while waiting
|
||||
@@ -319,7 +316,8 @@ impl<I, E> Builder<I, E> {
|
||||
///
|
||||
/// If not set, hyper will use a default.
|
||||
pub fn http2_initial_connection_window_size(mut self, sz: impl Into<Option<u32>>) -> Self {
|
||||
self.protocol.http2_initial_connection_window_size(sz.into());
|
||||
self.protocol
|
||||
.http2_initial_connection_window_size(sz.into());
|
||||
self
|
||||
}
|
||||
|
||||
@@ -387,7 +385,7 @@ impl<I, E> Builder<I, E> {
|
||||
I: Accept,
|
||||
I::Error: Into<Box<dyn StdError + Send + Sync>>,
|
||||
I::Conn: AsyncRead + AsyncWrite + Unpin + Send + 'static,
|
||||
S: MakeServiceRef<I::Conn, Body, ResBody=B>,
|
||||
S: MakeServiceRef<I::Conn, Body, ResBody = B>,
|
||||
S::Error: Into<Box<dyn StdError + Send + Sync>>,
|
||||
B: Payload,
|
||||
B::Data: Unpin,
|
||||
@@ -396,9 +394,7 @@ impl<I, E> Builder<I, E> {
|
||||
{
|
||||
let serve = self.protocol.serve(self.incoming, new_service);
|
||||
let spawn_all = serve.spawn_all();
|
||||
Server {
|
||||
spawn_all,
|
||||
}
|
||||
Server { spawn_all }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -440,4 +436,3 @@ impl<E> Builder<AddrIncoming, E> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
use std::error::Error as StdError;
|
||||
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
use pin_project::{pin_project, project};
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
|
||||
use super::conn::{SpawnAll, UpgradeableConnection, Watcher};
|
||||
use super::Accept;
|
||||
use crate::body::{Body, Payload};
|
||||
use crate::common::drain::{self, Draining, Signal, Watch, Watching};
|
||||
use crate::common::exec::{H2Exec, NewSvcExec};
|
||||
use crate::common::{Future, Pin, Poll, Unpin, task};
|
||||
use crate::service::{MakeServiceRef, HttpService};
|
||||
use super::Accept;
|
||||
use super::conn::{SpawnAll, UpgradeableConnection, Watcher};
|
||||
use crate::common::{task, Future, Pin, Poll, Unpin};
|
||||
use crate::service::{HttpService, MakeServiceRef};
|
||||
|
||||
#[allow(missing_debug_implementations)]
|
||||
#[pin_project]
|
||||
@@ -43,17 +43,16 @@ impl<I, S, F, E> Graceful<I, S, F, E> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<I, IO, IE, S, B, F, E> Future for Graceful<I, S, F, E>
|
||||
where
|
||||
I: Accept<Conn=IO, Error=IE>,
|
||||
I: Accept<Conn = IO, Error = IE>,
|
||||
IE: Into<Box<dyn StdError + Send + Sync>>,
|
||||
IO: AsyncRead + AsyncWrite + Unpin + Send + 'static,
|
||||
S: MakeServiceRef<IO, Body, ResBody=B>,
|
||||
S: MakeServiceRef<IO, Body, ResBody = B>,
|
||||
S::Error: Into<Box<dyn StdError + Send + Sync>>,
|
||||
B: Payload,
|
||||
B::Data: Unpin,
|
||||
F: Future<Output=()>,
|
||||
F: Future<Output = ()>,
|
||||
E: H2Exec<<S::Service as HttpService<Body>>::Future, B>,
|
||||
E: NewSvcExec<IO, S::Future, S::Service, E, GracefulWatcher>,
|
||||
{
|
||||
@@ -73,20 +72,13 @@ where
|
||||
} => match signal.poll(cx) {
|
||||
Poll::Ready(()) => {
|
||||
debug!("signal received, starting graceful shutdown");
|
||||
let sig = drain
|
||||
.take()
|
||||
.expect("drain channel")
|
||||
.0;
|
||||
let sig = drain.take().expect("drain channel").0;
|
||||
State::Draining(sig.drain())
|
||||
},
|
||||
}
|
||||
Poll::Pending => {
|
||||
let watch = drain
|
||||
.as_ref()
|
||||
.expect("drain channel")
|
||||
.1
|
||||
.clone();
|
||||
let watch = drain.as_ref().expect("drain channel").1.clone();
|
||||
return spawn_all.poll_watch(cx, &GracefulWatcher(watch));
|
||||
},
|
||||
}
|
||||
},
|
||||
State::Draining(ref mut draining) => {
|
||||
return Pin::new(draining).poll(cx).map(Ok);
|
||||
@@ -109,13 +101,11 @@ where
|
||||
<S::ResBody as Payload>::Data: Unpin,
|
||||
E: H2Exec<S::Future, S::ResBody>,
|
||||
{
|
||||
type Future = Watching<UpgradeableConnection<I, S, E>, fn(Pin<&mut UpgradeableConnection<I, S, E>>)>;
|
||||
type Future =
|
||||
Watching<UpgradeableConnection<I, S, E>, fn(Pin<&mut UpgradeableConnection<I, S, E>>)>;
|
||||
|
||||
fn watch(&self, conn: UpgradeableConnection<I, S, E>) -> Self::Future {
|
||||
self
|
||||
.0
|
||||
.clone()
|
||||
.watch(conn, on_drain)
|
||||
self.0.clone().watch(conn, on_drain)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,4 +120,3 @@ where
|
||||
{
|
||||
conn.graceful_shutdown()
|
||||
}
|
||||
|
||||
|
||||
@@ -7,10 +7,10 @@ use futures_util::FutureExt as _;
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::time::Delay;
|
||||
|
||||
use crate::common::{Future, Pin, Poll, task};
|
||||
use crate::common::{task, Future, Pin, Poll};
|
||||
|
||||
use super::Accept;
|
||||
pub use self::addr_stream::AddrStream;
|
||||
use super::Accept;
|
||||
|
||||
/// A stream of connections from binding to an address.
|
||||
#[must_use = "streams do nothing unless polled"]
|
||||
@@ -25,15 +25,13 @@ pub struct AddrIncoming {
|
||||
|
||||
impl AddrIncoming {
|
||||
pub(super) fn new(addr: &SocketAddr) -> crate::Result<Self> {
|
||||
let std_listener = StdTcpListener::bind(addr)
|
||||
.map_err(crate::Error::new_listen)?;
|
||||
let std_listener = StdTcpListener::bind(addr).map_err(crate::Error::new_listen)?;
|
||||
|
||||
AddrIncoming::from_std(std_listener)
|
||||
}
|
||||
|
||||
pub(super) fn from_std(std_listener: StdTcpListener) -> crate::Result<Self> {
|
||||
let listener = TcpListener::from_std(std_listener)
|
||||
.map_err(crate::Error::new_listen)?;
|
||||
let listener = TcpListener::from_std(std_listener).map_err(crate::Error::new_listen)?;
|
||||
let addr = listener.local_addr().map_err(crate::Error::new_listen)?;
|
||||
Ok(AddrIncoming {
|
||||
listener,
|
||||
@@ -115,7 +113,7 @@ impl AddrIncoming {
|
||||
trace!("error trying to set TCP nodelay: {}", e);
|
||||
}
|
||||
return Poll::Ready(Ok(AddrStream::new(socket, addr)));
|
||||
},
|
||||
}
|
||||
Poll::Pending => return Poll::Pending,
|
||||
Poll::Ready(Err(e)) => {
|
||||
// Connection errors can be ignored directly, continue by
|
||||
@@ -134,17 +132,17 @@ impl AddrIncoming {
|
||||
match Pin::new(&mut timeout).poll(cx) {
|
||||
Poll::Ready(()) => {
|
||||
// Wow, it's been a second already? Ok then...
|
||||
continue
|
||||
},
|
||||
continue;
|
||||
}
|
||||
Poll::Pending => {
|
||||
self.timeout = Some(timeout);
|
||||
return Poll::Pending;
|
||||
},
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Poll::Ready(Err(e));
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -154,7 +152,10 @@ impl Accept for AddrIncoming {
|
||||
type Conn = AddrStream;
|
||||
type Error = io::Error;
|
||||
|
||||
fn poll_accept(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
|
||||
fn poll_accept(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut task::Context<'_>,
|
||||
) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
|
||||
let result = ready!(self.poll_next_(cx));
|
||||
Poll::Ready(Some(result))
|
||||
}
|
||||
@@ -169,9 +170,9 @@ impl Accept for AddrIncoming {
|
||||
/// and EMFILE. Otherwise, could enter into tight loop.
|
||||
fn is_connection_error(e: &io::Error) -> bool {
|
||||
match e.kind() {
|
||||
io::ErrorKind::ConnectionRefused |
|
||||
io::ErrorKind::ConnectionAborted |
|
||||
io::ErrorKind::ConnectionReset => true,
|
||||
io::ErrorKind::ConnectionRefused
|
||||
| io::ErrorKind::ConnectionAborted
|
||||
| io::ErrorKind::ConnectionReset => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -188,14 +189,13 @@ impl fmt::Debug for AddrIncoming {
|
||||
}
|
||||
|
||||
mod addr_stream {
|
||||
use bytes::{Buf, BufMut};
|
||||
use std::io;
|
||||
use std::net::SocketAddr;
|
||||
use bytes::{Buf, BufMut};
|
||||
use tokio::net::TcpStream;
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
use crate::common::{Pin, Poll, task};
|
||||
|
||||
use crate::common::{task, Pin, Poll};
|
||||
|
||||
/// A transport returned yieled by `AddrIncoming`.
|
||||
#[derive(Debug)]
|
||||
@@ -226,29 +226,48 @@ mod addr_stream {
|
||||
}
|
||||
|
||||
impl AsyncRead for AddrStream {
|
||||
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [std::mem::MaybeUninit<u8>]) -> bool {
|
||||
unsafe fn prepare_uninitialized_buffer(
|
||||
&self,
|
||||
buf: &mut [std::mem::MaybeUninit<u8>],
|
||||
) -> bool {
|
||||
self.inner.prepare_uninitialized_buffer(buf)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn poll_read(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
|
||||
fn poll_read(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut task::Context<'_>,
|
||||
buf: &mut [u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
Pin::new(&mut self.inner).poll_read(cx, buf)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn poll_read_buf<B: BufMut>(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>, buf: &mut B) -> Poll<io::Result<usize>> {
|
||||
fn poll_read_buf<B: BufMut>(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut task::Context<'_>,
|
||||
buf: &mut B,
|
||||
) -> Poll<io::Result<usize>> {
|
||||
Pin::new(&mut self.inner).poll_read_buf(cx, buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncWrite for AddrStream {
|
||||
#[inline]
|
||||
fn poll_write(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
|
||||
fn poll_write(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut task::Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
Pin::new(&mut self.inner).poll_write(cx, buf)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn poll_write_buf<B: Buf>(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>, buf: &mut B) -> Poll<io::Result<usize>> {
|
||||
fn poll_write_buf<B: Buf>(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut task::Context<'_>,
|
||||
buf: &mut B,
|
||||
) -> Poll<io::Result<usize>> {
|
||||
Pin::new(&mut self.inner).poll_write_buf(cx, buf)
|
||||
}
|
||||
|
||||
@@ -259,7 +278,10 @@ mod addr_stream {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<io::Result<()>> {
|
||||
fn poll_shutdown(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut task::Context<'_>,
|
||||
) -> Poll<io::Result<()>> {
|
||||
Pin::new(&mut self.inner).poll_shutdown(cx)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user