claude
This commit is contained in:
parent
39c5591d36
commit
35877b69da
94 changed files with 3157 additions and 1859 deletions
|
|
@ -3,10 +3,10 @@ use std::{
|
|||
sync::{Arc, Mutex as StdMutex},
|
||||
};
|
||||
|
||||
use log::{debug, warn};
|
||||
use log::{debug, info, warn};
|
||||
use tokio::sync::{Mutex, broadcast};
|
||||
|
||||
use super::models::WebSocketServerMessageWithOrigin;
|
||||
use super::models::{WebSocketServerMessage, WebSocketServerMessageWithOrigin};
|
||||
use crate::{app_state::database::models::VaultId, config::server_config::ServerConfig};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
|
@ -46,8 +46,16 @@ impl Broadcasts {
|
|||
}
|
||||
|
||||
/// Remove senders for vaults with no active receivers
|
||||
fn prune_inactive_vaults(tx_map: &mut TxMap) {
|
||||
tx_map.retain(|_, sender| sender.receiver_count() > 0);
|
||||
fn prune_inactive_vaults(tx_map: &mut TxMap) -> Vec<VaultId> {
|
||||
let mut pruned = Vec::new();
|
||||
tx_map.retain(|vault, sender| {
|
||||
let alive = sender.receiver_count() > 0;
|
||||
if !alive {
|
||||
pruned.push(vault.clone());
|
||||
}
|
||||
alive
|
||||
});
|
||||
pruned
|
||||
}
|
||||
|
||||
pub fn get_receiver(
|
||||
|
|
@ -60,10 +68,15 @@ impl Broadcasts {
|
|||
.tx
|
||||
.lock()
|
||||
.expect("broadcasts.tx mutex poisoned — a previous holder panicked");
|
||||
Self::prune_inactive_vaults(&mut tx_map);
|
||||
|
||||
let count_before_prune = tx_map
|
||||
.get(&vault)
|
||||
.map_or(0, tokio::sync::broadcast::Sender::receiver_count);
|
||||
let pruned = Self::prune_inactive_vaults(&mut tx_map);
|
||||
let pruned_self = pruned.contains(&vault);
|
||||
|
||||
let sender = tx_map
|
||||
.entry(vault)
|
||||
.entry(vault.clone())
|
||||
.or_insert_with(|| broadcast::channel(self.broadcast_channel_capacity).0);
|
||||
|
||||
// Hold the lock across the count check *and* the subscribe so the
|
||||
|
|
@ -75,7 +88,13 @@ impl Broadcasts {
|
|||
)));
|
||||
}
|
||||
|
||||
Ok(sender.subscribe())
|
||||
let receiver = sender.subscribe();
|
||||
let count_after = sender.receiver_count();
|
||||
info!(
|
||||
"[BCAST] get_receiver vault={vault} count_before_prune={count_before_prune} pruned_self={pruned_self} pruned_total={} count_after_subscribe={count_after}",
|
||||
pruned.len()
|
||||
);
|
||||
Ok(receiver)
|
||||
}
|
||||
|
||||
/// Notify all clients (who are subscribed to the vault) about an update.
|
||||
|
|
@ -83,23 +102,46 @@ impl Broadcasts {
|
|||
/// function return without worrying about task cancellation dropping
|
||||
/// the broadcast mid-flight. Failures are logged, never propagated.
|
||||
pub fn send_document_update(&self, vault: VaultId, document: WebSocketServerMessageWithOrigin) {
|
||||
let vault_update_id = match &document.message {
|
||||
WebSocketServerMessage::VaultUpdate(u) => Some(u.document.vault_update_id),
|
||||
WebSocketServerMessage::CursorPositions(_) => None,
|
||||
};
|
||||
let is_deleted = match &document.message {
|
||||
WebSocketServerMessage::VaultUpdate(u) => Some(u.document.is_deleted),
|
||||
WebSocketServerMessage::CursorPositions(_) => None,
|
||||
};
|
||||
let mut tx_map = self
|
||||
.tx
|
||||
.lock()
|
||||
.expect("broadcasts.tx mutex poisoned — a previous holder panicked");
|
||||
Self::prune_inactive_vaults(&mut tx_map);
|
||||
let count_before_prune = tx_map
|
||||
.get(&vault)
|
||||
.map_or(0, tokio::sync::broadcast::Sender::receiver_count);
|
||||
let pruned = Self::prune_inactive_vaults(&mut tx_map);
|
||||
let pruned_self = pruned.contains(&vault);
|
||||
|
||||
let sender = tx_map
|
||||
.entry(vault.clone())
|
||||
.or_insert_with(|| broadcast::channel(self.broadcast_channel_capacity).0);
|
||||
|
||||
if sender.receiver_count() == 0 {
|
||||
let count_before_send = sender.receiver_count();
|
||||
|
||||
if count_before_send == 0 {
|
||||
info!(
|
||||
"[BCAST] send_document_update vault={vault} vuid={vault_update_id:?} is_deleted={is_deleted:?} count_before_prune={count_before_prune} pruned_self={pruned_self} count_before_send=0 SKIPPED"
|
||||
);
|
||||
debug!("Skipping broadcast, no clients connected for vault `{vault}`");
|
||||
return;
|
||||
}
|
||||
|
||||
if let Err(e) = sender.send(document) {
|
||||
warn!("Failed to broadcast to vault `{vault}`: {e}");
|
||||
let send_result = sender.send(document);
|
||||
match &send_result {
|
||||
Ok(n) => info!(
|
||||
"[BCAST] send_document_update vault={vault} vuid={vault_update_id:?} is_deleted={is_deleted:?} count_before_prune={count_before_prune} pruned_self={pruned_self} count_before_send={count_before_send} SENT delivered_to={n}"
|
||||
),
|
||||
Err(e) => warn!(
|
||||
"[BCAST] send_document_update vault={vault} vuid={vault_update_id:?} is_deleted={is_deleted:?} count_before_prune={count_before_prune} pruned_self={pruned_self} count_before_send={count_before_send} FAILED err={e}"
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,21 +44,29 @@ pub fn get_authenticated_handshake(
|
|||
}
|
||||
}
|
||||
|
||||
/// Stream the documents the client missed while offline, bounded above
|
||||
/// by `up_to_vault_update_id` so the catch-up is a stable snapshot at
|
||||
/// exactly that cursor. The WebSocket handshake atomically subscribes
|
||||
/// to the broadcast channel and snapshots this cursor under the per-
|
||||
/// vault send lock; commits past the cursor are then delivered solely
|
||||
/// through the broadcast channel (filtered by the same cursor on the
|
||||
/// receive side), so every committed update is delivered exactly once.
|
||||
pub async fn get_unseen_documents(
|
||||
state: &AppState,
|
||||
vault_id: &VaultId,
|
||||
last_seen_vault_update_id: Option<VaultUpdateId>,
|
||||
up_to_vault_update_id: VaultUpdateId,
|
||||
) -> Result<Vec<DocumentVersionWithoutContent>, SyncServerError> {
|
||||
if let Some(update_id) = last_seen_vault_update_id {
|
||||
state
|
||||
.database
|
||||
.get_latest_documents_since(vault_id, update_id, None)
|
||||
.get_latest_documents_since(vault_id, update_id, Some(up_to_vault_update_id), None)
|
||||
.await
|
||||
.map_err(server_error)
|
||||
} else {
|
||||
state
|
||||
.database
|
||||
.get_latest_documents(vault_id, None)
|
||||
.get_latest_documents(vault_id, Some(up_to_vault_update_id), None)
|
||||
.await
|
||||
.map_err(server_error)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue