liminal_server/server/connection/notifier.rs
1//! Connection-keyed notifier hook for worker registration lifecycle.
2//!
3//! This is the application seam for self-describing worker registration. When a
4//! worker sends a [`Frame::WorkerRegister`](liminal::protocol::Frame) over its
5//! established connection, the server associates the registration with the
6//! connection's beamr process id and invokes the configured
7//! [`ConnectionNotifier`]; on connection close it invokes the matching
8//! deregistration. The notifier is connection-keyed (by pid), which is distinct
9//! from the subject-keyed responder registry in [`super::services`].
10//!
11//! Keeping the hook a `liminal-server` trait — rather than a liminal-core
12//! concern — preserves liminal's generality: liminal still runs standalone with
13//! no notifier configured, and the application (aion, in Stage 2) plugs its
14//! registry in without liminal depending on it.
15
16use liminal::protocol::WorkerRegistration;
17
18use crate::ServerError;
19
20/// Application hook invoked when a worker registers or unregisters on a
21/// connection.
22///
23/// Implementations associate the connection's beamr process id (`pid`) with the
24/// worker's declared [`WorkerRegistration`] so the application can route work to
25/// it, and release that association on disconnect. The hook is synchronous: a
26/// registration is acknowledged to the worker only after
27/// [`on_worker_registered`](Self::on_worker_registered) returns, so a rejecting
28/// application surfaces a `Rejected` ack instead of leaving the worker silently
29/// connected but never dispatched-to.
30pub trait ConnectionNotifier: std::fmt::Debug + Send + Sync {
31 /// Called when a worker registers on the connection identified by `pid`.
32 ///
33 /// Returning `Ok(())` accepts the registration (the worker receives an
34 /// `Accepted` ack). Returning [`ServerError`] rejects it (the worker receives
35 /// a `Rejected` ack carrying the error text), so a failed association never
36 /// leaves the worker believing it is registered.
37 ///
38 /// # Errors
39 /// Returns [`ServerError`] when the application declines the registration.
40 fn on_worker_registered(
41 &self,
42 pid: u64,
43 registration: &WorkerRegistration,
44 ) -> Result<(), ServerError>;
45
46 /// Called when the connection identified by `pid` — which had a stored
47 /// registration — closes, so the application can release the association.
48 ///
49 /// Deregistration is best-effort and infallible from the connection's
50 /// perspective: it runs on the close path where there is no peer to report an
51 /// error to.
52 fn on_worker_unregistered(&self, pid: u64);
53}