Skip to content

Commit 1d6d47d

Browse files
committed
sdk: allow enabling sleep-when-idle feature
- Add `ClientOptions::sleep_when_idle` Closes #927 Pull-Request: #959 Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent cc5a2ff commit 1d6d47d

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

crates/nostr-sdk/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929

3030
- Extract at max 3 relays per NIP65 marker (https://github.com/rust-nostr/nostr/pull/951)
3131

32+
### Added
33+
34+
- Add `ClientOptions::sleep_when_idle` (https://github.com/rust-nostr/nostr/pull/959)
35+
3236
### Deprecated
3337

3438
- Deprecate `Options` in favor of `ClientOptions` (https://github.com/rust-nostr/nostr/pull/958)

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub mod options;
2121

2222
pub use self::builder::ClientBuilder;
2323
pub use self::error::Error;
24-
pub use self::options::ClientOptions;
24+
pub use self::options::{ClientOptions, SleepWhenIdle};
2525
#[cfg(not(target_arch = "wasm32"))]
2626
pub use self::options::{Connection, ConnectionTarget};
2727
use crate::gossip::{BrokenDownFilters, Gossip};
@@ -245,6 +245,14 @@ impl Client {
245245
},
246246
};
247247

248+
// Set sleep when idle
249+
let opts: RelayOptions = match self.opts.sleep_when_idle {
250+
// Do nothing
251+
SleepWhenIdle::Disabled => opts,
252+
// Enable: update relay options
253+
SleepWhenIdle::Enabled { timeout } => opts.sleep_when_idle(true).idle_timeout(timeout),
254+
};
255+
248256
// Set limits
249257
opts.limits(self.opts.relay_limits.clone())
250258
.max_avg_latency(self.opts.max_avg_latency)

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct ClientOptions {
2525
pub(super) connection: Connection,
2626
pub(super) relay_limits: RelayLimits,
2727
pub(super) max_avg_latency: Option<Duration>,
28+
pub(super) sleep_when_idle: SleepWhenIdle,
2829
pub(super) pool: RelayPoolOptions,
2930
}
3031

@@ -99,6 +100,13 @@ impl ClientOptions {
99100
self
100101
}
101102

103+
/// Set sleep when idle config
104+
#[inline]
105+
pub fn sleep_when_idle(mut self, config: SleepWhenIdle) -> Self {
106+
self.sleep_when_idle = config;
107+
self
108+
}
109+
102110
/// Notification channel size (default: [`DEFAULT_NOTIFICATION_CHANNEL_SIZE`])
103111
#[deprecated(since = "0.42.0", note = "Use `Options::pool` instead.")]
104112
pub fn notification_channel_size(mut self, size: usize) -> Self {
@@ -114,6 +122,21 @@ impl ClientOptions {
114122
}
115123
}
116124

125+
/// Put relays to sleep when idle.
126+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
127+
pub enum SleepWhenIdle {
128+
/// Disabled
129+
#[default]
130+
Disabled,
131+
/// Enabled for all relays
132+
Enabled {
133+
/// Idle timeout
134+
///
135+
/// After how much time of inactivity put the relay to sleep.
136+
timeout: Duration,
137+
},
138+
}
139+
117140
/// Connection target
118141
#[cfg(not(target_arch = "wasm32"))]
119142
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]

0 commit comments

Comments
 (0)