huskies: merge 1163 story Replace perm_rx lock-as-presence-signal with a permission router

This commit is contained in:
Huskies Agent
2026-07-16 13:19:20 +00:00
parent 6f8a8ffd87
commit 18b065f77a
26 changed files with 823 additions and 293 deletions
+5 -13
View File
@@ -507,11 +507,7 @@ pub fn spawn_gateway_bot(
gateway_projects_store: std::sync::Arc<tokio::sync::RwLock<BTreeMap<String, ProjectEntry>>>,
port: u16,
gateway_event_tx: Option<tokio::sync::broadcast::Sender<super::GatewayStatusEvent>>,
perm_rx: std::sync::Arc<
tokio::sync::Mutex<
tokio::sync::mpsc::UnboundedReceiver<crate::http::context::PermissionForward>,
>,
>,
permission_registry: std::sync::Arc<crate::service::permission_router::ResponderRegistry>,
) -> (
Option<tokio::task::AbortHandle>,
tokio::sync::watch::Sender<Option<crate::rebuild::ShutdownReason>>,
@@ -550,10 +546,8 @@ pub fn spawn_gateway_bot(
.map(|c| c.ambient_rooms.iter().cloned().collect())
.unwrap_or_default(),
)),
perm_rx,
pending_perm_replies: std::sync::Arc::new(tokio::sync::Mutex::new(
std::collections::HashMap::new(),
)),
permission_registry,
pending_perm_replies: crate::service::permission_router::PendingPermReplies::new(),
permission_timeout_secs: bot_cfg
.as_ref()
.map(|c| c.permission_timeout_secs)
@@ -599,9 +593,7 @@ mod tests {
let active = std::sync::Arc::new(tokio::sync::RwLock::new("proj".to_string()));
let (event_tx, _) = tokio::sync::broadcast::channel(4);
let (_perm_tx, perm_rx) =
tokio::sync::mpsc::unbounded_channel::<crate::http::context::PermissionForward>();
let perm_rx = std::sync::Arc::new(tokio::sync::Mutex::new(perm_rx));
let permission_registry = crate::service::permission_router::ResponderRegistry::new();
let projects_store =
std::sync::Arc::new(tokio::sync::RwLock::new(std::collections::BTreeMap::new()));
let (handle, shutdown_tx) = spawn_gateway_bot(
@@ -610,7 +602,7 @@ mod tests {
projects_store,
3001,
Some(event_tx),
perm_rx,
permission_registry,
);
// No bot.toml in tmp → no abort handle spawned.
+19 -6
View File
@@ -224,11 +224,14 @@ pub struct GatewayState {
/// received from connected sleds into the gateway's Matrix bot permission
/// pipeline.
pub perm_tx: mpsc::UnboundedSender<PermissionForward>,
/// Receiver end of the gateway's permission channel (shared with the Matrix bot).
/// Registry of tasks registered to receive forwarded permission requests
/// (shared with the gateway's embedded Matrix bot).
///
/// The Matrix bot's `permission_listener` holds this locked for its lifetime;
/// the sled-uplink WS handler sends requests via `perm_tx`.
pub perm_rx: Arc<TokioMutex<mpsc::UnboundedReceiver<PermissionForward>>>,
/// The Matrix bot's `permission_listener` registers with this for its
/// lifetime; the sled-uplink WS handler sends requests via `perm_tx`,
/// which the router task (spawned in [`GatewayState::new`]) dispatches
/// into this registry.
pub permission_registry: Arc<crate::service::permission_router::ResponderRegistry>,
/// Reversed sled-token map: token → project_name (sled_id).
///
/// Built at startup from both [`GatewayConfig::sled_tokens`] AND the
@@ -261,6 +264,16 @@ impl GatewayState {
.unwrap_or(first_from_config);
let (event_tx, _) = tokio::sync::broadcast::channel(EVENT_CHANNEL_CAPACITY);
let (perm_tx, perm_rx) = mpsc::unbounded_channel::<PermissionForward>();
let permission_registry = crate::service::permission_router::ResponderRegistry::new();
// `GatewayState::new` is also called from plain `#[test]` fns with no
// tokio runtime; skip spawning when there's no reactor to spawn onto
// since those tests never exercise the permission plumbing.
if tokio::runtime::Handle::try_current().is_ok() {
crate::service::permission_router::spawn_permission_router(
perm_rx,
Arc::clone(&permission_registry),
);
}
// Build token→project_name map from two sources:
// 1. Legacy top-level [sled_tokens] section (sled_id → token, reversed)
@@ -287,7 +300,7 @@ impl GatewayState {
bot_shutdown_tx: Arc::new(TokioMutex::new(None)),
event_tx,
perm_tx,
perm_rx: Arc::new(TokioMutex::new(perm_rx)),
permission_registry,
sled_tokens,
sled_connections: Arc::new(RwLock::new(HashMap::new())),
})
@@ -683,7 +696,7 @@ pub async fn save_bot_config_and_restart(state: &GatewayState, content: &str) ->
Arc::clone(&state.projects),
state.port,
Some(state.event_tx.clone()),
Arc::clone(&state.perm_rx),
Arc::clone(&state.permission_registry),
);
*handle = new_handle;
*state.bot_shutdown_tx.lock().await = Some(new_shutdown_tx);