From 0b9778d6cac66c1c606e9629341947406082b6e0 Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Sat, 17 Dec 2022 20:46:58 -0600 Subject: [PATCH] refactor: simplify tracking of subscriptions --- src/conn.rs | 5 +++++ src/server.rs | 12 ++---------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/conn.rs b/src/conn.rs index bd41696..4b7e0ba 100644 --- a/src/conn.rs +++ b/src/conn.rs @@ -46,6 +46,11 @@ impl ClientConn { &self.subscriptions } + /// Check if the given subscription already exists + pub fn has_subscription(&self, sub: &Subscription) -> bool { + self.subscriptions.values().any(|x| x == sub) + } + /// Get a short prefix of the client's unique identifier, suitable /// for logging. #[must_use] diff --git a/src/server.rs b/src/server.rs index 10d98ca..f94aece 100644 --- a/src/server.rs +++ b/src/server.rs @@ -476,8 +476,6 @@ async fn nostr_server( // when these subscriptions are cancelled, make a message // available to the executing query so it knows to stop. let mut running_queries: HashMap> = HashMap::new(); - // keep track of the subscriptions we have - let mut current_subs: Vec = Vec::new(); // for stats, keep track of how many events the client published, // and how many it received from queries. let mut client_published_event_count: usize = 0; @@ -625,11 +623,10 @@ async fn nostr_server( // * making a channel to cancel to request later // * sending a request for a SQL query // Do nothing if the sub already exists. - if !current_subs.contains(&s) { + if !conn.has_subscription(&s) { if let Some(ref lim) = sub_lim_opt { - lim.until_ready_with_jitter(jitter).await; + lim.until_ready_with_jitter(jitter).await; } - current_subs.push(s.clone()); let (abandon_query_tx, abandon_query_rx) = oneshot::channel::<()>(); match conn.subscribe(s.clone()) { Ok(()) => { @@ -653,11 +650,6 @@ async fn nostr_server( // closing a request simply removes the subscription. let parsed : Result = Result::::from(cc); if let Ok(c) = parsed { - // remove from the list of known subs - if let Some(pos) = current_subs.iter().position(|s| *s.id == c.id) { - current_subs.remove(pos); - } - // check if a query is currently // running, and remove it if so. let stop_tx = running_queries.remove(&c.id);