diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-12-11 13:09:39 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-12-11 13:09:39 +0000 |
| commit | 7146ee550944c71a4e48018a34820ce9a9d99f95 (patch) | |
| tree | 19400e16369f32630d193744de32eefeeacbc69e | |
| parent | a56e12e0aed87c68fd49b453b5c3dc0bfbf81285 (diff) | |
fix: classify sync events as startup/live based on EOSE, not relay type
Previously, events were classified as 'startup' or 'live' based on whether
they came from a bootstrap relay (is_bootstrap flag). This meant ALL events
from bootstrap relays were counted as 'startup', even events received after
the initial sync completed.
Now events are classified based on whether EOSE (End Of Stored Events) has
been received for that connection:
- Events BEFORE EOSE → 'startup' (historical events during initial sync)
- Events AFTER EOSE → 'live' (new events via real-time subscription)
This enables the test_live_sync_event_count test which validates that events
received after sync connection is established are counted as live events.
Also removed the #[ignore] attribute from test_live_sync_event_count since
the metrics are now properly wired up.
| -rw-r--r-- | src/sync/mod.rs | 12 | ||||
| -rw-r--r-- | tests/sync/metrics.rs | 6 |
2 files changed, 8 insertions, 10 deletions
diff --git a/src/sync/mod.rs b/src/sync/mod.rs index 15c89e3..b6cd00a 100644 --- a/src/sync/mod.rs +++ b/src/sync/mod.rs | |||
| @@ -1249,19 +1249,21 @@ impl SyncManager { | |||
| 1249 | // Spawn event processor | 1249 | // Spawn event processor |
| 1250 | let relay_url_clone = relay_url.clone(); | 1250 | let relay_url_clone = relay_url.clone(); |
| 1251 | let metrics_clone = self.metrics.clone(); // Clone metrics for the spawned task | 1251 | let metrics_clone = self.metrics.clone(); // Clone metrics for the spawned task |
| 1252 | let is_bootstrap_clone = is_bootstrap; // Clone is_bootstrap for the spawned task | ||
| 1253 | tokio::spawn(async move { | 1252 | tokio::spawn(async move { |
| 1254 | // Track whether we've already sent a disconnect notification | 1253 | // Track whether we've already sent a disconnect notification |
| 1255 | let mut disconnect_sent = false; | 1254 | let mut disconnect_sent = false; |
| 1255 | // Track whether EOSE has been received - events before EOSE are "startup", after are "live" | ||
| 1256 | let mut eose_received = false; | ||
| 1256 | 1257 | ||
| 1257 | while let Some(relay_event) = event_rx.recv().await { | 1258 | while let Some(relay_event) = event_rx.recv().await { |
| 1258 | match relay_event { | 1259 | match relay_event { |
| 1259 | RelayEvent::Event(event) => { | 1260 | RelayEvent::Event(event) => { |
| 1260 | if let Some(ref metrics) = metrics_clone { | 1261 | if let Some(ref metrics) = metrics_clone { |
| 1261 | let source = if is_bootstrap_clone { | 1262 | // Events before EOSE are "startup", events after EOSE are "live" |
| 1262 | event_source::STARTUP | 1263 | let source = if eose_received { |
| 1263 | } else { | ||
| 1264 | event_source::LIVE | 1264 | event_source::LIVE |
| 1265 | } else { | ||
| 1266 | event_source::STARTUP | ||
| 1265 | }; | 1267 | }; |
| 1266 | metrics.record_event(source); | 1268 | metrics.record_event(source); |
| 1267 | } | 1269 | } |
| @@ -1275,6 +1277,8 @@ impl SyncManager { | |||
| 1275 | .await; | 1277 | .await; |
| 1276 | } | 1278 | } |
| 1277 | RelayEvent::EndOfStoredEvents(sub_id) => { | 1279 | RelayEvent::EndOfStoredEvents(sub_id) => { |
| 1280 | // Mark EOSE as received - subsequent events are "live" | ||
| 1281 | eose_received = true; | ||
| 1278 | tracing::debug!( | 1282 | tracing::debug!( |
| 1279 | relay = %relay_url_clone, | 1283 | relay = %relay_url_clone, |
| 1280 | sub_id = %sub_id, | 1284 | sub_id = %sub_id, |
diff --git a/tests/sync/metrics.rs b/tests/sync/metrics.rs index 3accd0f..98ce4f3 100644 --- a/tests/sync/metrics.rs +++ b/tests/sync/metrics.rs | |||
| @@ -410,11 +410,7 @@ async fn test_connection_failure_increments_counter() { | |||
| 410 | /// This test validates that events received via live subscription | 410 | /// This test validates that events received via live subscription |
| 411 | /// (after sync connection is established) are counted separately | 411 | /// (after sync connection is established) are counted separately |
| 412 | /// from startup/bootstrap events. | 412 | /// from startup/bootstrap events. |
| 413 | /// | ||
| 414 | /// NOTE: This test may fail until sync metrics recording is fully wired up. | ||
| 415 | /// The test documents the expected behavior. | ||
| 416 | #[tokio::test] | 413 | #[tokio::test] |
| 417 | #[ignore] // Enable when live event sync metrics are wired up | ||
| 418 | async fn test_live_sync_event_count() { | 414 | async fn test_live_sync_event_count() { |
| 419 | let mut harness = MetricsTestHarness::with_sources(1).await; | 415 | let mut harness = MetricsTestHarness::with_sources(1).await; |
| 420 | 416 | ||
| @@ -437,8 +433,6 @@ async fn test_live_sync_event_count() { | |||
| 437 | let live_count = metrics.events_total("live"); | 433 | let live_count = metrics.events_total("live"); |
| 438 | println!("Live events synced: {:?}", live_count); | 434 | println!("Live events synced: {:?}", live_count); |
| 439 | 435 | ||
| 440 | // NOTE: This will likely fail until sync metrics are wired up | ||
| 441 | // Test documents the expectation | ||
| 442 | assert_eq!(live_count, Some(2), "Should have 2 live events"); | 436 | assert_eq!(live_count, Some(2), "Should have 2 live events"); |
| 443 | 437 | ||
| 444 | harness.stop_all().await; | 438 | harness.stop_all().await; |