Skip to content

Commit 4dd08af

Browse files
committed
Revert "pool: deprecate auto adjustment of retry seconds for relays"
This reverts commit c4270c0 Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 9d8d1ef commit 4dd08af

File tree

5 files changed

+63
-11
lines changed

5 files changed

+63
-11
lines changed

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
* database: update `NostrDatabase::event_by_id` fingerprint ([Yuki Kishimoto])
6060
* relay-builder: bump `tokio-tungstenite` to `v0.24` ([Yuki Kishimoto])
6161
* pool: bump `async-wsocket` to `v0.8` ([Yuki Kishimoto])
62-
* pool: deprecate auto adjustment of retry seconds for relays ([Yuki Kishimoto])
6362
* pool: avoid unnecessary `Url` and `Relay` clone in `RelayPool` methods ([Yuki Kishimoto])
6463
* pool: avoid `Relay` clone in `RelayPool::connect_relay` method ([Yuki Kishimoto])
6564
* pool: `RelayPool::send_event` and `RelayPool::batch_event` send only to relays with `WRITE` flag ([Yuki Kishimoto])

bindings/nostr-sdk-ffi/src/relay/options.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ impl RelayOptions {
152152
self.inner.update_retry_sec(retry_sec);
153153
}
154154

155+
/// Automatically adjust retry seconds based on success/attempts (default: true)
156+
pub fn adjust_retry_sec(self: Arc<Self>, adjust_retry_sec: bool) -> Self {
157+
let mut builder = unwrap_or_clone_arc(self);
158+
builder.inner = builder.inner.adjust_retry_sec(adjust_retry_sec);
159+
builder
160+
}
161+
162+
/// Set adjust_retry_sec option
163+
pub fn update_adjust_retry_sec(&self, adjust_retry_sec: bool) {
164+
self.inner.update_adjust_retry_sec(adjust_retry_sec);
165+
}
166+
155167
/// Set custom limits
156168
pub fn limits(self: Arc<Self>, limits: &RelayLimits) -> Self {
157169
let mut builder = unwrap_or_clone_arc(self);

bindings/nostr-sdk-js/src/relay/options.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ impl JsRelayOptions {
9494
self.inner.update_retry_sec(retry_sec);
9595
}
9696

97+
/// Automatically adjust retry seconds based on success/attempts (default: true)
98+
pub fn adjust_retry_sec(self, adjust_retry_sec: bool) -> Self {
99+
self.inner.adjust_retry_sec(adjust_retry_sec).into()
100+
}
101+
102+
/// Set adjust_retry_sec option
103+
pub fn update_adjust_retry_sec(&self, adjust_retry_sec: bool) {
104+
self.inner.update_adjust_retry_sec(adjust_retry_sec);
105+
}
106+
97107
/// Set custom limits
98108
pub fn limits(self, limits: &JsRelayLimits) -> Self {
99109
self.inner.limits(limits.deref().clone()).into()

crates/nostr-relay-pool/src/relay/internal.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
//! Internal Relay
66
7+
use std::cmp;
78
use std::collections::{HashMap, HashSet};
89
use std::sync::atomic::{AtomicBool, Ordering};
910
use std::sync::Arc;
@@ -19,8 +20,7 @@ use nostr::message::MessageHandleError;
1920
use nostr::nips::nip01::Coordinate;
2021
#[cfg(feature = "nip11")]
2122
use nostr::nips::nip11::RelayInformationDocument;
22-
#[cfg(not(target_arch = "wasm32"))]
23-
use nostr::secp256k1::rand;
23+
use nostr::secp256k1::rand::{self, Rng};
2424
use nostr::{
2525
ClientMessage, Event, EventId, Filter, JsonUtil, Kind, MissingPartialEvent, PartialEvent,
2626
RawRelayMessage, RelayMessage, SubscriptionId, Timestamp, Url,
@@ -34,8 +34,8 @@ use super::filtering::{CheckFiltering, RelayFiltering};
3434
use super::flags::AtomicRelayServiceFlags;
3535
use super::options::{
3636
FilterOptions, NegentropyOptions, RelayOptions, RelaySendOptions, SubscribeAutoCloseOptions,
37-
SubscribeOptions, NEGENTROPY_BATCH_SIZE_DOWN, NEGENTROPY_HIGH_WATER_UP,
38-
NEGENTROPY_LOW_WATER_UP,
37+
SubscribeOptions, MAX_ADJ_RETRY_SEC, MIN_RETRY_SEC, NEGENTROPY_BATCH_SIZE_DOWN,
38+
NEGENTROPY_HIGH_WATER_UP, NEGENTROPY_LOW_WATER_UP,
3939
};
4040
use super::stats::RelayConnectionStats;
4141
use super::{Error, Reconciliation, RelayNotification, RelayStatus};
@@ -513,7 +513,8 @@ impl InternalRelay {
513513
};
514514

515515
// Sleep
516-
let retry_sec: u64 = relay.opts.get_retry_sec();
516+
let retry_sec: u64 = relay.calculate_retry_sec();
517+
tracing::trace!("{} retry time set to {retry_sec} secs", relay.url);
517518
thread::sleep(Duration::from_secs(retry_sec)).await;
518519
}
519520
});
@@ -526,6 +527,25 @@ impl InternalRelay {
526527
}
527528
}
528529

