Skip to content

Commit

Permalink
Merge #172: Custom time of reconnect options added
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Sep 28, 2023
2 parents c612a16 + 7ceae16 commit b309592
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crates/nostr-sdk/examples/client-with-opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion crates/nostr-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr-sdk/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 18 additions & 3 deletions crates/nostr-sdk/src/relay/options.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -14,20 +14,23 @@ pub struct RelayOptions {
read: Arc<AtomicBool>,
/// Allow/disallow write actions
write: Arc<AtomicBool>,
/// Retry connection time - avoid reconnection
retry_sec: Arc<AtomicU64>,
}

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)),
}
}

Expand All @@ -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
Expand Down

0 comments on commit b309592

Please sign in to comment.