Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: two stage service management #54

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions monolake-core/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ impl<S> Default for WorkerController<S> {

pub struct SiteHandler<S> {
handler_slot: HandlerSlot<S>,
two_stage_handler_slot: UnsafeCell<Option<S>>,
_stop: OReceiver<()>,
}

Expand All @@ -166,6 +167,7 @@ impl<S> SiteHandler<S> {
(
Self {
handler_slot,
two_stage_handler_slot: UnsafeCell::new(None),
_stop: rx,
},
tx,
Expand Down Expand Up @@ -210,6 +212,9 @@ pub enum Command<F, LF> {
Add(Arc<String>, F, LF),
Update(Arc<String>, F),
Remove(Arc<String>),
TwoStageCreate(Arc<String>, F),
TwoStageApply(Arc<String>),
TwoStageAbort(Arc<String>),
}

pub struct Update<F, LF> {
Expand Down Expand Up @@ -254,6 +259,38 @@ 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(&current_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::TwoStageAbort(name) => {
let sites = unsafe { &mut *controller.sites.get() };
let Some(sh) = sites.get(&name) else {
bail_into!("site {name} not exist");
};
unsafe { (*sh.two_stage_handler_slot.get()) = None };
Ok(())
}
Command::Add(name, factory, listener_factory) => {
// TODO: make sure the named service has not been started
let listener = match listener_factory.make() {
Expand Down
Loading