Skip to content

Commit

Permalink
feature: extension server framework: reliability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sisungo committed Apr 19, 2024
1 parent 2ef6a41 commit c5aaf18
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions airupfx/airupfx-extensions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ publish = false
anyhow = "1"
airup-sdk = { path = "../../airup-sdk" }
airupfx-fs = { path = "../airupfx-fs" }
airupfx-signal = { path = "../airupfx-signal" }
ciborium = "0.2"
tokio = { workspace = true }
31 changes: 26 additions & 5 deletions airupfx/airupfx-extensions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use airup_sdk::{
info::ConnectionExt,
ipc::MessageProto,
nonblocking::ipc::{MessageProtoRecvExt, MessageProtoSendExt},
system::ConnectionExt as _,
system::{ConnectionExt as _, Event},
};
use airupfx_signal::SIGTERM;
use ciborium::cbor;
use std::{
collections::{HashMap, HashSet},
Expand All @@ -22,6 +23,7 @@ pub struct Server {
listener: UnixListener,
path: String,
extension_name: String,
service_name: String,
rpc_methods: HashMap<String, Method>,
}
impl Server {
Expand All @@ -39,11 +41,12 @@ impl Server {
.to_string();
std::fs::remove_file(&extension_socket_path).ok();

Ok(Self::with_config(extension_name, extension_socket_path).await?)
Ok(Self::with_config(extension_name, service_name, extension_socket_path).await?)
}

pub async fn with_config<P: AsRef<Path>>(
extension_name: impl Into<String>,
service_name: impl Into<String>,
path: P,
) -> std::io::Result<Self> {
let listener = UnixListener::bind(path.as_ref())?;
Expand All @@ -53,6 +56,7 @@ impl Server {
listener,
path: path.as_ref().display().to_string(),
extension_name: extension_name.into(),
service_name: service_name.into(),
rpc_methods: HashMap::with_capacity(16),
})
}
Expand All @@ -64,15 +68,15 @@ impl Server {

pub async fn run(self) -> ! {
let rpc_methods = Arc::new(self.rpc_methods);
let extension_name = self.extension_name;
let path = self.path;
let method_set: HashSet<_> = rpc_methods.keys().cloned().collect();

let extension_name = self.extension_name.clone();
tokio::spawn(async move {
let mut airup_rpc_conn =
airup_sdk::nonblocking::Connection::connect(airup_sdk::socket_path()).await?;

match airup_rpc_conn
.load_extension(&extension_name, &path, method_set)
.load_extension(&extension_name, &self.path, method_set)
.await
{
Ok(Ok(())) => (),
Expand All @@ -86,9 +90,26 @@ impl Server {
}
}

airup_rpc_conn
.trigger_event(&Event::new("notify_active".into(), self.service_name))
.await
.ok();

Ok::<(), anyhow::Error>(())
});

let extension_name = self.extension_name.clone();
airupfx_signal::signal(SIGTERM, |_| async move {
let Ok(mut airup_rpc_conn) =
airup_sdk::nonblocking::Connection::connect(airup_sdk::socket_path()).await
else {
return;
};

airup_rpc_conn.unload_extension(&extension_name).await.ok();
})
.ok();

loop {
let Ok((stream, _)) = self.listener.accept().await else {
continue;
Expand Down
6 changes: 3 additions & 3 deletions airupfx/airupfx-signal/src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ pub const SIGFPE: i32 = libc::SIGFPE;
/// # Errors
/// An `Err(_)` is returned if the underlying OS function failed.
pub fn signal<
F: FnMut(i32) -> T + Send + Sync + 'static,
F: FnOnce(i32) -> T + Clone + Send + Sync + 'static,
T: Future<Output = ()> + Send + 'static,
>(
signum: i32,
mut op: F,
op: F,
) -> std::io::Result<()> {
let mut signal = tokio::signal::unix::signal(SignalKind::from_raw(signum))?;
tokio::spawn(async move {
loop {
signal.recv().await;
op(signum).await;
op.clone()(signum).await;
}
});

Expand Down
1 change: 1 addition & 0 deletions docs/resources/airup-fallback-logger.airs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[service]
display-name = "Airup Fallback Logger"
description = "An Airup extension that provides a simple logger interface for fallback use."
kind = "notify"

[exec]
start = "/usr/libexec/airup/fallback-logger"
Expand Down

0 comments on commit c5aaf18

Please sign in to comment.