#[cfg(not(unix))]
#[path = "server/unsupported.rs"]
mod platform;
#[cfg(unix)]
#[path = "server/unix.rs"]
mod platform;
use std::path::Path;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
use micromux::{ServiceControl, SessionModelReader};
use tokio_util::sync::CancellationToken;
use crate::ControlError;
use crate::endpoint::{CanonicalConfigPath, ControlEndpoint, endpoint_hash};
pub use platform::EndpointGuard;
static LAST_SESSION_START_TOKEN: AtomicU64 = AtomicU64::new(0);
#[derive(Debug, Clone)]
pub struct SessionIdentity {
pub id: String,
pub pid: u32,
pub start_time: u64,
pub name: String,
pub working_dir: String,
pub config_path: String,
pub micromux_version: String,
}
impl SessionIdentity {
#[must_use]
pub fn new(name: String, working_dir: &Path, config_path: &CanonicalConfigPath) -> Self {
Self {
id: endpoint_hash(config_path),
pid: std::process::id(),
start_time: session_start_token(),
name,
working_dir: working_dir.to_string_lossy().into_owned(),
config_path: config_path.to_string_lossy().into_owned(),
micromux_version: env!("CARGO_PKG_VERSION").to_string(),
}
}
}
fn session_start_token() -> u64 {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |duration| {
u64::try_from(duration.as_nanos()).unwrap_or(u64::MAX)
});
let mut previous = LAST_SESSION_START_TOKEN.load(Ordering::Relaxed);
loop {
let next = now.max(previous.saturating_add(1));
match LAST_SESSION_START_TOKEN.compare_exchange_weak(
previous,
next,
Ordering::Relaxed,
Ordering::Relaxed,
) {
Ok(_) => return next,
Err(observed) => previous = observed,
}
}
}
pub fn bind(endpoint: &ControlEndpoint) -> Result<Option<EndpointGuard>, ControlError> {
platform::bind(endpoint)
}
pub fn bind_project(
endpoint: &ControlEndpoint,
config_path: &CanonicalConfigPath,
) -> Result<Option<EndpointGuard>, ControlError> {
platform::bind_project(endpoint, config_path)
}
pub fn endpoint_owner_lock_held(endpoint: &ControlEndpoint) -> Result<bool, ControlError> {
platform::endpoint_owner_lock_held(endpoint)
}
#[cfg_attr(
not(unix),
expect(
dead_code,
reason = "unsupported platforms construct the shared type but do not have a server transport that reads the fields"
)
)]
pub struct ControlServer {
reader: SessionModelReader,
control: ServiceControl,
identity: SessionIdentity,
dynamic_policy: micromux::DynamicServicesPolicy,
}
impl ControlServer {
#[must_use]
pub fn new(
reader: SessionModelReader,
control: ServiceControl,
identity: SessionIdentity,
dynamic_policy: micromux::DynamicServicesPolicy,
) -> Self {
Self {
reader,
control,
identity,
dynamic_policy,
}
}
pub async fn serve(
self: Arc<Self>,
guard: EndpointGuard,
shutdown: CancellationToken,
) -> Result<(), ControlError> {
platform::serve(self, guard, shutdown).await
}
}
#[cfg(all(test, not(unix)))]
mod unsupported_transport_tests {
use std::path::Path;
use super::{bind, endpoint_owner_lock_held};
use crate::ControlError;
use crate::endpoint::endpoint_from_hash;
#[test]
fn bind_and_owner_probe_report_unsupported_transport() {
let endpoint = endpoint_from_hash(Path::new("ignored"), "0011223344556677");
assert!(matches!(bind(&endpoint), Err(ControlError::Unsupported)));
assert!(matches!(
endpoint_owner_lock_held(&endpoint),
Err(ControlError::Unsupported)
));
}
}
#[cfg(all(test, unix))]
mod tests {
use super::*;
#[test]
fn session_start_token_is_unique_for_fast_successive_sessions() {
let first = session_start_token();
let second = session_start_token();
assert_ne!(first, second);
}
#[test]
fn session_identity_id_uses_raw_config_path_hash() {
use similar_asserts::assert_eq;
use std::ffi::OsString;
use std::os::unix::ffi::OsStringExt;
use std::path::PathBuf;
let config_path = CanonicalConfigPath::from_canonicalized(PathBuf::from(
OsString::from_vec(b"/tmp/micromux-\xff.yaml".to_vec()),
));
let identity = SessionIdentity::new("test".to_string(), Path::new("."), &config_path);
assert_eq!(identity.id, endpoint_hash(&config_path));
assert_ne!(
identity.id,
crate::endpoint::hash_path(Path::new(&identity.config_path))
);
}
}