From 257ba7c31edee3dcdd16b236d49b7f0910e400ca Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Mon, 6 Feb 2023 17:14:35 +1300 Subject: [PATCH] Replace GLOBALS.relays_watching with GLOBALS.relays (connected field) --- src/globals.rs | 26 +++++++++---------- src/overlord/mod.rs | 59 ++++++++++++++++++++++---------------------- src/ui/relays/all.rs | 4 +-- src/ui/relays/mod.rs | 9 ++++++- 4 files changed, 51 insertions(+), 47 deletions(-) diff --git a/src/globals.rs b/src/globals.rs index 2a567797..466eea91 100644 --- a/src/globals.rs +++ b/src/globals.rs @@ -1,5 +1,5 @@ -use crate::db::DbRelay; use crate::comms::{ToMinionMessage, ToOverlordMessage}; +use crate::db::DbRelay; use crate::events::Events; use crate::feed::Feed; use crate::fetcher::Fetcher; @@ -51,11 +51,8 @@ pub struct Globals { /// All nostr relay records we have, with associated info pub relays: DashMap, - /// The relays we are currently connected to - pub relays_watching: RwLock>, - /// These are the relays we are currently connected to for general feed, along with - /// the public keys they serve. Yes this overlaps with relays_watching, but each + /// the public keys they serve. Yes this overlaps with relays, but each /// has data the other doesn't. pub relay_assignments: RwLock>, @@ -111,7 +108,6 @@ lazy_static! { relationships: RwLock::new(HashMap::new()), people: People::new(), relays: DashMap::new(), - relays_watching: RwLock::new(Vec::new()), relay_assignments: RwLock::new(Vec::new()), relay_picker: RwLock::new(Default::default()), shutting_down: AtomicBool::new(false), @@ -225,10 +221,10 @@ impl Globals { } pub fn relays_filtered(&self, mut f: F) -> Vec - where F: FnMut(&DbRelay) -> bool + where + F: FnMut(&DbRelay) -> bool, { - self - .relays + self.relays .iter() .filter_map(|r| { if f(&r.value().dbrelay) { @@ -238,14 +234,13 @@ impl Globals { } }) .collect() - } pub fn relays_url_filtered(&self, mut f: F) -> Vec - where F: FnMut(&DbRelay) -> bool + where + F: FnMut(&DbRelay) -> bool, { - self - .relays + self.relays .iter() .filter_map(|r| { if f(&r.value().dbrelay) { @@ -255,6 +250,11 @@ impl Globals { } }) .collect() + } + pub fn relay_is_connected(&self, url: &RelayUrl) -> bool { + self.relays + .iter() + .any(|ri| ri.value().dbrelay.url == *url && ri.value().connected) } } diff --git a/src/overlord/mod.rs b/src/overlord/mod.rs index 5e81f072..a2289847 100644 --- a/src/overlord/mod.rs +++ b/src/overlord/mod.rs @@ -101,7 +101,7 @@ impl Overlord { GLOBALS.relays.insert( dbrelay.url.clone(), RelayInfo { - dbrelay: dbrelay, + dbrelay, connected: false, assignments: vec![], subscriptions: vec![], @@ -229,7 +229,7 @@ impl Overlord { let read_relay_urls: Vec = GLOBALS.relays_url_filtered(|r| r.read); for relay_url in read_relay_urls.iter() { // Start a minion for this relay if there is none - if !GLOBALS.relays_watching.read().await.contains(relay_url) { + if !GLOBALS.relay_is_connected(relay_url) { self.start_minion(relay_url.clone()).await?; } @@ -329,7 +329,11 @@ impl Overlord { let abort_handle = self.minions.spawn(async move { minion.handle().await }); let id = abort_handle.id(); self.minions_task_url.insert(id, url.clone()); - GLOBALS.relays_watching.write().await.push(url.clone()); + if let Some(mut ri) = GLOBALS.relays.get_mut(&url) { + ri.connected = true + } else { + tracing::error!("GLOBAL relays was missing a relay!: {}", url); + } Ok(()) } @@ -391,12 +395,12 @@ impl Overlord { // Minion probably already logged failure in relay table - // Remove from our urls_watching vec - GLOBALS - .relays_watching - .write() - .await - .retain(|value| *value != url); + // Set to not connected + if let Some(mut ri) = GLOBALS.relays.get_mut(&url) { + ri.connected = false; + } else { + tracing::error!("GLOBAL relays missing {}", url); + } // Remove from our hashmap self.minions_task_url.remove(&id); @@ -417,12 +421,12 @@ impl Overlord { Some(url) => { tracing::info!("Relay Task {} completed", &url); - // Remove from our urls_watching vec - GLOBALS - .relays_watching - .write() - .await - .retain(|value| *value != url); + // Set to not connected + if let Some(mut ri) = GLOBALS.relays.get_mut(&url) { + ri.connected = false; + } else { + tracing::error!("GLOBAL relays missing {}", url); + } // Remove from our hashmap self.minions_task_url.remove(&id); @@ -485,7 +489,7 @@ impl Overlord { GLOBALS.relays.insert( relay_str, RelayInfo { - dbrelay: dbrelay, + dbrelay, connected: false, assignments: vec![], subscriptions: vec![], @@ -658,12 +662,7 @@ impl Overlord { for person_relay in person_relays.iter() { // Start a minion for this relay if there is none - if !GLOBALS - .relays_watching - .read() - .await - .contains(&person_relay.relay) - { + if !GLOBALS.relay_is_connected(&person_relay.relay) { self.start_minion(person_relay.relay.clone()).await?; } @@ -860,7 +859,7 @@ impl Overlord { for url in relay_urls { // Start a minion for it, if there is none - if !GLOBALS.relays_watching.read().await.contains(&url) { + if !GLOBALS.relay_is_connected(&url) { self.start_minion(url.clone()).await?; } @@ -920,7 +919,7 @@ impl Overlord { for relay_url in advertise_to_relay_urls { // Start a minion for it, if there is none - if !GLOBALS.relays_watching.read().await.contains(&relay_url) { + if !GLOBALS.relay_is_connected(&relay_url) { self.start_minion(relay_url.clone()).await?; } @@ -986,7 +985,7 @@ impl Overlord { for relay in relays { // Start a minion for it, if there is none - if !GLOBALS.relays_watching.read().await.contains(&relay.url) { + if !GLOBALS.relay_is_connected(&relay.url) { self.start_minion(relay.url.clone()).await?; } @@ -1015,7 +1014,7 @@ impl Overlord { for relay in relays { // Start a minion for it, if there is none - if !GLOBALS.relays_watching.read().await.contains(&relay.url) { + if !GLOBALS.relay_is_connected(&relay.url) { self.start_minion(relay.url.clone()).await?; } @@ -1042,7 +1041,7 @@ impl Overlord { for relay in relays { // Start a minion for it, if there is none - if !GLOBALS.relays_watching.read().await.contains(&relay.url) { + if !GLOBALS.relay_is_connected(&relay.url) { self.start_minion(relay.url.clone()).await?; } @@ -1080,7 +1079,7 @@ impl Overlord { for relay in relays { // Start a minion for it, if there is none - if !GLOBALS.relays_watching.read().await.contains(&relay.url) { + if !GLOBALS.relay_is_connected(&relay.url) { self.start_minion(relay.url.clone()).await?; } @@ -1119,7 +1118,7 @@ impl Overlord { for (url, pubkeys) in map.drain() { // Start minion if needed - if !GLOBALS.relays_watching.read().await.contains(&url) { + if !GLOBALS.relay_is_connected(&url) { self.start_minion(url.clone()).await?; } @@ -1211,7 +1210,7 @@ impl Overlord { for url in relays.iter() { // Start minion if needed - if !GLOBALS.relays_watching.read().await.contains(url) { + if !GLOBALS.relay_is_connected(url) { self.start_minion(url.clone()).await?; } diff --git a/src/ui/relays/all.rs b/src/ui/relays/all.rs index c439efb5..395fe491 100644 --- a/src/ui/relays/all.rs +++ b/src/ui/relays/all.rs @@ -16,9 +16,7 @@ pub(super) fn update(app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Fr ui.add(TextEdit::singleline(&mut app.new_relay_url)); if ui.button("Add").clicked() { if let Ok(url) = RelayUrl::try_from_str(&app.new_relay_url) { - let _ = GLOBALS - .to_overlord - .send(ToOverlordMessage::AddRelay(url.clone())); + let _ = GLOBALS.to_overlord.send(ToOverlordMessage::AddRelay(url)); *GLOBALS.status_message.blocking_write() = format!( "I asked the overlord to add relay {}. Check for it below.", &app.new_relay_url diff --git a/src/ui/relays/mod.rs b/src/ui/relays/mod.rs index df727971..b2babdc1 100644 --- a/src/ui/relays/mod.rs +++ b/src/ui/relays/mod.rs @@ -5,6 +5,7 @@ use crate::relay_info::RelayAssignment; use eframe::egui; use egui::{Context, ScrollArea, SelectableLabel, Ui}; use egui_extras::{Column, TableBuilder}; +use nostr_types::RelayUrl; mod all; @@ -36,7 +37,13 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram ui.heading("Connected Relays"); ui.add_space(18.0); - let relays_watching = GLOBALS.relays_watching.blocking_read().clone(); + let relays_watching: Vec = GLOBALS + .relays + .iter() + .filter(|ri| ri.value().connected) + .map(|r| r.key().to_owned()) + .collect(); + let mut relay_assignments = GLOBALS.relay_assignments.blocking_read().clone(); let relays: Vec = relays_watching .iter()