Skip to content

Commit

Permalink
sdk: add with_database constructor to RelayPool
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Oct 16, 2023
1 parent 73b7d9e commit 60b41be
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/nostr-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#![warn(missing_docs)]
#![warn(rustdoc::bare_urls)]
#![allow(clippy::arc_with_non_send_sync)]

//! High level Nostr client library.
Expand Down
1 change: 1 addition & 0 deletions crates/nostr-sdk/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ pub struct Relay {
document: Arc<RwLock<RelayInformationDocument>>,
opts: RelayOptions,
stats: RelayConnectionStats,
#[allow(dead_code)]
database: Arc<DynNostrDatabase>,
scheduled_for_stop: Arc<AtomicBool>,
scheduled_for_termination: Arc<AtomicBool>,
Expand Down
14 changes: 10 additions & 4 deletions crates/nostr-sdk/src/relay/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down Expand Up @@ -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<DynNostrDatabase>) -> 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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 60b41be

Please sign in to comment.