mirror of
https://github.com/fafhrd91/actix-net
synced 2025-03-20 16:05:18 +01:00
Server: hide internal structure (#424)
This commit is contained in:
parent
62ffe5f389
commit
8c4ec34cd4
@ -1,6 +1,9 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
* Hide implementation details of `Server`. [#424]
|
||||||
|
|
||||||
|
[#424]: https://github.com/actix/actix-net/pull/424
|
||||||
|
|
||||||
|
|
||||||
## 2.0.0-beta.9 - 2021-11-15
|
## 2.0.0-beta.9 - 2021-11-15
|
||||||
|
@ -120,10 +120,7 @@ pub(crate) enum ServerCommand {
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[must_use = "futures do nothing unless you `.await` or poll them"]
|
#[must_use = "futures do nothing unless you `.await` or poll them"]
|
||||||
pub enum Server {
|
pub struct Server(Result<ServerInner, Option<io::Error>>);
|
||||||
Server(ServerInner),
|
|
||||||
Error(Option<io::Error>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Server {
|
impl Server {
|
||||||
/// Create server build.
|
/// Create server build.
|
||||||
@ -131,60 +128,17 @@ impl Server {
|
|||||||
ServerBuilder::default()
|
ServerBuilder::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn new(mut builder: ServerBuilder) -> Self {
|
pub(crate) fn new(builder: ServerBuilder) -> Self {
|
||||||
let sockets = mem::take(&mut builder.sockets)
|
Server(ServerInner::new(builder).map_err(Some))
|
||||||
.into_iter()
|
|
||||||
.map(|t| (t.0, t.2))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
// Give log information on what runtime will be used.
|
|
||||||
let is_actix = actix_rt::System::try_current().is_some();
|
|
||||||
let is_tokio = tokio::runtime::Handle::try_current().is_ok();
|
|
||||||
|
|
||||||
match (is_actix, is_tokio) {
|
|
||||||
(true, _) => info!("Actix runtime found; starting in Actix runtime"),
|
|
||||||
(_, true) => info!("Tokio runtime found; starting in existing Tokio runtime"),
|
|
||||||
(_, false) => panic!("Actix or Tokio runtime not found; halting"),
|
|
||||||
}
|
|
||||||
|
|
||||||
for (_, name, lst) in &builder.sockets {
|
|
||||||
info!(
|
|
||||||
r#"Starting service: "{}", workers: {}, listening on: {}"#,
|
|
||||||
name,
|
|
||||||
builder.threads,
|
|
||||||
lst.local_addr()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
match Accept::start(sockets, &builder) {
|
|
||||||
Ok((waker_queue, worker_handles)) => {
|
|
||||||
// construct OS signals listener future
|
|
||||||
let signals = (builder.listen_os_signals).then(Signals::new);
|
|
||||||
|
|
||||||
Self::Server(ServerInner {
|
|
||||||
cmd_tx: builder.cmd_tx.clone(),
|
|
||||||
cmd_rx: builder.cmd_rx,
|
|
||||||
signals,
|
|
||||||
waker_queue,
|
|
||||||
worker_handles,
|
|
||||||
worker_config: builder.worker_config,
|
|
||||||
services: builder.factories,
|
|
||||||
exit: builder.exit,
|
|
||||||
stop_task: None,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
Err(err) => Self::Error(Some(err)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a handle for ServerFuture that can be used to change state of actix server.
|
/// Get a handle for ServerFuture that can be used to change state of actix server.
|
||||||
///
|
///
|
||||||
/// See [ServerHandle](ServerHandle) for usage.
|
/// See [ServerHandle](ServerHandle) for usage.
|
||||||
pub fn handle(&self) -> ServerHandle {
|
pub fn handle(&self) -> ServerHandle {
|
||||||
match self {
|
match &self.0 {
|
||||||
Server::Server(inner) => ServerHandle::new(inner.cmd_tx.clone()),
|
Ok(inner) => ServerHandle::new(inner.cmd_tx.clone()),
|
||||||
Server::Error(err) => {
|
Err(err) => {
|
||||||
// TODO: i don't think this is the best way to handle server startup fail
|
// TODO: i don't think this is the best way to handle server startup fail
|
||||||
panic!(
|
panic!(
|
||||||
"server handle can not be obtained because server failed to start up: {}",
|
"server handle can not be obtained because server failed to start up: {}",
|
||||||
@ -199,12 +153,12 @@ impl Future for Server {
|
|||||||
type Output = io::Result<()>;
|
type Output = io::Result<()>;
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
match self.as_mut().get_mut() {
|
match &mut self.as_mut().get_mut().0 {
|
||||||
Self::Error(err) => Poll::Ready(Err(err
|
Err(err) => Poll::Ready(Err(err
|
||||||
.take()
|
.take()
|
||||||
.expect("Server future cannot be polled after error"))),
|
.expect("Server future cannot be polled after error"))),
|
||||||
|
|
||||||
Self::Server(inner) => {
|
Ok(inner) => {
|
||||||
// poll Signals
|
// poll Signals
|
||||||
if let Some(ref mut signals) = inner.signals {
|
if let Some(ref mut signals) = inner.signals {
|
||||||
if let Poll::Ready(signal) = Pin::new(signals).poll(cx) {
|
if let Poll::Ready(signal) = Pin::new(signals).poll(cx) {
|
||||||
@ -247,6 +201,49 @@ pub struct ServerInner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ServerInner {
|
impl ServerInner {
|
||||||
|
fn new(mut builder: ServerBuilder) -> io::Result<Self> {
|
||||||
|
let sockets = mem::take(&mut builder.sockets)
|
||||||
|
.into_iter()
|
||||||
|
.map(|t| (t.0, t.2))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
// Give log information on what runtime will be used.
|
||||||
|
let is_actix = actix_rt::System::try_current().is_some();
|
||||||
|
let is_tokio = tokio::runtime::Handle::try_current().is_ok();
|
||||||
|
|
||||||
|
match (is_actix, is_tokio) {
|
||||||
|
(true, _) => info!("Actix runtime found; starting in Actix runtime"),
|
||||||
|
(_, true) => info!("Tokio runtime found; starting in existing Tokio runtime"),
|
||||||
|
(_, false) => panic!("Actix or Tokio runtime not found; halting"),
|
||||||
|
}
|
||||||
|
|
||||||
|
for (_, name, lst) in &builder.sockets {
|
||||||
|
info!(
|
||||||
|
r#"Starting service: "{}", workers: {}, listening on: {}"#,
|
||||||
|
name,
|
||||||
|
builder.threads,
|
||||||
|
lst.local_addr()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let (waker_queue, worker_handles) = Accept::start(sockets, &builder)?;
|
||||||
|
|
||||||
|
// construct OS signals listener future
|
||||||
|
let signals = (builder.listen_os_signals).then(Signals::new);
|
||||||
|
|
||||||
|
Ok(ServerInner {
|
||||||
|
cmd_tx: builder.cmd_tx.clone(),
|
||||||
|
cmd_rx: builder.cmd_rx,
|
||||||
|
signals,
|
||||||
|
waker_queue,
|
||||||
|
worker_handles,
|
||||||
|
worker_config: builder.worker_config,
|
||||||
|
services: builder.factories,
|
||||||
|
exit: builder.exit,
|
||||||
|
stop_task: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn handle_cmd(&mut self, item: ServerCommand) -> Option<BoxFuture<'static, ()>> {
|
fn handle_cmd(&mut self, item: ServerCommand) -> Option<BoxFuture<'static, ()>> {
|
||||||
match item {
|
match item {
|
||||||
ServerCommand::Pause(tx) => {
|
ServerCommand::Pause(tx) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user