refactor: apply 5-pass audit optimizations across mcp-memory codebase

This commit is contained in:
Riz Ashraf committed 2026-10-06 06:05:38 +01:00
1 parent 924b6d09fa
commit 5bd8b1587a
43 files changed
+1866 -1658

No files matched your search

+30 -16
View File
@@ -122,10 +122,8 @@ pub async fn ttl_sweeper_worker(state: Arc<MemoryState>) {
pub async fn index_committer_worker(state: Arc<MemoryState>) {
loop {
state.index_commit_notify.notified().await;
let idx_opt = state.search_index.read().ok().map(|idx| idx.clone());
if let Some(idx) = idx_opt {
let _ = idx.commit().await;
}
let idx = state.search_index.read().await.clone();
let _ = idx.commit().await;
}
}
@@ -234,15 +232,26 @@ pub async fn run_server(state: Arc<MemoryState>) -> Result<(), Box<dyn std::erro
loop {
match rx.recv().await {
Ok(msg) => {
let senders: Vec<_> = app_state_clone
.clients
.read()
.unwrap_or_else(|e| e.into_inner())
.values()
.cloned()
.collect();
for client_tx in senders {
let _ = client_tx.try_send(msg.clone());
let mut closed_ids = Vec::new();
{
let clients_guard = app_state_clone
.clients
.read()
.unwrap_or_else(|e| e.into_inner());
for (id, tx) in clients_guard.iter() {
if tx.try_send(msg.clone()).is_err() && tx.is_closed() {
closed_ids.push(id.clone());
}
}
}
if !closed_ids.is_empty() {
let mut write_guard = app_state_clone
.clients
.write()
.unwrap_or_else(|e| e.into_inner());
for id in closed_ids {
write_guard.remove(&id);
}
}
}
Err(tokio::sync::broadcast::error::RecvError::Closed) => break,
@@ -304,9 +313,14 @@ pub async fn run_server(state: Arc<MemoryState>) -> Result<(), Box<dyn std::erro
let session = &payload.session_id;
let _is_unix_socket = session.starts_with('/') || session.starts_with('~');
if let Some(home) = dirs::home_dir() {
let nvim_txt_path = home.join(".gemini").join("active_nvim.txt");
let _ = tokio::fs::create_dir_all(nvim_txt_path.parent().unwrap()).await;
let _ = tokio::fs::write(&nvim_txt_path, session).await;
let nvim_dir = home.join(".gemini");
let nvim_txt_path = nvim_dir.join("active_nvim.txt");
let tmp_path = nvim_dir.join(format!("active_nvim_{}.tmp", std::process::id()));
if tokio::fs::create_dir_all(&nvim_dir).await.is_ok()
&& tokio::fs::write(&tmp_path, session).await.is_ok()
{
let _ = tokio::fs::rename(&tmp_path, &nvim_txt_path).await;
}
}
}