diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-12-10 14:07:49 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-12-10 14:07:49 +0000 |
| commit | e0eedf25f3218ee54563229257c1ce949bfafd10 (patch) | |
| tree | 5e13a4bf3233cbab1f9f2467df23897ca9da769e /src | |
| parent | f34d0952eea4f4fac97be0cf46d0c105e1690866 (diff) | |
improve: count all active subscriptions in get_filter_count (IMPROVE-1)
Diffstat (limited to 'src')
| -rw-r--r-- | src/sync/mod.rs | 56 |
1 files changed, 47 insertions, 9 deletions
diff --git a/src/sync/mod.rs b/src/sync/mod.rs index d53bcfa..7a0a705 100644 --- a/src/sync/mod.rs +++ b/src/sync/mod.rs | |||
| @@ -1441,25 +1441,63 @@ impl SyncManager { | |||
| 1441 | 1441 | ||
| 1442 | /// Get the current filter count for a relay | 1442 | /// Get the current filter count for a relay |
| 1443 | /// | 1443 | /// |
| 1444 | /// Counts all outstanding subscriptions in pending batches for this relay. | 1444 | /// Counts both pending subscriptions (outstanding_subs in batches) and |
| 1445 | /// confirmed subscriptions (active Layer 2/3 filters based on RelayState). | ||
| 1445 | /// This is used to determine if consolidation is needed. | 1446 | /// This is used to determine if consolidation is needed. |
| 1447 | /// | ||
| 1448 | /// Confirmed filter counts: | ||
| 1449 | /// - Layer 1: 1 filter (announcement subscription) | ||
| 1450 | /// - Layer 2: 3 filters per 100-repo chunk (for kinds 1617/1618/1619/1621) | ||
| 1451 | /// - Layer 3: 3 filters per 100-event chunk (for replies/reactions/etc) | ||
| 1446 | async fn get_filter_count(&self, relay_url: &str) -> usize { | 1452 | async fn get_filter_count(&self, relay_url: &str) -> usize { |
| 1447 | let pending = self.pending_sync_index.read().await; | 1453 | // Count pending subscriptions |
| 1448 | 1454 | let pending_count = { | |
| 1449 | let count = match pending.get(relay_url) { | 1455 | let pending = self.pending_sync_index.read().await; |
| 1450 | Some(batches) => { | 1456 | match pending.get(relay_url) { |
| 1451 | batches.iter().map(|b| b.outstanding_subs.len()).sum() | 1457 | Some(batches) => batches.iter().map(|b| b.outstanding_subs.len()).sum(), |
| 1458 | None => 0, | ||
| 1459 | } | ||
| 1460 | }; | ||
| 1461 | |||
| 1462 | // Count confirmed subscriptions from relay state | ||
| 1463 | let confirmed_count = { | ||
| 1464 | let relay_index = self.relay_sync_index.read().await; | ||
| 1465 | if let Some(state) = relay_index.get(relay_url) { | ||
| 1466 | // Layer 1: 1 filter for announcements | ||
| 1467 | // Layer 2: 3 filters per 100-repo chunk (ceiling division) | ||
| 1468 | // Layer 3: 3 filters per 100-event chunk (ceiling division) | ||
| 1469 | let repo_count = state.repos.len(); | ||
| 1470 | let event_count = state.root_events.len(); | ||
| 1471 | |||
| 1472 | let layer1_filters = 1; | ||
| 1473 | let layer2_filters = if repo_count > 0 { | ||
| 1474 | ((repo_count + 99) / 100) * 3 | ||
| 1475 | } else { | ||
| 1476 | 0 | ||
| 1477 | }; | ||
| 1478 | let layer3_filters = if event_count > 0 { | ||
| 1479 | ((event_count + 99) / 100) * 3 | ||
| 1480 | } else { | ||
| 1481 | 0 | ||
| 1482 | }; | ||
| 1483 | |||
| 1484 | layer1_filters + layer2_filters + layer3_filters | ||
| 1485 | } else { | ||
| 1486 | 0 | ||
| 1452 | } | 1487 | } |
| 1453 | None => 0, | ||
| 1454 | }; | 1488 | }; |
| 1455 | 1489 | ||
| 1490 | let total_count = pending_count + confirmed_count; | ||
| 1491 | |||
| 1456 | tracing::debug!( | 1492 | tracing::debug!( |
| 1457 | relay = %relay_url, | 1493 | relay = %relay_url, |
| 1458 | filter_count = count, | 1494 | pending_count = pending_count, |
| 1495 | confirmed_count = confirmed_count, | ||
| 1496 | total_count = total_count, | ||
| 1459 | "Counted active filters for relay" | 1497 | "Counted active filters for relay" |
| 1460 | ); | 1498 | ); |
| 1461 | 1499 | ||
| 1462 | count | 1500 | total_count |
| 1463 | } | 1501 | } |
| 1464 | 1502 | ||
| 1465 | /// Wait until all pending batches for a relay are complete | 1503 | /// Wait until all pending batches for a relay are complete |