Skip to content

Commit 8e2acee

Browse files
committed
Custom time of reconnect options added
1 parent c612a16 commit 8e2acee

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

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)