diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-12-11 11:19:38 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-12-11 11:19:38 +0000 |
| commit | c82684092c7b4f81e49833b0888500fcb9851218 (patch) | |
| tree | 6853940a5bb975aa17e1f91be988e3b3303ff39f /src/sync/relay_connection.rs | |
| parent | 532a7d0d5d8461bad0fc799aacb5eea0135f79f3 (diff) | |
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
Diffstat (limited to 'src/sync/relay_connection.rs')
| -rw-r--r-- | src/sync/relay_connection.rs | 18 |
1 files changed, 17 insertions, 1 deletions
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 { | |||
| 55 | /// This method: | 55 | /// This method: |
| 56 | /// 1. Adds the relay to the client | 56 | /// 1. Adds the relay to the client |
| 57 | /// 2. Establishes the WebSocket connection | 57 | /// 2. Establishes the WebSocket connection |
| 58 | /// 3. Subscribes to Layer 1 filter (kinds 30617 + 30618) | 58 | /// 3. Verifies connection was established |
| 59 | /// 4. Subscribes to Layer 1 filter (kinds 30617 + 30618) | ||
| 59 | /// | 60 | /// |
| 60 | /// # Arguments | 61 | /// # Arguments |
| 61 | /// * `since` - Optional timestamp for incremental sync on reconnect | 62 | /// * `since` - Optional timestamp for incremental sync on reconnect |
| @@ -76,6 +77,21 @@ impl RelayConnection { | |||
| 76 | // Establish connection | 77 | // Establish connection |
| 77 | self.client.connect().await; | 78 | self.client.connect().await; |
| 78 | 79 | ||
| 80 | // Wait briefly for connection to establish and check status | ||
| 81 | // nostr-sdk's connect() is async and may not immediately reflect failure | ||
| 82 | tokio::time::sleep(std::time::Duration::from_millis(500)).await; | ||
| 83 | |||
| 84 | // Check if relay is actually connected | ||
| 85 | let relay = self.client.relay(&self.url).await | ||
| 86 | .map_err(|e| format!("Failed to get relay handle for {}: {}", self.url, e))?; | ||
| 87 | |||
| 88 | if !relay.is_connected() { | ||
| 89 | return Err(format!( | ||
| 90 | "Failed to connect to relay {}: connection not established after timeout", | ||
| 91 | self.url | ||
| 92 | )); | ||
| 93 | } | ||
| 94 | |||
| 79 | // Subscribe to Layer 1 (announcements) | 95 | // Subscribe to Layer 1 (announcements) |
| 80 | let filter = build_announcement_filter(since); | 96 | let filter = build_announcement_filter(since); |
| 81 | let output = self | 97 | let output = self |