From 5f9fe1ce59a20c1818c3d9afbc60313ac9cb51cb Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Mon, 26 Dec 2022 12:20:36 -0600 Subject: [PATCH] improvement: do not send realtime only filters to the DB (limit:0) --- src/server.rs | 4 +++- src/subscription.rs | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/server.rs b/src/server.rs index f543db7..4b9b4ea 100644 --- a/src/server.rs +++ b/src/server.rs @@ -674,8 +674,10 @@ async fn nostr_server( if let Some(previous_query) = running_queries.insert(s.id.to_owned(), abandon_query_tx) { previous_query.send(()).ok(); } + if s.needs_historical_events() { // start a database query. this spawns a blocking database query on a worker thread. - db::db_query(s, cid.to_owned(), pool.clone(), query_tx.clone(), abandon_query_rx).await; + db::db_query(s, cid.to_owned(), pool.clone(), query_tx.clone(), abandon_query_rx).await; + } }, Err(e) => { info!("Subscription error: {} (cid: {}, sub: {:?})", e, cid, s.id); diff --git a/src/subscription.rs b/src/subscription.rs index 972e978..f019ce2 100644 --- a/src/subscription.rs +++ b/src/subscription.rs @@ -200,6 +200,13 @@ impl Subscription { pub fn get_id(&self) -> String { self.id.clone() } + + /// Determine if any filter is requesting historical (database) + /// queries. If every filter has limit:0, we do not need to query the DB. + pub fn needs_historical_events(&self) -> bool { + self.filters.iter().any(|f| f.limit!=Some(0)) + } + /// Determine if this subscription matches a given [`Event`]. Any /// individual filter match is sufficient. pub fn interested_in_event(&self, event: &Event) -> bool {