From 41128094d1b274bfc8798ece66b93577e2155930 Mon Sep 17 00:00:00 2001 From: Przemyslaw Hugh Kaznowski Date: Tue, 9 Jul 2024 10:18:59 +0100 Subject: [PATCH] 3987 Reduce read lock duration (#4328) --- src/rpc/mod.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/rpc/mod.rs b/src/rpc/mod.rs index abb48522..a1eba119 100644 --- a/src/rpc/mod.rs +++ b/src/rpc/mod.rs @@ -54,9 +54,18 @@ pub(crate) async fn notifications(state: Arc, canceller: CancellationT // Receive a notification on the channel Ok(notification) = channel.recv() => { // Find which WebSocket the notification belongs to - if let Some(id) = state.live_queries.read().await.get(¬ification.id) { + let found_ws = { + // We remove the lock asap + state.live_queries.read().await.get(¬ification.id).cloned() + }; + if let Some(id) = found_ws { // Check to see if the WebSocket exists - if let Some(rpc) = state.web_sockets.read().await.get(id) { + let maybe_ws = { + // We remove the lock ASAP + // WS is an Arc anyway + state.web_sockets.read().await.get(&id).cloned() + }; + if let Some(rpc) = maybe_ws { // Serialize the message to send let message = success(None, notification); // Add metrics