From 6d5063d4410a2280484b1b7cf97cb594c562e76c Mon Sep 17 00:00:00 2001 From: Avi Date: Mon, 24 Aug 2026 22:58:27 -0500 Subject: [PATCH] refactor: share relay pool bootstrap --- src/feed.rs | 22 ++++------------------ src/profiles.rs | 1 + src/publish.rs | 19 +++++++------------ src/relays.rs | 24 ++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/feed.rs b/src/feed.rs index dd1fcb6..71c866f 100644 --- a/src/feed.rs +++ b/src/feed.rs @@ -98,15 +98,8 @@ async fn contact_pubkeys(settings: &Settings, owner_hex: &str) -> Result, @@ -210,7 +206,6 @@ pub(crate) async fn send_to_all_relays( noun: &str, ) -> (Vec, Vec) { let noun = noun.to_string(); - client.connect().await; // No explicit wait for connections here: `send_event` waits for each relay // to become writable itself, and the per-send timeout below already bounds // the whole publish. Waiting for *all* relays first would burn the full diff --git a/src/relays.rs b/src/relays.rs index ae0904c..066c416 100644 --- a/src/relays.rs +++ b/src/relays.rs @@ -69,6 +69,30 @@ pub fn enabled_urls(settings: &Settings) -> Vec { .collect() } +/// Build a client pool over `relay_urls`, adding each relay and optionally +/// waiting (up to `wait`) for connections before returning. +/// +/// Fails on the first relay that cannot be added. Callers that should tolerate +/// an unreachable relay instead of aborting add their relays themselves. +pub(crate) async fn open_pool( + keys: Keys, + relay_urls: &[String], + wait: Option, +) -> Result { + let client = Client::new(keys); + for url in relay_urls { + client + .add_relay(url.as_str()) + .await + .map_err(|e| AppError::network(format!("Could not add relay {url}: {e}")))?; + } + client.connect().await; + if let Some(timeout) = wait { + client.wait_for_connection(timeout).await; + } + Ok(client) +} + /// Result of testing a relay connection. #[derive(Debug, Clone, Serialize)] pub struct RelayTestResult {