From 85ca2bc01c2b465b8ff4229f48fc918046258715 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Mon, 22 Apr 2024 17:26:21 -0700 Subject: [PATCH] Refactor internal ignite sequencing. --- core/lib/src/local/asynchronous/client.rs | 2 +- core/lib/src/rocket.rs | 80 ++++++++++------------- 2 files changed, 36 insertions(+), 46 deletions(-) diff --git a/core/lib/src/local/asynchronous/client.rs b/core/lib/src/local/asynchronous/client.rs index 68d502e4..23690e8b 100644 --- a/core/lib/src/local/asynchronous/client.rs +++ b/core/lib/src/local/asynchronous/client.rs @@ -113,7 +113,7 @@ impl Client { let rocket = self.rocket; rocket.shutdown().notify(); rocket.fairings.handle_shutdown(&rocket).await; - rocket.into_ignite() + rocket.deorbit() } // Generates the public API methods, which call the private methods above. diff --git a/core/lib/src/rocket.rs b/core/lib/src/rocket.rs index fa192815..10a541f1 100644 --- a/core/lib/src/rocket.rs +++ b/core/lib/src/rocket.rs @@ -3,11 +3,12 @@ use std::ops::{Deref, DerefMut}; use std::sync::Arc; use std::time::Duration; use std::any::Any; +use std::future::Future; -use futures::{Future, TryFutureExt}; use yansi::Paint; use either::Either; use figment::{Figment, Provider}; +use futures::TryFutureExt; use crate::shutdown::{Stages, Shutdown}; use crate::{sentinel, shield::Shield, Catcher, Config, Route}; @@ -682,31 +683,7 @@ impl Rocket { rocket } - async fn _launch_with(self) -> Result, Error> { - let bind_endpoint = B::bind_endpoint(&&self).ok(); - let listener: B = B::bind(&self).await - .map_err(|e| ErrorKind::Bind(bind_endpoint, Box::new(e)))?; - - let any: Box = Box::new(listener); - match any.downcast::() { - Ok(listener) => { - let listener = *listener; - crate::util::for_both!(listener, listener => { - crate::util::for_both!(listener, listener => { - self._launch_on(listener).await - }) - }) - } - Err(any) => { - let listener = *any.downcast::().unwrap(); - self._launch_on(listener).await - } - } - } - - async fn _launch_on(self, listener: L) -> Result, Error> - where L: Listener + 'static, - { + async fn _launch(self, listener: L) -> Result, Error> { let rocket = self.listen_and_serve(listener, |rocket| async move { let rocket = Arc::new(rocket); @@ -761,7 +738,7 @@ impl Rocket { match Arc::try_unwrap(self) { Ok(rocket) => { info!("Graceful shutdown completed successfully."); - Ok(rocket.into_ignite()) + Ok(rocket.deorbit()) } Err(rocket) => { warn!("Shutdown failed: outstanding background I/O."); @@ -770,7 +747,7 @@ impl Rocket { } } - pub(crate) fn into_ignite(self) -> Rocket { + pub(crate) fn deorbit(self) -> Rocket { Rocket(Igniting { router: self.0.router, fairings: self.0.fairings, @@ -955,14 +932,16 @@ impl Rocket

{ } } - pub(crate) async fn local_launch(self, e: Endpoint) -> Result, Error> { - let rocket = match self.0.into_state() { - State::Build(s) => Rocket::from(s).ignite().await?._local_launch(e).await, - State::Ignite(s) => Rocket::from(s)._local_launch(e).await, - State::Orbit(s) => Rocket::from(s) - }; + async fn into_ignite(self) -> Result, Error> { + match self.0.into_state() { + State::Build(s) => Rocket::from(s).ignite().await, + State::Ignite(s) => Ok(Rocket::from(s)), + State::Orbit(s) => Ok(Rocket::from(s).deorbit()), + } + } - Ok(rocket) + pub(crate) async fn local_launch(self, e: Endpoint) -> Result, Error> { + Ok(self.into_ignite().await?._local_launch(e).await) } /// Returns a `Future` that transitions this instance of `Rocket` from any @@ -1014,10 +993,25 @@ impl Rocket

{ } pub async fn launch_with(self) -> Result, Error> { - match self.0.into_state() { - State::Build(s) => Rocket::from(s).ignite().await?._launch_with::().await, - State::Ignite(s) => Rocket::from(s)._launch_with::().await, - State::Orbit(s) => Ok(Rocket::from(s).into_ignite()) + let rocket = self.into_ignite().await?; + let bind_endpoint = B::bind_endpoint(&rocket).ok(); + let listener: B = B::bind(&rocket).await + .map_err(|e| ErrorKind::Bind(bind_endpoint, Box::new(e)))?; + + let any: Box = Box::new(listener); + match any.downcast::() { + Ok(listener) => { + let listener = *listener; + crate::util::for_both!(listener, listener => { + crate::util::for_both!(listener, listener => { + rocket._launch(listener).await + }) + }) + } + Err(any) => { + let listener = *any.downcast::().unwrap(); + rocket._launch(listener).await + } } } @@ -1027,17 +1021,13 @@ impl Rocket

{ E: std::error::Error + Send + 'static { let listener = listener.map_err(|e| ErrorKind::Bind(None, Box::new(e))).await?; - self.launch_on(listener).await + self.into_ignite().await?._launch(listener).await } pub async fn launch_on(self, listener: L) -> Result, Error> where L: Listener + 'static, { - match self.0.into_state() { - State::Build(s) => Rocket::from(s).ignite().await?._launch_on(listener).await, - State::Ignite(s) => Rocket::from(s)._launch_on(listener).await, - State::Orbit(s) => Ok(Rocket::from(s).into_ignite()) - } + self.into_ignite().await?._launch(listener).await } }