530+
/// Depending on attempts and success, use default or incremental retry time
531+
fn calculate_retry_sec(&self) -> u64 {
532+
if self.opts.get_adjust_retry_sec() {
533+
// diff = attempts - success
534+
let diff: u64 = self.stats.attempts().saturating_sub(self.stats.success()) as u64;
535+
536+
// Use incremental retry time if diff >= 3
537+
if diff >= 3 {
538+
let retry_interval: i64 =
539+
cmp::min(MIN_RETRY_SEC * (1 + diff), MAX_ADJ_RETRY_SEC) as i64;
540+
let jitter: i64 = rand::thread_rng().gen_range(-1..=1);
541+
return retry_interval.saturating_add(jitter) as u64;
542+
}
543+
}
544+
545+
// Use default retry time
546+
self.opts.get_retry_sec()
547+
}
548+
529549
#[cfg(feature = "nip11")]
530550
fn request_nip11_document(&self) {
531551
let (allowed, proxy) = match self.opts.connection_mode {

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::RelayLimits;
2121
pub const DEFAULT_SEND_TIMEOUT: Duration = Duration::from_secs(20);
2222
pub(super) const DEFAULT_RETRY_SEC: u64 = 10;
2323
pub(super) const MIN_RETRY_SEC: u64 = 5;
24+
pub(super) const MAX_ADJ_RETRY_SEC: u64 = 60;
2425
pub(super) const NEGENTROPY_HIGH_WATER_UP: usize = 100;
2526
pub(super) const NEGENTROPY_LOW_WATER_UP: usize = 50;
2627
pub(super) const NEGENTROPY_BATCH_SIZE_DOWN: usize = 50;
@@ -33,6 +34,7 @@ pub struct RelayOptions {
3334
pow: Arc<AtomicU8>,
3435
reconnect: Arc<AtomicBool>,
3536
retry_sec: Arc<AtomicU64>,
37+
adjust_retry_sec: Arc<AtomicBool>,
3638
pub(super) limits: RelayLimits,
3739
pub(super) max_avg_latency: Option<Duration>,
3840
pub(super) filtering_mode: RelayFilteringMode,
@@ -46,6 +48,7 @@ impl Default for RelayOptions {
4648
pow: Arc::new(AtomicU8::new(0)),
4749
reconnect: Arc::new(AtomicBool::new(true)),
4850
retry_sec: Arc::new(AtomicU64::new(DEFAULT_RETRY_SEC)),
51+
adjust_retry_sec: Arc::new(AtomicBool::new(true)),
4952
limits: RelayLimits::default(),
5053
max_avg_latency: None,
5154
filtering_mode: RelayFilteringMode::default(),
@@ -174,14 +177,22 @@ impl RelayOptions {
174177
}
175178

176179
/// Automatically adjust retry seconds based on success/attempts (default: true)
177-
#[deprecated(since = "0.35.0")]
178-
pub fn adjust_retry_sec(self, _adjust_retry_sec: bool) -> Self {
179-
self
180+
pub fn adjust_retry_sec(self, adjust_retry_sec: bool) -> Self {
181+
Self {
182+
adjust_retry_sec: Arc::new(AtomicBool::new(adjust_retry_sec)),
183+
..self
184+
}
185+
}
186+
187+
pub(crate) fn get_adjust_retry_sec(&self) -> bool {
188+
self.adjust_retry_sec.load(Ordering::SeqCst)
180189
}
181190

182191
/// Set adjust_retry_sec option
183-
#[deprecated(since = "0.35.0")]
184-
pub fn update_adjust_retry_sec(&self, _adjust_retry_sec: bool) {}
192+
pub fn update_adjust_retry_sec(&self, adjust_retry_sec: bool) {
193+
self.adjust_retry_sec
194+
.store(adjust_retry_sec, Ordering::SeqCst);
195+
}
185196

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

0 commit comments

Comments
 (0)