perf: Connect to relays in a separate task

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb
2025-05-08 14:25:09 +00:00
parent bd086538fd
commit 70ee4a59b2

View File

@@ -21,6 +21,7 @@ pub mod utils;
use std::time::Duration; use std::time::Duration;
use futures::future;
use nostr::{ use nostr::{
event::{Event, EventId, Kind, UnsignedEvent}, event::{Event, EventId, Kind, UnsignedEvent},
filter::Filter, filter::Filter,
@@ -71,15 +72,21 @@ impl NostrClient {
/// Add relays and connect to them /// Add relays and connect to them
pub async fn add_relays(&self, relays: &[RelayUrl]) { pub async fn add_relays(&self, relays: &[RelayUrl]) {
let mut tasks = Vec::new();
for relay in relays { for relay in relays {
self.client let relay = relay.clone();
.add_relay(relay) let client = self.client.clone();
tasks.push(tokio::spawn(async move {
client
.add_relay(&relay)
.await .await
.expect("It's a valid relay url"); .expect("It's a valid relay url");
if let Err(err) = self.client.try_connect_relay(relay, CLIENT_TIMEOUT).await { if let Err(err) = client.try_connect_relay(&relay, CLIENT_TIMEOUT).await {
tracing::error!("Failed to connect to relay '{relay}': {err}"); tracing::error!("Failed to connect to relay '{relay}': {err}");
} }
}));
} }
future::join_all(tasks).await;
} }
/// Broadcasts an unsigned event to given relays, optionally broadcast the /// Broadcasts an unsigned event to given relays, optionally broadcast the