diff options
Diffstat (limited to 'src/sync/relay_connection.rs')
| -rw-r--r-- | src/sync/relay_connection.rs | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/src/sync/relay_connection.rs b/src/sync/relay_connection.rs index d69e112..4f26779 100644 --- a/src/sync/relay_connection.rs +++ b/src/sync/relay_connection.rs | |||
| @@ -74,23 +74,24 @@ impl RelayConnection { | |||
| 74 | .await | 74 | .await |
| 75 | .map_err(|e| format!("Failed to add relay {}: {}", self.url, e))?; | 75 | .map_err(|e| format!("Failed to add relay {}: {}", self.url, e))?; |
| 76 | 76 | ||
| 77 | // Establish connection | 77 | // Establish connection using try_connect_relay for immediate failure detection |
| 78 | self.client.connect().await; | 78 | // |
| 79 | 79 | // Key difference from client.connect(): | |
| 80 | // Wait briefly for connection to establish and check status | 80 | // - try_connect_relay: Single attempt with timeout, returns Err on failure, |
| 81 | // nostr-sdk's connect() is async and may not immediately reflect failure | 81 | // does NOT spawn background retry task (we control retries via HealthTracker) |
| 82 | tokio::time::sleep(std::time::Duration::from_millis(500)).await; | 82 | // - connect(): Spawns background task, returns immediately, auto-retries forever |
| 83 | 83 | // | |
| 84 | // Check if relay is actually connected | 84 | // Using try_connect_relay gives us: |
| 85 | let relay = self.client.relay(&self.url).await | 85 | // 1. Immediate error return on connection failure |
| 86 | .map_err(|e| format!("Failed to get relay handle for {}: {}", self.url, e))?; | 86 | // 2. Configurable timeout (5 seconds default) |
| 87 | 87 | // 3. No conflicting retry logic (we use HealthTracker for backoff) | |
| 88 | if !relay.is_connected() { | 88 | // 4. Cleaner error messages for metrics recording |
| 89 | return Err(format!( | 89 | // |
| 90 | "Failed to connect to relay {}: connection not established after timeout", | 90 | // See: nostr-sdk-0.44 Client::try_connect_relay documentation |
| 91 | self.url | 91 | self.client |
| 92 | )); | 92 | .try_connect_relay(&self.url, std::time::Duration::from_secs(5)) |
| 93 | } | 93 | .await |
| 94 | .map_err(|e| format!("Failed to connect to relay {}: {}", self.url, e))?; | ||
| 94 | 95 | ||
| 95 | // Subscribe to Layer 1 (announcements) | 96 | // Subscribe to Layer 1 (announcements) |
| 96 | let filter = build_announcement_filter(since); | 97 | let filter = build_announcement_filter(since); |