From 60b41be9b6e327c73d81098e808535f837e2f68a Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Mon, 16 Oct 2023 14:43:27 +0200 Subject: [PATCH] sdk: add `with_database` constructor to `RelayPool` --- crates/nostr-sdk/src/lib.rs | 1 + crates/nostr-sdk/src/relay/mod.rs | 1 + crates/nostr-sdk/src/relay/pool.rs | 14 ++++++++++---- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/nostr-sdk/src/lib.rs b/crates/nostr-sdk/src/lib.rs index 41dfefb8b..8ccfb1216 100644 --- a/crates/nostr-sdk/src/lib.rs +++ b/crates/nostr-sdk/src/lib.rs @@ -3,6 +3,7 @@ #![warn(missing_docs)] #![warn(rustdoc::bare_urls)] +#![allow(clippy::arc_with_non_send_sync)] //! High level Nostr client library. diff --git a/crates/nostr-sdk/src/relay/mod.rs b/crates/nostr-sdk/src/relay/mod.rs index 1885fe1a7..1c13cc62b 100644 --- a/crates/nostr-sdk/src/relay/mod.rs +++ b/crates/nostr-sdk/src/relay/mod.rs @@ -257,6 +257,7 @@ pub struct Relay { document: Arc>, opts: RelayOptions, stats: RelayConnectionStats, + #[allow(dead_code)] database: Arc, scheduled_for_stop: Arc, scheduled_for_termination: Arc, diff --git a/crates/nostr-sdk/src/relay/pool.rs b/crates/nostr-sdk/src/relay/pool.rs index 4b2e73d18..e2d50c26a 100644 --- a/crates/nostr-sdk/src/relay/pool.rs +++ b/crates/nostr-sdk/src/relay/pool.rs @@ -199,8 +199,10 @@ impl RelayPoolTask { } // Set event as seen by relay - if let Err(e) = - this.database.event_id_seen(event.id, Some(relay_url)).await + if let Err(e) = this + .database + .event_id_seen(event.id, Some(relay_url)) + .await { tracing::error!( "Impossible to set event {} as seen by relay: {e}", @@ -329,11 +331,14 @@ impl Drop for RelayPool { impl RelayPool { /// Create new `RelayPool` pub fn new(opts: RelayPoolOptions) -> Self { + Self::with_database(opts, Arc::new(MemoryDatabase::new())) + } + + /// New with database + pub fn with_database(opts: RelayPoolOptions, database: Arc) -> Self { let (notification_sender, _) = broadcast::channel(opts.notification_channel_size); let (pool_task_sender, pool_task_receiver) = mpsc::channel(opts.task_channel_size); - let database = Arc::new(MemoryDatabase::new()); - let relay_pool_task = RelayPoolTask::new( database.clone(), pool_task_receiver, @@ -467,6 +472,7 @@ impl RelayPool { if !relays.contains_key(&url) { let relay = Relay::new( url, + self.database.clone(), self.pool_task_sender.clone(), self.notification_sender.clone(), opts,