Skip to content

Commit

Permalink
pool: refactor atomic fields in RelayOptions to simple types
Browse files Browse the repository at this point in the history
Replaced atomic types with standard types in the `RelayOptions` struct for simplifying the code.

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Nov 3, 2024
1 parent 8da0d28 commit dec5b46
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 87 deletions.
15 changes: 0 additions & 15 deletions bindings/nostr-sdk-ffi/src/relay/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@ impl RelayOptions {
builder
}

/// Update `reconnect` option
pub fn update_reconnect(&self, reconnect: bool) {
self.inner.update_reconnect(reconnect);
}

/// Retry connection time (default: 10 sec)
///
/// Are allowed values `>=` 5 secs
Expand All @@ -147,23 +142,13 @@ impl RelayOptions {
builder
}

/// Set retry_sec option
pub fn update_retry_sec(&self, retry_sec: u64) {
self.inner.update_retry_sec(retry_sec);
}

/// Automatically adjust retry seconds based on success/attempts (default: true)
pub fn adjust_retry_sec(self: Arc<Self>, adjust_retry_sec: bool) -> Self {
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.adjust_retry_sec(adjust_retry_sec);
builder
}

/// Set adjust_retry_sec option
pub fn update_adjust_retry_sec(&self, adjust_retry_sec: bool) {
self.inner.update_adjust_retry_sec(adjust_retry_sec);
}

