From 4cc313fa2d8006f21a4219551a2a354145cfaadd Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Sun, 30 Jan 2022 15:14:02 -0600 Subject: [PATCH] fix: cleanup database connections with same name When a large number of subscriptions is created with identical names, we do not send a signal over the abandon-read channel. This eventually leads to resource exhaustion. --- src/main.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 95aa2a3..1b04114 100644 --- a/src/main.rs +++ b/src/main.rs @@ -280,7 +280,6 @@ async fn nostr_server( // maintain a hashmap of a oneshot channel for active subscriptions. // when these subscriptions are cancelled, make a message // available to the executing query so it knows to stop. - //let (abandon_query_tx, _) = oneshot::channel::<()>(); let mut running_queries: HashMap> = HashMap::new(); // for stats, keep track of how many events the client published, // and how many it received from queries. @@ -348,7 +347,10 @@ async fn nostr_server( let (abandon_query_tx, abandon_query_rx) = oneshot::channel::<()>(); match conn.subscribe(s.clone()) { Ok(()) => { - running_queries.insert(s.id.to_owned(), abandon_query_tx); + // when we insert, if there was a previous query running with the same name, cancel it. + if let Some(previous_query) = running_queries.insert(s.id.to_owned(), abandon_query_tx) { + previous_query.send(()).ok(); + } // start a database query // show pool stats debug!("DB pool stats: {:?}", pool.state());