Skip to content

Commit

Permalink
Custom time of reconnect options added
Browse files Browse the repository at this point in the history
  • Loading branch information
arkanoider committed Sep 28, 2023
1 parent c612a16 commit 8e2acee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
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 8e2acee

Please sign in to comment.