Replace GLOBALS.relays_watching with GLOBALS.relays (connected field)

This commit is contained in:
Mike Dilger 2023-02-06 17:14:35 +13:00
parent cbf63bfc63
commit 257ba7c31e
4 changed files with 51 additions and 47 deletions

View File

@ -1,5 +1,5 @@
use crate::db::DbRelay;
use crate::comms::{ToMinionMessage, ToOverlordMessage}; use crate::comms::{ToMinionMessage, ToOverlordMessage};
use crate::db::DbRelay;
use crate::events::Events; use crate::events::Events;
use crate::feed::Feed; use crate::feed::Feed;
use crate::fetcher::Fetcher; use crate::fetcher::Fetcher;
@ -51,11 +51,8 @@ pub struct Globals {
/// All nostr relay records we have, with associated info /// All nostr relay records we have, with associated info
pub relays: DashMap<RelayUrl, RelayInfo>, pub relays: DashMap<RelayUrl, RelayInfo>,
/// The relays we are currently connected to
pub relays_watching: RwLock<Vec<RelayUrl>>,
/// These are the relays we are currently connected to for general feed, along with /// 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. /// has data the other doesn't.
pub relay_assignments: RwLock<Vec<RelayAssignment>>, pub relay_assignments: RwLock<Vec<RelayAssignment>>,
@ -111,7 +108,6 @@ lazy_static! {
relationships: RwLock::new(HashMap::new()), relationships: RwLock::new(HashMap::new()),
people: People::new(), people: People::new(),
relays: DashMap::new(), relays: DashMap::new(),
relays_watching: RwLock::new(Vec::new()),
relay_assignments: RwLock::new(Vec::new()), relay_assignments: RwLock::new(Vec::new()),
relay_picker: RwLock::new(Default::default()), relay_picker: RwLock::new(Default::default()),
shutting_down: AtomicBool::new(false), shutting_down: AtomicBool::new(false),
@ -225,10 +221,10 @@ impl Globals {
} }
pub fn relays_filtered<F>(&self, mut f: F) -> Vec<DbRelay> pub fn relays_filtered<F>(&self, mut f: F) -> Vec<DbRelay>
where F: FnMut(&DbRelay) -> bool where
F: FnMut(&DbRelay) -> bool,
{ {
self self.relays
.relays
.iter() .iter()
.filter_map(|r| { .filter_map(|r| {
if f(&r.value().dbrelay) { if f(&r.value().dbrelay) {
@ -238,14 +234,13 @@ impl Globals {
} }
}) })
.collect() .collect()
} }
pub fn relays_url_filtered<F>(&self, mut f: F) -> Vec<RelayUrl> pub fn relays_url_filtered<F>(&self, mut f: F) -> Vec<RelayUrl>
where F: FnMut(&DbRelay) -> bool where
F: FnMut(&DbRelay) -> bool,
{ {
self self.relays
.relays
.iter() .iter()
.filter_map(|r| { .filter_map(|r| {
if f(&r.value().dbrelay) { if f(&r.value().dbrelay) {
@ -255,6 +250,11 @@ impl Globals {
} }
}) })
.collect() .collect()
}
pub fn relay_is_connected(&self, url: &RelayUrl) -> bool {
self.relays
.iter()
.any(|ri| ri.value().dbrelay.url == *url && ri.value().connected)
} }
} }

View File

@ -101,7 +101,7 @@ impl Overlord {
GLOBALS.relays.insert( GLOBALS.relays.insert(
dbrelay.url.clone(), dbrelay.url.clone(),
RelayInfo { RelayInfo {
dbrelay: dbrelay, dbrelay,
connected: false, connected: false,
assignments: vec![], assignments: vec![],
subscriptions: vec![], subscriptions: vec![],
@ -229,7 +229,7 @@ impl Overlord {
let read_relay_urls: Vec<RelayUrl> = GLOBALS.relays_url_filtered(|r| r.read); let read_relay_urls: Vec<RelayUrl> = GLOBALS.relays_url_filtered(|r| r.read);
for relay_url in read_relay_urls.iter() { for relay_url in read_relay_urls.iter() {
// Start a minion for this relay if there is none // 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?; 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 abort_handle = self.minions.spawn(async move { minion.handle().await });
let id = abort_handle.id(); let id = abort_handle.id();
self.minions_task_url.insert(id, url.clone()); 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(()) Ok(())
} }
@ -391,12 +395,12 @@ impl Overlord {
// Minion probably already logged failure in relay table // Minion probably already logged failure in relay table
// Remove from our urls_watching vec // Set to not connected
GLOBALS if let Some(mut ri) = GLOBALS.relays.get_mut(&url) {
.relays_watching ri.connected = false;
.write() } else {
.await tracing::error!("GLOBAL relays missing {}", url);
.retain(|value| *value != url); }
// Remove from our hashmap // Remove from our hashmap
self.minions_task_url.remove(&id); self.minions_task_url.remove(&id);
@ -417,12 +421,12 @@ impl Overlord {
Some(url) => { Some(url) => {
tracing::info!("Relay Task {} completed", &url); tracing::info!("Relay Task {} completed", &url);
// Remove from our urls_watching vec // Set to not connected
GLOBALS if let Some(mut ri) = GLOBALS.relays.get_mut(&url) {
.relays_watching ri.connected = false;
.write() } else {
.await tracing::error!("GLOBAL relays missing {}", url);
.retain(|value| *value != url); }
// Remove from our hashmap // Remove from our hashmap
self.minions_task_url.remove(&id); self.minions_task_url.remove(&id);
@ -485,7 +489,7 @@ impl Overlord {
GLOBALS.relays.insert( GLOBALS.relays.insert(
relay_str, relay_str,
RelayInfo { RelayInfo {
dbrelay: dbrelay, dbrelay,
connected: false, connected: false,
assignments: vec![], assignments: vec![],
subscriptions: vec![], subscriptions: vec![],
@ -658,12 +662,7 @@ impl Overlord {
for person_relay in person_relays.iter() { for person_relay in person_relays.iter() {
// Start a minion for this relay if there is none // Start a minion for this relay if there is none
if !GLOBALS if !GLOBALS.relay_is_connected(&person_relay.relay) {
.relays_watching
.read()
.await
.contains(&person_relay.relay)
{
self.start_minion(person_relay.relay.clone()).await?; self.start_minion(person_relay.relay.clone()).await?;
} }
@ -860,7 +859,7 @@ impl Overlord {
for url in relay_urls { for url in relay_urls {
// Start a minion for it, if there is none // 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?; self.start_minion(url.clone()).await?;
} }
@ -920,7 +919,7 @@ impl Overlord {
for relay_url in advertise_to_relay_urls { for relay_url in advertise_to_relay_urls {
// Start a minion for it, if there is none // 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?; self.start_minion(relay_url.clone()).await?;
} }
@ -986,7 +985,7 @@ impl Overlord {
for relay in relays { for relay in relays {
// Start a minion for it, if there is none // 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?; self.start_minion(relay.url.clone()).await?;
} }
@ -1015,7 +1014,7 @@ impl Overlord {
for relay in relays { for relay in relays {
// Start a minion for it, if there is none // 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?; self.start_minion(relay.url.clone()).await?;
} }
@ -1042,7 +1041,7 @@ impl Overlord {
for relay in relays { for relay in relays {
// Start a minion for it, if there is none // 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?; self.start_minion(relay.url.clone()).await?;
} }
@ -1080,7 +1079,7 @@ impl Overlord {
for relay in relays { for relay in relays {
// Start a minion for it, if there is none // 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?; self.start_minion(relay.url.clone()).await?;
} }
@ -1119,7 +1118,7 @@ impl Overlord {
for (url, pubkeys) in map.drain() { for (url, pubkeys) in map.drain() {
// Start minion if needed // Start minion if needed
if !GLOBALS.relays_watching.read().await.contains(&url) { if !GLOBALS.relay_is_connected(&url) {
self.start_minion(url.clone()).await?; self.start_minion(url.clone()).await?;
} }
@ -1211,7 +1210,7 @@ impl Overlord {
for url in relays.iter() { for url in relays.iter() {
// Start minion if needed // Start minion if needed
if !GLOBALS.relays_watching.read().await.contains(url) { if !GLOBALS.relay_is_connected(url) {
self.start_minion(url.clone()).await?; self.start_minion(url.clone()).await?;
} }

View File

@ -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)); ui.add(TextEdit::singleline(&mut app.new_relay_url));
if ui.button("Add").clicked() { if ui.button("Add").clicked() {
if let Ok(url) = RelayUrl::try_from_str(&app.new_relay_url) { if let Ok(url) = RelayUrl::try_from_str(&app.new_relay_url) {
let _ = GLOBALS let _ = GLOBALS.to_overlord.send(ToOverlordMessage::AddRelay(url));
.to_overlord
.send(ToOverlordMessage::AddRelay(url.clone()));
*GLOBALS.status_message.blocking_write() = format!( *GLOBALS.status_message.blocking_write() = format!(
"I asked the overlord to add relay {}. Check for it below.", "I asked the overlord to add relay {}. Check for it below.",
&app.new_relay_url &app.new_relay_url

View File

@ -5,6 +5,7 @@ use crate::relay_info::RelayAssignment;
use eframe::egui; use eframe::egui;
use egui::{Context, ScrollArea, SelectableLabel, Ui}; use egui::{Context, ScrollArea, SelectableLabel, Ui};
use egui_extras::{Column, TableBuilder}; use egui_extras::{Column, TableBuilder};
use nostr_types::RelayUrl;
mod all; mod all;
@ -36,7 +37,13 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram
ui.heading("Connected Relays"); ui.heading("Connected Relays");
ui.add_space(18.0); ui.add_space(18.0);
let relays_watching = GLOBALS.relays_watching.blocking_read().clone(); let relays_watching: Vec<RelayUrl> = 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 mut relay_assignments = GLOBALS.relay_assignments.blocking_read().clone();
let relays: Vec<RelayAssignment> = relays_watching let relays: Vec<RelayAssignment> = relays_watching
.iter() .iter()