/// Set custom limits
pub fn limits(self: Arc<Self>, limits: &RelayLimits) -> Self {
let mut builder = unwrap_or_clone_arc(self);
Expand Down
15 changes: 0 additions & 15 deletions bindings/nostr-sdk-js/src/relay/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,33 +77,18 @@ impl JsRelayOptions {
self.inner.reconnect(reconnect).into()
}

/// Update `reconnect` option
pub fn update_reconnect(&self, reconnect: bool) {
self.inner.update_reconnect(reconnect);
}

/// Retry connection time (default: 10 sec)
///
/// Are allowed values `>=` 5 secs
pub fn retry_sec(self, retry_sec: u64) -> Self {
self.inner.retry_sec(retry_sec).into()
}

/// Set retry_sec option
pub fn update_retry_sec(&self, retry_sec: u64) {
self.inner.update_retry_sec(retry_sec);
}

/// Automatically adjust retry seconds based on success/attempts (default: true)
pub fn adjust_retry_sec(self, adjust_retry_sec: bool) -> Self {
self.inner.adjust_retry_sec(adjust_retry_sec).into()
}

/// Set adjust_retry_sec option
pub fn update_adjust_retry_sec(&self, adjust_retry_sec: bool) {
self.inner.update_adjust_retry_sec(adjust_retry_sec);
}

/// Set custom limits
pub fn limits(self, limits: &JsRelayLimits) -> Self {
self.inner.limits(limits.deref().clone()).into()
Expand Down
6 changes: 3 additions & 3 deletions crates/nostr-relay-pool/src/relay/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ impl InnerRelay {
}

// Check if reconnection is enabled
if relay.opts.get_reconnect() {
if relay.opts.reconnect {
// Sleep before retry to connect
let retry_sec: u64 = relay.calculate_retry_sec();
tracing::info!("Reconnecting to '{}' relay in {retry_sec} secs", relay.url);
Expand All @@ -496,7 +496,7 @@ impl InnerRelay {

/// Depending on attempts and success, use default or incremental retry time
fn calculate_retry_sec(&self) -> u64 {
if self.opts.get_adjust_retry_sec() {
if self.opts.adjust_retry_sec {
// diff = attempts - success
let diff: u64 = self.stats.attempts().saturating_sub(self.stats.success()) as u64;

Expand All @@ -507,7 +507,7 @@ impl InnerRelay {
}

// Use default retry time
self.opts.get_retry_sec()
self.opts.retry_sec
}

/// Connect and run message handler
Expand Down
81 changes: 27 additions & 54 deletions crates/nostr-relay-pool/src/relay/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

//! Relay options
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicU8, Ordering};
use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::Arc;
use std::time::Duration;

Expand All @@ -16,15 +16,16 @@ use super::filtering::RelayFilteringMode;
use super::flags::RelayServiceFlags;
use crate::RelayLimits;

/// [`Relay`](super::Relay) options
/// Relay options
#[derive(Debug, Clone)]
pub struct RelayOptions {
pub(super) connection_mode: ConnectionMode,
pub(super) flags: RelayServiceFlags,
// TODO: what to do with this atomic?
pow: Arc<AtomicU8>,
reconnect: Arc<AtomicBool>,
retry_sec: Arc<AtomicU64>,
adjust_retry_sec: Arc<AtomicBool>,
pub(super) reconnect: bool,
pub(super) retry_sec: u64,
pub(super) adjust_retry_sec: bool,
pub(super) limits: RelayLimits,
pub(super) max_avg_latency: Option<Duration>,
pub(super) filtering_mode: RelayFilteringMode,
Expand All @@ -36,9 +37,9 @@ impl Default for RelayOptions {
connection_mode: ConnectionMode::default(),
flags: RelayServiceFlags::default(),
pow: Arc::new(AtomicU8::new(0)),
reconnect: Arc::new(AtomicBool::new(true)),
retry_sec: Arc::new(AtomicU64::new(DEFAULT_RETRY_SEC)),
adjust_retry_sec: Arc::new(AtomicBool::new(true)),
reconnect: true,
retry_sec: DEFAULT_RETRY_SEC,
adjust_retry_sec: true,
limits: RelayLimits::default(),
max_avg_latency: None,
filtering_mode: RelayFilteringMode::default(),
Expand All @@ -47,7 +48,8 @@ impl Default for RelayOptions {
}

impl RelayOptions {
/// New [`RelayOptions`]
/// New default options
#[inline]
pub fn new() -> Self {
Self::default()
}
Expand Down Expand Up @@ -111,67 +113,38 @@ impl RelayOptions {
}

/// Enable/disable auto reconnection (default: true)
pub fn reconnect(self, reconnect: bool) -> Self {
Self {
reconnect: Arc::new(AtomicBool::new(reconnect)),
..self
}
}

pub(crate) fn get_reconnect(&self) -> bool {
self.reconnect.load(Ordering::SeqCst)
pub fn reconnect(mut self, reconnect: bool) -> Self {
self.reconnect = reconnect;
self
}

/// Set `reconnect` option
pub fn update_reconnect(&self, reconnect: bool) {
self.reconnect.store(reconnect, Ordering::SeqCst);
}
#[deprecated(since = "0.36.0")]
pub fn update_reconnect(&self, _reconnect: bool) {}

/// Retry connection time (default: 10 sec)
///
/// Are allowed values `>=` 5 secs
pub fn retry_sec(self, retry_sec: u64) -> Self {
let retry_sec = if retry_sec >= MIN_RETRY_SEC {
retry_sec
} else {
DEFAULT_RETRY_SEC
pub fn retry_sec(mut self, retry_sec: u64) -> Self {
if retry_sec >= MIN_RETRY_SEC {
self.retry_sec = retry_sec;
};
Self {
retry_sec: Arc::new(AtomicU64::new(retry_sec)),
..self
}
}

pub(crate) fn get_retry_sec(&self) -> u64 {
self.retry_sec.load(Ordering::SeqCst)
self
}

/// Set retry_sec option
pub fn update_retry_sec(&self, retry_sec: u64) {
if retry_sec >= MIN_RETRY_SEC {
self.retry_sec.store(retry_sec, Ordering::SeqCst);
} else {
tracing::warn!("Relay options: retry_sec it's less then the minimum value allowed (min: {MIN_RETRY_SEC} secs)");
}
}
#[deprecated(since = "0.36.0")]
pub fn update_retry_sec(&self, _retry_sec: u64) {}

/// Automatically adjust retry seconds based on success/attempts (default: true)
pub fn adjust_retry_sec(self, adjust_retry_sec: bool) -> Self {
Self {
adjust_retry_sec: Arc::new(AtomicBool::new(adjust_retry_sec)),
..self
}
}

pub(crate) fn get_adjust_retry_sec(&self) -> bool {
self.adjust_retry_sec.load(Ordering::SeqCst)
pub fn adjust_retry_sec(mut self, adjust_retry_sec: bool) -> Self {
self.adjust_retry_sec = adjust_retry_sec;
self
}

/// Set adjust_retry_sec option
pub fn update_adjust_retry_sec(&self, adjust_retry_sec: bool) {
self.adjust_retry_sec
.store(adjust_retry_sec, Ordering::SeqCst);
}
#[deprecated(since = "0.36.0")]
pub fn update_adjust_retry_sec(&self, _adjust_retry_sec: bool) {}

/// Set custom limits
pub fn limits(mut self, limits: RelayLimits) -> Self {
Expand Down

0 comments on commit dec5b46

Please sign in to comment.