upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2025-12-11 13:00:34 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2025-12-11 13:00:34 +0000
commita56e12e0aed87c68fd49b453b5c3dc0bfbf81285 (patch)
treec0b6d8e3177b744bba7b0005f56df18d6e9d5620
parent82c5783a4d40c4273cb12317ec9bf88a2e281a04 (diff)
docs(sync): document why RelayConnection uses Client instead of Relay directly
nostr-sdk 0.44's Relay::new() is pub(crate), making it impossible to construct a Relay directly from outside the crate. Relays can only be created through Client::add_relay() or RelayPool::add_relay(). This commit: - Adds 'Why Client instead of Relay directly?' section to struct docs - Updates run_event_loop() docs to explain the API constraint - Removes outdated 'Future Refactoring' suggestion (not feasible)
-rw-r--r--src/sync/relay_connection.rs19
1 files changed, 14 insertions, 5 deletions
diff --git a/src/sync/relay_connection.rs b/src/sync/relay_connection.rs
index 91825f2..32071e5 100644
--- a/src/sync/relay_connection.rs
+++ b/src/sync/relay_connection.rs
@@ -32,6 +32,17 @@ pub enum RelayEvent {
32/// - Layer 1 subscription (announcements) 32/// - Layer 1 subscription (announcements)
33/// - Additional filter subscriptions (Layers 2 & 3) 33/// - Additional filter subscriptions (Layers 2 & 3)
34/// - Event notification loop 34/// - Event notification loop
35///
36/// # Why Client instead of Relay directly?
37///
38/// While it would be cleaner to hold a `Relay` directly (since we only manage
39/// one relay per connection), the nostr-sdk API makes `Relay::new()` private
40/// (`pub(crate)`). Relays can only be created through `Client::add_relay()` or
41/// `RelayPool::add_relay()`. This is an intentional design in nostr-sdk to
42/// ensure proper lifecycle management.
43///
44/// The Client adds minimal overhead since we configure it with a single relay,
45/// and we retrieve the `Relay` reference for notification handling.
35#[derive(Clone)] 46#[derive(Clone)]
36pub struct RelayConnection { 47pub struct RelayConnection {
37 /// The relay URL this connection is for 48 /// The relay URL this connection is for
@@ -135,10 +146,8 @@ impl RelayConnection {
135 /// `RelayNotification::RelayStatus` events are not forwarded to the pool-level channel. 146 /// `RelayNotification::RelayStatus` events are not forwarded to the pool-level channel.
136 /// This enables immediate, event-driven disconnect detection without polling. 147 /// This enables immediate, event-driven disconnect detection without polling.
137 /// 148 ///
138 /// # Future Refactoring 149 /// We must retrieve the Relay from the Client because nostr-sdk does not expose
139 /// Consider refactoring `RelayConnection` to hold a `Relay` directly instead of 150 /// `Relay::new()` publicly - relays can only be created through Client or RelayPool.
140 /// wrapping a `Client`. This would simplify the architecture since we only manage
141 /// one relay per connection.
142 pub async fn run_event_loop(self, event_sender: mpsc::Sender<RelayEvent>) { 151 pub async fn run_event_loop(self, event_sender: mpsc::Sender<RelayEvent>) {
143 let url = self.url.clone(); 152 let url = self.url.clone();
144 153
@@ -324,4 +333,4 @@ impl RelayConnection {
324 self.client.unsubscribe_all().await; 333 self.client.unsubscribe_all().await;
325 tracing::debug!(relay = %self.url, "Unsubscribed from all subscriptions"); 334 tracing::debug!(relay = %self.url, "Unsubscribed from all subscriptions");
326 } 335 }
327} \ No newline at end of file 336}