1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-24 01:11:07 +01:00

Refactor actix_server WorkerState::Restarting enum variant. (#306)

Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
fakeshadow 2021-04-03 11:40:12 -07:00 committed by GitHub
parent 39d1f282f7
commit fd3e5fba02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 43 deletions

View File

@ -7,14 +7,14 @@ use actix_service::{
fn_service, IntoServiceFactory as IntoBaseServiceFactory, fn_service, IntoServiceFactory as IntoBaseServiceFactory,
ServiceFactory as BaseServiceFactory, ServiceFactory as BaseServiceFactory,
}; };
use actix_utils::counter::CounterGuard; use actix_utils::{counter::CounterGuard, future::ready};
use futures_core::future::LocalBoxFuture; use futures_core::future::LocalBoxFuture;
use log::error; use log::error;
use crate::builder::bind_addr; use crate::builder::bind_addr;
use crate::service::{BoxedServerService, InternalServiceFactory, StreamService}; use crate::service::{BoxedServerService, InternalServiceFactory, StreamService};
use crate::socket::{MioStream, MioTcpListener, StdSocketAddr, StdTcpListener, ToSocketAddrs}; use crate::socket::{MioStream, MioTcpListener, StdSocketAddr, StdTcpListener, ToSocketAddrs};
use crate::{ready, Token}; use crate::Token;
pub struct ServiceConfig { pub struct ServiceConfig {
pub(crate) services: Vec<(String, MioTcpListener)>, pub(crate) services: Vec<(String, MioTcpListener)>,

View File

@ -55,24 +55,6 @@ pub fn new() -> ServerBuilder {
ServerBuilder::default() ServerBuilder::default()
} }
// temporary Ready type for std::future::{ready, Ready}; Can be removed when MSRV surpass 1.48
#[doc(hidden)]
pub struct Ready<T>(Option<T>);
pub(crate) fn ready<T>(t: T) -> Ready<T> {
Ready(Some(t))
}
impl<T> Unpin for Ready<T> {}
impl<T> Future for Ready<T> {
type Output = T;
fn poll(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
Poll::Ready(self.get_mut().0.take().unwrap())
}
}
// a poor man's join future. joined future is only used when starting/stopping the server. // a poor man's join future. joined future is only used when starting/stopping the server.
// pin_project and pinned futures are overkill for this task. // pin_project and pinned futures are overkill for this task.
pub(crate) struct JoinAll<T> { pub(crate) struct JoinAll<T> {
@ -132,6 +114,8 @@ impl<T> Future for JoinAll<T> {
mod test { mod test {
use super::*; use super::*;
use actix_utils::future::ready;
#[actix_rt::test] #[actix_rt::test]
async fn test_join_all() { async fn test_join_all() {
let futs = vec![ready(Ok(1)), ready(Err(3)), ready(Ok(9))]; let futs = vec![ready(Ok(1)), ready(Err(3)), ready(Ok(9))];

View File

@ -3,12 +3,15 @@ use std::net::SocketAddr;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use actix_service::{Service, ServiceFactory as BaseServiceFactory}; use actix_service::{Service, ServiceFactory as BaseServiceFactory};
use actix_utils::counter::CounterGuard; use actix_utils::{
counter::CounterGuard,
future::{ready, Ready},
};
use futures_core::future::LocalBoxFuture; use futures_core::future::LocalBoxFuture;
use log::error; use log::error;
use crate::socket::{FromStream, MioStream}; use crate::socket::{FromStream, MioStream};
use crate::{ready, Ready, Token}; use crate::Token;
pub trait ServiceFactory<Stream: FromStream>: Send + Clone + 'static { pub trait ServiceFactory<Stream: FromStream>: Send + Clone + 'static {
type Factory: BaseServiceFactory<Stream, Config = ()>; type Factory: BaseServiceFactory<Stream, Config = ()>;

View File

@ -387,27 +387,27 @@ impl Future for ServerWorker {
} }
}, },
WorkerState::Restarting(idx, token, ref mut fut) => { WorkerState::Restarting(idx, token, ref mut fut) => {
match fut.as_mut().poll(cx) { let item = ready!(fut.as_mut().poll(cx)).unwrap_or_else(|_| {
Poll::Ready(Ok(item)) => { panic!(
// only interest in the first item? "Can not restart {:?} service",
if let Some((token, service)) = item.into_iter().next() { self.factories[idx].name(token)
trace!( )
"Service {:?} has been restarted", });
self.factories[idx].name(token)
); // Only interest in the first item?
self.services[token.0].created(service); let (token, service) = item
self.state = WorkerState::Unavailable; .into_iter()
return self.poll(cx); .next()
} .expect("No BoxedServerService. Restarting can not progress");
}
Poll::Ready(Err(_)) => { trace!(
panic!( "Service {:?} has been restarted",
"Can not restart {:?} service", self.factories[idx].name(token)
self.factories[idx].name(token) );
);
} self.services[token.0].created(service);
Poll::Pending => return Poll::Pending, self.state = WorkerState::Unavailable;
}
self.poll(cx) self.poll(cx)
} }
WorkerState::Shutdown(ref mut t1, ref mut t2, ref mut tx) => { WorkerState::Shutdown(ref mut t1, ref mut t2, ref mut tx) => {