Skip to content

Commit c2b6f1f

Browse files
committed
doc: improve connection docs
Closes #799 Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 68a4026 commit c2b6f1f

File tree

7 files changed

+137
-18
lines changed

7 files changed

+137
-18
lines changed

bindings/nostr-sdk-ffi/src/client/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ impl Client {
177177
}
178178

179179
/// Connect to all added relays
180+
///
181+
/// Attempts to initiate a connection for every relay currently in
182+
/// [`RelayStatus::Initialized`] or [`RelayStatus::Terminated`].
183+
/// A background connection task is spawned for each such relay, which then tries
184+
/// to establish the connection.
185+
/// Any relay not in one of these two statuses is skipped.
186+
///
187+
/// For further details, see the documentation of [`Relay::connect`].
180188
pub async fn connect(&self) {
181189
self.inner.connect().await
182190
}
@@ -191,10 +199,14 @@ impl Client {
191199

192200
/// Try to establish a connection with the relays.
193201
///
194-
/// Attempts to establish a connection without spawning the connection task if it fails.
202+
/// Attempts to establish a connection for every relay currently in
203+
/// [`RelayStatus::Initialized`] or [`RelayStatus::Terminated`]
204+
/// without spawning the connection task if it fails.
195205
/// This means that if the connection fails, no automatic retries are scheduled.
196206
/// Use [`Client::connect`] if you want to immediately spawn a connection task,
197207
/// regardless of whether the initial connection succeeds.
208+
///
209+
/// For further details, see the documentation of [`Relay::try_connect`].
198210
pub async fn try_connect(&self, timeout: Duration) -> Output {
199211
self.inner.try_connect(timeout).await.into()
200212
}

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,42 @@ impl Relay {
162162

163163
// TODO: add notifications
164164

165-
/// Connect to relay
165+
/// Connect to the relay
166+
///
167+
/// # Overview
168+
///
169+
/// If the relay’s status is not [`RelayStatus::Initialized`] or [`RelayStatus::Terminated`],
170+
/// this method returns immediately without doing anything.
171+
/// Otherwise, the connection task will be spawned, which will attempt to connect to relay.
166172
///
167173
/// This method returns immediately and doesn't provide any information on if the connection was successful or not.
174+
///
175+
/// # Automatic reconnection
176+
///
177+
/// By default, in case of disconnection, the connection task will automatically attempt to reconnect.
178+
/// This behavior can be disabled by changing [`RelayOptions::reconnect`] option.
168179
pub fn connect(&self) {
169180
self.inner.connect()
170181
}
171182

172-
/// Try to connect to relay
183+
/// Try to establish a connection with the relay.
184+
///
185+
/// # Overview
186+
///
187+
/// If the relay’s status is not [`RelayStatus::Initialized`] or [`RelayStatus::Terminated`],
188+
/// this method returns immediately without doing anything.
189+
/// Otherwise, attempts to establish a connection without spawning the connection task if it fails.
190+
/// This means that if the connection fails, no automatic retries are scheduled.
191+
/// Use [`Relay::connect`] if you want to immediately spawn a connection task,
192+
/// regardless of whether the initial connection succeeds.
193+
///
194+
/// Returns an error if the connection fails.
195+
///
196+
/// # Automatic reconnection
173197
///
174-
/// This method returns an error if the connection fails.
175-
/// If the connection fails,
176-
/// a task will continue to retry in the background (unless configured differently in `RelayOptions`.
198+
/// By default, in case of disconnection (after a first successful connection),
199+
/// the connection task will automatically attempt to reconnect.
200+
/// This behavior can be disabled by changing [`RelayOptions::reconnect`] option.
177201
pub async fn try_connect(&self, timeout: Duration) -> Result<()> {
178202
Ok(self.inner.try_connect(timeout).await?)
179203
}

bindings/nostr-sdk-js/src/client/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ impl JsClient {
205205
}
206206

207207
/// Connect to all added relays
208+
///
209+
/// Attempts to initiate a connection for every relay currently in
210+
/// [`RelayStatus::Initialized`] or [`RelayStatus::Terminated`].
211+
/// A background connection task is spawned for each such relay, which then tries
212+
/// to establish the connection.
213+
/// Any relay not in one of these two statuses is skipped.
214+
///
215+
/// For further details, see the documentation of [`Relay::connect`].
208216
pub async fn connect(&self) {
209217
self.inner.connect().await
210218
}
@@ -220,10 +228,14 @@ impl JsClient {
220228

221229
/// Try to establish a connection with the relays.
222230
///
223-
/// Attempts to establish a connection without spawning the connection task if it fails.
231+
/// Attempts to establish a connection for every relay currently in
232+
/// [`RelayStatus::Initialized`] or [`RelayStatus::Terminated`]
233+
/// without spawning the connection task if it fails.
224234
/// This means that if the connection fails, no automatic retries are scheduled.
225235
/// Use [`Client::connect`] if you want to immediately spawn a connection task,
226236
/// regardless of whether the initial connection succeeds.
237+
///
238+
/// For further details, see the documentation of [`Relay::try_connect`].
227239
#[wasm_bindgen(js_name = tryConnect)]
228240
pub async fn try_connect(&self, timeout: &JsDuration) -> JsOutput {
229241
self.inner.try_connect(**timeout).await.into()

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,42 @@ impl JsRelay {
141141
self.inner.queue() as u64
142142
}
143143

144-
/// Connect to relay
144+
/// Connect to the relay
145+
///
146+
/// # Overview
147+
///
148+
/// If the relay’s status is not [`RelayStatus::Initialized`] or [`RelayStatus::Terminated`],
149+
/// this method returns immediately without doing anything.
150+
/// Otherwise, the connection task will be spawned, which will attempt to connect to relay.
145151
///
146152
/// This method returns immediately and doesn't provide any information on if the connection was successful or not.
153+
///
154+
/// # Automatic reconnection
155+
///
156+
/// By default, in case of disconnection, the connection task will automatically attempt to reconnect.
157+
/// This behavior can be disabled by changing [`RelayOptions::reconnect`] option.
147158
pub fn connect(&self) {
148159
self.inner.connect()
149160
}
150161

151-
/// Try to connect to relay
162+
/// Try to establish a connection with the relay.
163+
///
164+
/// # Overview
165+
///
166+
/// If the relay’s status is not [`RelayStatus::Initialized`] or [`RelayStatus::Terminated`],
167+
/// this method returns immediately without doing anything.
168+
/// Otherwise, attempts to establish a connection without spawning the connection task if it fails.
169+
/// This means that if the connection fails, no automatic retries are scheduled.
170+
/// Use [`Relay::connect`] if you want to immediately spawn a connection task,
171+
/// regardless of whether the initial connection succeeds.
172+
///
173+
/// Returns an error if the connection fails.
174+
///
175+
/// # Automatic reconnection
152176
///
153-
/// This method returns an error if the connection fails.
154-
/// If the connection fails,
155-
/// a task will continue to retry in the background (unless configured differently in `RelayOptions`.
177+
/// By default, in case of disconnection (after a first successful connection),
178+
/// the connection task will automatically attempt to reconnect.
179+
/// This behavior can be disabled by changing [`RelayOptions::reconnect`] option.
156180
#[wasm_bindgen(js_name = tryConnect)]
157181
pub async fn try_connect(&self, timeout: &JsDuration) -> Result<()> {
158182
self.inner.try_connect(**timeout).await.map_err(into_err)

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,17 @@ impl RelayPool {
320320
}
321321

322322
/// Connect to all added relays
323+
///
324+
/// Attempts to initiate a connection for every relay currently in
325+
/// [`RelayStatus::Initialized`] or [`RelayStatus::Terminated`].
326+
/// A background connection task is spawned for each such relay, which then tries
327+
/// to establish the connection.
328+
/// Any relay not in one of these two statuses is skipped.
329+
///
330+
/// For further details, see the documentation of [`Relay::connect`].
331+
///
332+
/// [`RelayStatus::Initialized`]: crate::relay::RelayStatus::Initialized
333+
/// [`RelayStatus::Terminated`]: crate::relay::RelayStatus::Terminated
323334
pub async fn connect(&self) {
324335
// Lock with read shared access
325336
let relays = self.inner.atomic.relays.read().await;
@@ -350,7 +361,9 @@ impl RelayPool {
350361

351362
/// Try to establish a connection with the relays.
352363
///
353-
/// Attempts to establish a connection without spawning the connection task if it fails.
364+
/// Attempts to establish a connection for every relay currently in
365+
/// [`RelayStatus::Initialized`] or [`RelayStatus::Terminated`]
366+
/// without spawning the connection task if it fails.
354367
/// This means that if the connection fails, no automatic retries are scheduled.
355368
/// Use [`RelayPool::connect`] if you want to immediately spawn a connection task,
356369
/// regardless of whether the initial connection succeeds.

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,22 @@ impl Relay {
231231
self.inner.internal_notification_sender.subscribe()
232232
}
233233

234-
/// Connect to relay
234+
/// Connect to the relay
235+
///
236+
/// # Overview
237+
///
238+
/// If the relay’s status is not [`RelayStatus::Initialized`] or [`RelayStatus::Terminated`],
239+
/// this method returns immediately without doing anything.
240+
/// Otherwise, the connection task will be spawned, which will attempt to connect to relay.
235241
///
236242
/// This method returns immediately and doesn't provide any information on if the connection was successful or not.
243+
///
244+
/// # Automatic reconnection
245+
///
246+
/// By default, in case of disconnection, the connection task will automatically attempt to reconnect.
247+
/// This behavior can be disabled by changing [`RelayOptions::reconnect`] option.
237248
pub fn connect(&self) {
249+
// Immediately return if can't connect
238250
if !self.status().can_connect() {
239251
return;
240252
}
@@ -279,12 +291,22 @@ impl Relay {
279291

280292
/// Try to establish a connection with the relay.
281293
///
282-
/// Attempts to establish a connection without spawning the connection task if it fails.
294+
/// # Overview
295+
///
296+
/// If the relay’s status is not [`RelayStatus::Initialized`] or [`RelayStatus::Terminated`],
297+
/// this method returns immediately without doing anything.
298+
/// Otherwise, attempts to establish a connection without spawning the connection task if it fails.
283299
/// This means that if the connection fails, no automatic retries are scheduled.
284300
/// Use [`Relay::connect`] if you want to immediately spawn a connection task,
285301
/// regardless of whether the initial connection succeeds.
286302
///
287303
/// Returns an error if the connection fails.
304+
///
305+
/// # Automatic reconnection
306+
///
307+
/// By default, in case of disconnection (after a first successful connection),
308+
/// the connection task will automatically attempt to reconnect.
309+
/// This behavior can be disabled by changing [`RelayOptions::reconnect`] option.
288310
pub async fn try_connect(&self, timeout: Duration) -> Result<(), Error> {
289311
// Check if relay can't connect
290312
if !self.status().can_connect() {
@@ -304,7 +326,9 @@ impl Relay {
304326
Ok(())
305327
}
306328

307-
/// Disconnect from relay and set status to 'Terminated'
329+
/// Disconnect from relay and set status to [`RelayStatus::Terminated`].
330+
///
331+
/// Returns immediately if the status is already [`RelayStatus::Terminated`].
308332
#[inline]
309333
pub fn disconnect(&self) {
310334
self.inner.disconnect()

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,14 @@ impl Client {
437437
}
438438

439439
/// Connect to all added relays
440+
///
441+
/// Attempts to initiate a connection for every relay currently in
442+
/// [`RelayStatus::Initialized`] or [`RelayStatus::Terminated`].
443+
/// A background connection task is spawned for each such relay, which then tries
444+
/// to establish the connection.
445+
/// Any relay not in one of these two statuses is skipped.
446+
///
447+
/// For further details, see the documentation of [`Relay::connect`].
440448
#[inline]
441449
pub async fn connect(&self) {
442450
self.pool.connect().await;
@@ -453,12 +461,14 @@ impl Client {
453461

454462
/// Try to establish a connection with the relays.
455463
///
456-
/// Attempts to establish a connection without spawning the connection task if it fails.
464+
/// Attempts to establish a connection for every relay currently in
465+
/// [`RelayStatus::Initialized`] or [`RelayStatus::Terminated`]
466+
/// without spawning the connection task if it fails.
457467
/// This means that if the connection fails, no automatic retries are scheduled.
458468
/// Use [`Client::connect`] if you want to immediately spawn a connection task,
459469
/// regardless of whether the initial connection succeeds.
460470
///
461-
/// For further details, see the documentation of [`RelayPool::try_connect`].
471+
/// For further details, see the documentation of [`Relay::try_connect`].
462472
#[inline]
463473
pub async fn try_connect(&self, timeout: Duration) -> Output<()> {
464474
self.pool.try_connect(timeout).await

0 commit comments

Comments
 (0)