Skip to content

Commit

Permalink
sdk: replace wait_for_connection option with connection_timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Jan 15, 2024
1 parent 247c8ec commit 55b4174
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 42 deletions.
15 changes: 9 additions & 6 deletions bindings/nostr-sdk-ffi/src/client/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ impl Options {
}
}

pub fn wait_for_connection(self: Arc<Self>, wait: bool) -> Arc<Self> {
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.wait_for_connection(wait);
Arc::new(builder)
}

pub fn wait_for_send(self: Arc<Self>, wait: bool) -> Arc<Self> {
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.wait_for_send(wait);
Expand Down Expand Up @@ -79,6 +73,15 @@ impl Options {
Arc::new(builder)
}

/// Connection timeout (default: None)
///
/// If set to `None`, the client will try to connect to the relays without waiting.
pub fn connection_timeout(self: Arc<Self>, timeout: Option<Duration>) -> Arc<Self> {
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.connection_timeout(timeout);
Arc::new(builder)
}

pub fn send_timeout(self: Arc<Self>, send_timeout: Option<Duration>) -> Arc<Self> {
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.send_timeout(send_timeout);
Expand Down
4 changes: 2 additions & 2 deletions bindings/nostr-sdk-ffi/src/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ impl Relay {
self.inner.queue() as u64
}

pub fn connect(&self, wait_for_connection: bool) {
block_on(self.inner.connect(wait_for_connection))
pub fn connect(&self, connection_timeout: Option<Duration>) {
block_on(self.inner.connect(connection_timeout))
}

pub fn stop(&self) -> Result<()> {
Expand Down
15 changes: 10 additions & 5 deletions bindings/nostr-sdk-js/src/client/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ impl JsOptions {
}
}

#[wasm_bindgen(js_name = waitForConnection)]
pub fn wait_for_connection(self, wait: bool) -> Self {
self.inner.wait_for_connection(wait).into()
}

#[wasm_bindgen(js_name = waitForSend)]
pub fn wait_for_send(self, wait: bool) -> Self {
self.inner.wait_for_send(wait).into()
Expand Down Expand Up @@ -72,6 +67,16 @@ impl JsOptions {
self.inner.timeout(**timeout).into()
}

/// Connection timeout (default: None)
///
/// If set to `None`, the client will try to connect to the relays without waiting.
#[wasm_bindgen(js_name = connectionTimeout)]
pub fn connection_timeout(self, connection_timeout: Option<JsDuration>) -> Self {
self.inner
.connection_timeout(connection_timeout.map(|d| *d))
.into()
}

#[wasm_bindgen(js_name = sendTimeout)]
pub fn send_timeout(self, send_timeout: Option<JsDuration>) -> Self {
self.inner.send_timeout(send_timeout.map(|d| *d)).into()
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr-sdk/examples/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ async fn main() -> Result<()> {
let secret_key = SecretKey::from_bech32(BECH32_SK)?;
let keys = Keys::new(secret_key);
let opts = Options::new()
.wait_for_connection(true)
.skip_disconnected_relays(true)
.connection_timeout(Some(Duration::from_secs(10)))
.send_timeout(Some(Duration::from_secs(5)));
let client = Client::with_opts(&keys, opts);

Expand Down
4 changes: 2 additions & 2 deletions crates/nostr-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ impl Client {
{
let relay: Relay = self.relay(url).await?;
self.pool
.connect_relay(&relay, self.opts.wait_for_connection)
.connect_relay(&relay, self.opts.connection_timeout)
.await;
Ok(())
}
Expand Down Expand Up @@ -469,7 +469,7 @@ impl Client {
/// # }
/// ```
pub async fn connect(&self) {
self.pool.connect(self.opts.wait_for_connection).await;
self.pool.connect(self.opts.connection_timeout).await;
}

/// Disconnect from all relays
Expand Down
24 changes: 16 additions & 8 deletions crates/nostr-sdk/src/client/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ pub(crate) const DEFAULT_SEND_TIMEOUT: Duration = Duration::from_secs(20);
/// Options
#[derive(Debug, Clone)]
pub struct Options {
/// Wait for connection (default: false)
pub wait_for_connection: bool,
/// Wait for the msg to be sent (default: true)
wait_for_send: Arc<AtomicBool>,
/// Wait for the subscription msg to be sent (default: false)
Expand All @@ -35,6 +33,10 @@ pub struct Options {
///
/// Used in `get_events_of`, `req_events_of` and similar as default timeout.
pub timeout: Duration,
/// Relay connection timeout (default: None)
///
/// If set to `None`, the client will try to connect to relay without waiting.
pub connection_timeout: Option<Duration>,
/// Send timeout (default: 20 secs)
pub send_timeout: Option<Duration>,
/// NIP46 timeout (default: 180 secs)
Expand All @@ -52,13 +54,13 @@ pub struct Options {
impl Default for Options {
fn default() -> Self {
Self {
wait_for_connection: false,
wait_for_send: Arc::new(AtomicBool::new(true)),
wait_for_subscription: Arc::new(AtomicBool::new(false)),
difficulty: Arc::new(AtomicU8::new(0)),
req_filters_chunk_size: Arc::new(AtomicU8::new(10)),
skip_disconnected_relays: Arc::new(AtomicBool::new(true)),
timeout: Duration::from_secs(60),
connection_timeout: None,
send_timeout: Some(DEFAULT_SEND_TIMEOUT),
#[cfg(feature = "nip46")]
nip46_timeout: Some(Duration::from_secs(180)),
Expand All @@ -77,11 +79,9 @@ impl Options {
}

/// If set to `true`, `Client` wait that `Relay` try at least one time to enstablish a connection before continue.
pub fn wait_for_connection(self, wait: bool) -> Self {
Self {
wait_for_connection: wait,
..self
}
#[deprecated(since = "0.27.0", note = "Use `connection_timeout` instead")]
pub fn wait_for_connection(self, _wait: bool) -> Self {
self
}

/// If set to `true`, `Client` wait that a message is sent before continue.
Expand Down Expand Up @@ -157,6 +157,14 @@ impl Options {
Self { timeout, ..self }
}

/// Connection timeout (default: None)
///
/// If set to `None`, the client will try to connect to the relays without waiting.
pub fn connection_timeout(mut self, timeout: Option<Duration>) -> Self {
self.connection_timeout = timeout;
self
}

/// Set default send timeout
pub fn send_timeout(self, timeout: Option<Duration>) -> Self {
Self {
Expand Down
31 changes: 20 additions & 11 deletions crates/nostr-sdk/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,21 +436,21 @@ impl Relay {
}

/// Connect to relay and keep alive connection
pub async fn connect(&self, wait_for_connection: bool) {
pub async fn connect(&self, connection_timeout: Option<Duration>) {
self.schedule_for_stop(false);
self.schedule_for_termination(false);

if let RelayStatus::Initialized | RelayStatus::Stopped | RelayStatus::Terminated =
self.status().await
{
if self.opts.get_reconnect() {
if wait_for_connection {
self.try_connect().await
if connection_timeout.is_some() {
self.try_connect(connection_timeout).await
}

tracing::debug!("Auto connect loop started for {}", self.url);

if !wait_for_connection {
if connection_timeout.is_none() {
self.set_status(RelayStatus::Pending).await;
}

Expand Down Expand Up @@ -491,7 +491,9 @@ impl Relay {
match relay.status().await {
RelayStatus::Initialized
| RelayStatus::Pending
| RelayStatus::Disconnected => relay.try_connect().await,
| RelayStatus::Disconnected => {
relay.try_connect(connection_timeout).await
}
RelayStatus::Stopped | RelayStatus::Terminated => {
tracing::debug!("Auto connect loop terminated for {}", relay.url);
break;
Expand All @@ -518,16 +520,16 @@ impl Relay {
thread::sleep(Duration::from_secs(retry_sec)).await;
}
});
} else if wait_for_connection {
self.try_connect().await
} else if connection_timeout.is_some() {
self.try_connect(connection_timeout).await
} else {
let relay = self.clone();
thread::spawn(async move { relay.try_connect().await });
thread::spawn(async move { relay.try_connect(connection_timeout).await });
}
}
}

async fn try_connect(&self) {
async fn try_connect(&self, connection_timeout: Option<Duration>) {
self.stats.new_attempt();

let url: String = self.url.to_string();
Expand Down Expand Up @@ -556,10 +558,17 @@ impl Relay {
});
}

let timeout: Option<Duration> = if self.stats.attempts() > 1 {
// Many attempts, use the default timeout
Some(Duration::from_secs(60))
} else {
// First attempt, use external timeout
connection_timeout
};
#[cfg(not(target_arch = "wasm32"))]
let connection = net::native::connect(&self.url, self.proxy(), None).await;
let connection = net::native::connect(&self.url, self.proxy(), timeout).await;
#[cfg(target_arch = "wasm32")]
let connection = net::wasm::connect(&self.url).await;
let connection = net::wasm::connect(&self.url, timeout).await;

// Connect
match connection {
Expand Down
14 changes: 7 additions & 7 deletions crates/nostr-sdk/src/relay/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,16 +854,16 @@ impl RelayPool {
}

/// Connect to all added relays and keep connection alive
pub async fn connect(&self, wait_for_connection: bool) {
pub async fn connect(&self, connection_timeout: Option<Duration>) {
let relays: HashMap<Url, Relay> = self.relays().await;

if wait_for_connection {
let mut handles = Vec::new();
if connection_timeout.is_some() {
let mut handles = Vec::with_capacity(relays.len());

for relay in relays.into_values() {
let pool = self.clone();
let handle = thread::spawn(async move {
pool.connect_relay(&relay, wait_for_connection).await;
pool.connect_relay(&relay, connection_timeout).await;
});
handles.push(handle);
}
Expand All @@ -873,7 +873,7 @@ impl RelayPool {
}
} else {
for relay in relays.values() {
self.connect_relay(relay, wait_for_connection).await;
self.connect_relay(relay, None).await;
}
}
}
Expand All @@ -890,12 +890,12 @@ impl RelayPool {
/// Connect to relay
///
/// Internal Subscription ID set to `InternalSubscriptionId::Pool`
pub async fn connect_relay(&self, relay: &Relay, wait_for_connection: bool) {
pub async fn connect_relay(&self, relay: &Relay, connection_timeout: Option<Duration>) {
let filters: Vec<Filter> = self.subscription_filters().await;
relay
.update_subscription_filters(InternalSubscriptionId::Pool, filters)
.await;
relay.connect(wait_for_connection).await;
relay.connect(connection_timeout).await;
}

/// Disconnect from relay
Expand Down

0 comments on commit 55b4174

Please sign in to comment.