From deea6de0641d0438dec3b00d4f5948d41c9f3ef3 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 18 Jul 2023 03:58:57 -0400 Subject: stop trying to reconnect to notifications so aggressively it adds a bunch of latency to every command otherwise --- src/bin/rbw-agent/actions.rs | 18 ++++++++++-------- src/bin/rbw-agent/agent.rs | 31 +++++++++++++------------------ 2 files changed, 23 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/bin/rbw-agent/actions.rs b/src/bin/rbw-agent/actions.rs index 6291680..b7295f8 100644 --- a/src/bin/rbw-agent/actions.rs +++ b/src/bin/rbw-agent/actions.rs @@ -205,11 +205,10 @@ pub async fn login( } } } - } - let err = subscribe_to_notifications(state.clone()).await.err(); - if let Some(e) = err { - eprintln!("failed to subscribe to notifications: {e}"); + if let Err(e) = subscribe_to_notifications(state.clone()).await { + eprintln!("failed to subscribe to notifications: {e}"); + } } respond_ack(sock).await?; @@ -685,6 +684,10 @@ async fn config_pinentry() -> anyhow::Result { pub async fn subscribe_to_notifications( state: std::sync::Arc>, ) -> anyhow::Result<()> { + if state.lock().await.notifications_handler.is_connected() { + return Ok(()); + } + // access token might be out of date, so we do a sync to refresh it sync(None).await?; @@ -705,11 +708,10 @@ pub async fn subscribe_to_notifications( .replace("https://", "wss://"); let mut state = state.lock().await; - let err = state + state .notifications_handler .connect(websocket_url) .await - .err(); - - err.map_or_else(|| Ok(()), |err| Err(anyhow::anyhow!(err.to_string()))) + .err() + .map_or_else(|| Ok(()), |err| Err(anyhow::anyhow!(err.to_string()))) } diff --git a/src/bin/rbw-agent/agent.rs b/src/bin/rbw-agent/agent.rs index fe46e3b..131bab2 100644 --- a/src/bin/rbw-agent/agent.rs +++ b/src/bin/rbw-agent/agent.rs @@ -156,26 +156,21 @@ impl Agent { self.state.lock().await.clear(); } Event::Sync(()) => { - // this could fail if we aren't logged in, but we don't - // care about that let state = self.state.clone(); tokio::spawn(async move { - let result = crate::actions::sync(None).await; - if let Err(e) = result { - eprintln!("failed to sync: {e:#}"); - } else if !state - .lock() - .await - .notifications_handler - .is_connected() - { - let err = - crate::actions::subscribe_to_notifications( - state, - ) - .await; - if let Err(e) = err { - eprintln!("failed to subscribe to notifications: {e:#}"); + // this could fail if we aren't logged in, but we + // don't care about that + match crate::actions::sync(None).await { + Ok(()) => { + if let Err(e) = crate::actions::subscribe_to_notifications( + state, + ) + .await { + eprintln!("failed to subscribe to notifications: {e:#}"); + } + } + Err(e) => { + eprintln!("failed to sync: {e:#}"); } } }); -- cgit v1.2.3-54-g00ecf