Skip to content

Commit b309592

Browse files
committed
Merge #172: Custom time of reconnect options added
2 parents c612a16 + 7ceae16 commit b309592

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

crates/nostr-sdk/examples/client-with-opts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async fn main() -> Result<()> {
2222
client.add_relay("wss://relay.damus.io", None).await?;
2323
client.add_relay("wss://nostr.openchain.fr", None).await?;
2424
client
25-
.add_relay_with_opts("wss://nostr.mom", None, RelayOptions::new(true, false))
25+
.add_relay_with_opts("wss://nostr.mom", None, RelayOptions::new(true, false, 10))
2626
.await?;
2727
client
2828
.add_relay(

crates/nostr-sdk/src/client/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ impl Client {
332332
/// # let client = Client::new(&my_keys);
333333
/// let read = true;
334334
/// let write = false;
335-
/// let opts = RelayOptions::new(read, write);
335+
/// let retry_sec = 10;
336+
/// let opts = RelayOptions::new(read, write, retry_sec);
336337
/// client
337338
/// .add_relay_with_opts("wss://relay.nostr.info", None, opts)
338339
/// .await

crates/nostr-sdk/src/relay/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ impl Relay {
679679
_ => (),
680680
};
681681

682-
thread::sleep(Duration::from_secs(10)).await;
682+
thread::sleep(Duration::from_secs(relay.opts().retry_sec())).await;
683683
}
684684

685685
relay.set_auto_connect_loop_running(false);

crates/nostr-sdk/src/relay/options.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2022-2023 Yuki Kishimoto
22
// Distributed under the MIT software license
33

4-
use std::sync::atomic::{AtomicBool, Ordering};
4+
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
55
use std::sync::Arc;
66
use std::time::Duration;
77

@@ -14,20 +14,23 @@ pub struct RelayOptions {
1414
read: Arc<AtomicBool>,
1515
/// Allow/disallow write actions
1616
write: Arc<AtomicBool>,
17+
/// Retry connection time - avoid reconnection
18+
retry_sec: Arc<AtomicU64>,
1719
}
1820

1921
impl Default for RelayOptions {
2022
fn default() -> Self {
21-
Self::new(true, true)
23+
Self::new(true, true, 10)
2224
}
2325
}
2426

2527
impl RelayOptions {
2628
/// New [`RelayOptions`]
27-
pub fn new(read: bool, write: bool) -> Self {
29+
pub fn new(read: bool, write: bool, retry_sec: u64) -> Self {
2830
Self {
2931
read: Arc::new(AtomicBool::new(read)),
3032
write: Arc::new(AtomicBool::new(write)),
33+
retry_sec: Arc::new(AtomicU64::new(retry_sec)),
3134
}
3235
}
3336

@@ -54,6 +57,18 @@ impl RelayOptions {
5457
.write
5558
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |_| Some(write));
5659
}
60+
61+
/// Get retry_sec option
62+
pub fn retry_sec(&self) -> u64 {
63+
self.retry_sec.load(Ordering::SeqCst)
64+
}
65+
66+
/// Set retry_sec option
67+
pub fn set_retry_sec(&self, retry_sec: u64) {
68+
let _ = self
69+
.retry_sec
70+
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |_| Some(retry_sec));
71+
}
5772
}
5873

5974
/// [`Relay`](super::Relay) send options

0 commit comments

Comments
 (0)