From 8e2acee589317ee718a7a5784e961e5178949a2e Mon Sep 17 00:00:00 2001 From: arkanoider Date: Thu, 28 Sep 2023 14:39:48 +0200 Subject: [PATCH 1/2] Custom time of reconnect options added --- crates/nostr-sdk/src/relay/mod.rs | 2 +- crates/nostr-sdk/src/relay/options.rs | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/crates/nostr-sdk/src/relay/mod.rs b/crates/nostr-sdk/src/relay/mod.rs index 5164394bd..626b32671 100644 --- a/crates/nostr-sdk/src/relay/mod.rs +++ b/crates/nostr-sdk/src/relay/mod.rs @@ -679,7 +679,7 @@ impl Relay { _ => (), }; - thread::sleep(Duration::from_secs(10)).await; + thread::sleep(Duration::from_secs(relay.opts().retry_sec())).await; } relay.set_auto_connect_loop_running(false); diff --git a/crates/nostr-sdk/src/relay/options.rs b/crates/nostr-sdk/src/relay/options.rs index c2696ed41..0cb83cf11 100644 --- a/crates/nostr-sdk/src/relay/options.rs +++ b/crates/nostr-sdk/src/relay/options.rs @@ -1,7 +1,7 @@ // Copyright (c) 2022-2023 Yuki Kishimoto // Distributed under the MIT software license -use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::Arc; use std::time::Duration; @@ -14,20 +14,23 @@ pub struct RelayOptions { read: Arc, /// Allow/disallow write actions write: Arc, + /// Retry connection time - avoid reconnection + retry_sec: Arc, } impl Default for RelayOptions { fn default() -> Self { - Self::new(true, true) + Self::new(true, true, 10) } } impl RelayOptions { /// New [`RelayOptions`] - pub fn new(read: bool, write: bool) -> Self { + pub fn new(read: bool, write: bool, retry_sec: u64) -> Self { Self { read: Arc::new(AtomicBool::new(read)), write: Arc::new(AtomicBool::new(write)), + retry_sec: Arc::new(AtomicU64::new(retry_sec)), } } @@ -54,6 +57,18 @@ impl RelayOptions { .write .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |_| Some(write)); } + + /// Get retry_sec option + pub fn retry_sec(&self) -> u64 { + self.retry_sec.load(Ordering::SeqCst) + } + + /// Set retry_sec option + pub fn set_retry_sec(&self, retry_sec: u64) { + let _ = self + .retry_sec + .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |_| Some(retry_sec)); + } } /// [`Relay`](super::Relay) send options From 7ceae166e23560275255dc2989de4e4bc40a30a1 Mon Sep 17 00:00:00 2001 From: arkanoider Date: Thu, 28 Sep 2023 16:04:05 +0200 Subject: [PATCH 2/2] fixes for test build --- crates/nostr-sdk/examples/client-with-opts.rs | 2 +- crates/nostr-sdk/src/client/mod.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/nostr-sdk/examples/client-with-opts.rs b/crates/nostr-sdk/examples/client-with-opts.rs index fd7be177c..65c7714bb 100644 --- a/crates/nostr-sdk/examples/client-with-opts.rs +++ b/crates/nostr-sdk/examples/client-with-opts.rs @@ -22,7 +22,7 @@ async fn main() -> Result<()> { client.add_relay("wss://relay.damus.io", None).await?; client.add_relay("wss://nostr.openchain.fr", None).await?; client - .add_relay_with_opts("wss://nostr.mom", None, RelayOptions::new(true, false)) + .add_relay_with_opts("wss://nostr.mom", None, RelayOptions::new(true, false, 10)) .await?; client .add_relay( diff --git a/crates/nostr-sdk/src/client/mod.rs b/crates/nostr-sdk/src/client/mod.rs index 50c50769b..a11f87532 100644 --- a/crates/nostr-sdk/src/client/mod.rs +++ b/crates/nostr-sdk/src/client/mod.rs @@ -332,7 +332,8 @@ impl Client { /// # let client = Client::new(&my_keys); /// let read = true; /// let write = false; - /// let opts = RelayOptions::new(read, write); + /// let retry_sec = 10; + /// let opts = RelayOptions::new(read, write, retry_sec); /// client /// .add_relay_with_opts("wss://relay.nostr.info", None, opts) /// .await