From 112ff48e1a8f6496f69c8dfa26755acacd614f3c Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Thu, 14 Nov 2024 09:14:41 +0100 Subject: [PATCH] sdk: allow to specify relay pool notification channel size in `Options` Closes https://github.com/rust-nostr/nostr/issues/623 Signed-off-by: Yuki Kishimoto --- CHANGELOG.md | 1 + crates/nostr-relay-pool/src/pool/constants.rs | 5 +++++ crates/nostr-relay-pool/src/pool/mod.rs | 2 +- crates/nostr-relay-pool/src/pool/options.rs | 5 +++-- crates/nostr-relay-pool/src/prelude.rs | 2 ++ crates/nostr-sdk/src/client/options.rs | 7 +++++++ 6 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 507b88754..52bcc0223 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ * nostr: add NIP73 support ([Yuki Kishimoto]) * nostr: add `NostrSigner::backend` ([Yuki Kishimoto]) * pool: add relay reconnection and disconnection unit tests ([Yuki Kishimoto]) +* sdk: allow to specify relay pool notification channel size in `Options` ([Yuki Kishimoto]) * connect: add `NostrConnect::non_secure_set_user_public_key` ([Yuki Kishimoto]) * book: add some examples ([RydalWater]) diff --git a/crates/nostr-relay-pool/src/pool/constants.rs b/crates/nostr-relay-pool/src/pool/constants.rs index 1e46cbe80..798fba7aa 100644 --- a/crates/nostr-relay-pool/src/pool/constants.rs +++ b/crates/nostr-relay-pool/src/pool/constants.rs @@ -2,4 +2,9 @@ // Copyright (c) 2023-2024 Rust Nostr Developers // Distributed under the MIT software license +//! Constants + pub(super) const MAX_CONNECTING_CHUNK: usize = 100; + +/// Relay Pool default notification channel size +pub const DEFAULT_NOTIFICATION_CHANNEL_SIZE: usize = 4096; diff --git a/crates/nostr-relay-pool/src/pool/mod.rs b/crates/nostr-relay-pool/src/pool/mod.rs index feeda63ad..de051bd41 100644 --- a/crates/nostr-relay-pool/src/pool/mod.rs +++ b/crates/nostr-relay-pool/src/pool/mod.rs @@ -15,7 +15,7 @@ use nostr_database::{DynNostrDatabase, Events, IntoNostrDatabase, MemoryDatabase use tokio::sync::broadcast; pub use tokio_stream::wrappers::ReceiverStream; -mod constants; +pub mod constants; mod error; mod inner; pub mod options; diff --git a/crates/nostr-relay-pool/src/pool/options.rs b/crates/nostr-relay-pool/src/pool/options.rs index e4343c7ae..52f256373 100644 --- a/crates/nostr-relay-pool/src/pool/options.rs +++ b/crates/nostr-relay-pool/src/pool/options.rs @@ -4,6 +4,7 @@ //! Pool options +use super::constants::DEFAULT_NOTIFICATION_CHANNEL_SIZE; use crate::relay::RelayFilteringMode; /// Relay Pool Options @@ -18,7 +19,7 @@ impl Default for RelayPoolOptions { fn default() -> Self { Self { max_relays: None, - notification_channel_size: 4096, + notification_channel_size: DEFAULT_NOTIFICATION_CHANNEL_SIZE, filtering_mode: RelayFilteringMode::default(), } } @@ -38,7 +39,7 @@ impl RelayPoolOptions { self } - /// Notification channel size (default: 4096) + /// Notification channel size (default: [`DEFAULT_NOTIFICATION_CHANNEL_SIZE`]) #[inline] pub fn notification_channel_size(mut self, size: usize) -> Self { self.notification_channel_size = size; diff --git a/crates/nostr-relay-pool/src/prelude.rs b/crates/nostr-relay-pool/src/prelude.rs index 83c1c318a..10412efb1 100644 --- a/crates/nostr-relay-pool/src/prelude.rs +++ b/crates/nostr-relay-pool/src/prelude.rs @@ -13,6 +13,8 @@ pub use async_utility::futures_util::StreamExt; // Needed for `RelayPool::stream pub use nostr::prelude::*; pub use nostr_database::*; +pub use crate::pool::constants::*; +pub use crate::pool::options::*; // Internal modules pub use crate::pool::{self, *}; pub use crate::relay::{self, *}; diff --git a/crates/nostr-sdk/src/client/options.rs b/crates/nostr-sdk/src/client/options.rs index 83ee45b53..9d76fb9e2 100644 --- a/crates/nostr-sdk/src/client/options.rs +++ b/crates/nostr-sdk/src/client/options.rs @@ -183,6 +183,13 @@ impl Options { self.pool = self.pool.filtering_mode(mode); self } + + /// Notification channel size (default: [`DEFAULT_NOTIFICATION_CHANNEL_SIZE`]) + #[inline] + pub fn notification_channel_size(mut self, size: usize) -> Self { + self.pool = self.pool.notification_channel_size(size); + self + } } /// Connection target