Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom time of reconnect options added #172

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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