From 9e313efc53ca8fc118d9e17fcd538528b7e7497c Mon Sep 17 00:00:00 2001 From: ihciah Date: Mon, 8 Jan 2024 07:38:05 +0000 Subject: [PATCH] feat: two stage service management --- monolake-core/src/server/mod.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/monolake-core/src/server/mod.rs b/monolake-core/src/server/mod.rs index f1ae38c..c870a22 100644 --- a/monolake-core/src/server/mod.rs +++ b/monolake-core/src/server/mod.rs @@ -157,6 +157,7 @@ impl Default for WorkerController { pub struct SiteHandler { handler_slot: HandlerSlot, + two_stage_handler_slot: UnsafeCell>, _stop: OReceiver<()>, } @@ -166,6 +167,7 @@ impl SiteHandler { ( Self { handler_slot, + two_stage_handler_slot: UnsafeCell::new(None), _stop: rx, }, tx, @@ -210,6 +212,8 @@ pub enum Command { Add(Arc, F, LF), Update(Arc, F), Remove(Arc), + TwoStageCreate(Arc, F), + TwoStageApply(Arc), } pub struct Update { @@ -254,6 +258,30 @@ where None => bail_into!("site {name} not exist"), } } + Command::TwoStageCreate(name, factory) => { + let sites = unsafe { &mut *controller.sites.get() }; + let Some(sh) = sites.get(&name) else { + bail_into!("site {name} not exist"); + }; + + let current_svc = sh.handler_slot.clone(); + let svc = factory + .make_via_ref(Some(¤t_svc.get_svc())) + .map_err(|e| anyhow!("build service fail {e:?}"))?; + unsafe { *sh.two_stage_handler_slot.get() = Some(svc) }; + Ok(()) + } + Command::TwoStageApply(name) => { + let sites = unsafe { &mut *controller.sites.get() }; + let Some(sh) = sites.get(&name) else { + bail_into!("site {name} not exist"); + }; + + let svc = unsafe { (*sh.two_stage_handler_slot.get()).take() } + .ok_or_else(|| anyhow!("two stage service not exist"))?; + sh.handler_slot.update_svc(Rc::new(svc)); + Ok(()) + } Command::Add(name, factory, listener_factory) => { // TODO: make sure the named service has not been started let listener = match listener_factory.make() {