From c82684092c7b4f81e49833b0888500fcb9851218 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 11 Dec 2025 11:19:38 +0000 Subject: fix(sync): improve metrics recording and connection failure detection Changes: - Fix connection attempt metrics: record success/failure based on actual connection result instead of pre-emptively recording failure - Add health tracker integration on connection failure: call record_failure() and record_health_state() in error path - Add connection verification in relay_connection.rs: wait 500ms after connect() then verify is_connected() to detect silent failures - Add configurable disconnect check interval via NGIT_SYNC_DISCONNECT_CHECK_INTERVAL_SECS env var - Update TestRelay with fast test settings: startup_delay=0, jitter=0, disconnect_check_interval=1s - Add debug output to metrics tests for investigation Note: Tests may still fail due to 5-second base backoff in health tracker. A follow-up task will add NGIT_SYNC_BASE_BACKOFF_SECS config parameter to allow faster test cycles. Related: metrics-wiring-plan.md Tasks 1 & 2 --- src/sync/relay_connection.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src/sync/relay_connection.rs') diff --git a/src/sync/relay_connection.rs b/src/sync/relay_connection.rs index 09c9887..d69e112 100644 --- a/src/sync/relay_connection.rs +++ b/src/sync/relay_connection.rs @@ -55,7 +55,8 @@ impl RelayConnection { /// This method: /// 1. Adds the relay to the client /// 2. Establishes the WebSocket connection - /// 3. Subscribes to Layer 1 filter (kinds 30617 + 30618) + /// 3. Verifies connection was established + /// 4. Subscribes to Layer 1 filter (kinds 30617 + 30618) /// /// # Arguments /// * `since` - Optional timestamp for incremental sync on reconnect @@ -76,6 +77,21 @@ impl RelayConnection { // Establish connection self.client.connect().await; + // Wait briefly for connection to establish and check status + // nostr-sdk's connect() is async and may not immediately reflect failure + tokio::time::sleep(std::time::Duration::from_millis(500)).await; + + // Check if relay is actually connected + let relay = self.client.relay(&self.url).await + .map_err(|e| format!("Failed to get relay handle for {}: {}", self.url, e))?; + + if !relay.is_connected() { + return Err(format!( + "Failed to connect to relay {}: connection not established after timeout", + self.url + )); + } + // Subscribe to Layer 1 (announcements) let filter = build_announcement_filter(since); let output = self -- cgit v1.2.3