Skip to content

Commit 6adf42a

Browse files
committed
sdk: add sync_id for enhanced gossip sync tracing
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 6ef4fbe commit 6adf42a

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

crates/nostr-sdk/src/client/mod.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,7 @@ impl Client {
13621362
/// 2. For any relays where negentropy sync fails, falls back to standard REQ messages to fetch the gossip lists
13631363
async fn check_and_update_gossip<I>(
13641364
&self,
1365-
gossip: &Arc<dyn NostrGossip>,
1365+
gossip: &GossipWrapper,
13661366
public_keys: I,
13671367
gossip_kind: GossipListKind,
13681368
) -> Result<(), Error>
@@ -1382,11 +1382,13 @@ impl Client {
13821382
return Ok(());
13831383
}
13841384

1385-
tracing::debug!("Acquiring gossip sync permit...");
1385+
let sync_id: u64 = gossip.next_sync_id();
1386+
1387+
tracing::debug!(sync_id, "Acquiring gossip sync permit...");
13861388

13871389
let _permit = self.gossip_sync.acquire().await;
13881390

1389-
tracing::debug!(kind = ?gossip_kind, "Acquired gossip sync permit. Start syncing...");
1391+
tracing::debug!(sync_id, kind = ?gossip_kind, "Acquired gossip sync permit. Start syncing...");
13901392

13911393
// Second check: check data is still outdated after acquiring permit
13921394
// (another process might have updated it while we were waiting)
@@ -1396,27 +1398,33 @@ impl Client {
13961398

13971399
// Double-check: data might have been updated while waiting for permit
13981400
if outdated_public_keys.is_empty() {
1399-
tracing::debug!(kind = ?gossip_kind, "Gossip data is up to date.");
1401+
tracing::debug!(sync_id = %sync_id, kind = ?gossip_kind, "Gossip sync skipped: data updated by another process while acquiring permits.");
14001402
return Ok(());
14011403
}
14021404

14031405
// Negentropy sync and database check
14041406
let (output, stored_events) = self
1405-
.check_and_update_gossip_sync(gossip, &gossip_kind, outdated_public_keys.clone())
1407+
.check_and_update_gossip_sync(
1408+
sync_id,
1409+
gossip,
1410+
&gossip_kind,
1411+
outdated_public_keys.clone(),
1412+
)
14061413
.await?;
14071414

14081415
// Keep track of the missing public keys
14091416
let mut missing_public_keys: HashSet<PublicKey> = outdated_public_keys;
14101417

14111418
// Check if sync failed for some relay
14121419
if !output.failed.is_empty() {
1413-
tracing::debug!(
1420+
tracing::debug!(sync_id,
14141421
relays = ?output.failed,
14151422
"Gossip sync failed for some relays."
14161423
);
14171424

14181425
// Try to fetch the updated events
14191426
self.check_and_update_gossip_fetch(
1427+
sync_id,
14201428
gossip,
14211429
&gossip_kind,
14221430
&output,
@@ -1429,6 +1437,7 @@ impl Client {
14291437
if !missing_public_keys.is_empty() {
14301438
// Try to fetch the missing events
14311439
self.check_and_update_gossip_missing(
1440+
sync_id,
14321441
gossip,
14331442
&gossip_kind,
14341443
&output,
@@ -1438,14 +1447,15 @@ impl Client {
14381447
}
14391448
}
14401449

1441-
tracing::debug!(kind = ?gossip_kind, "Gossip sync terminated.");
1450+
tracing::debug!(sync_id, kind = ?gossip_kind, "Gossip sync terminated.");
14421451

14431452
Ok(())
14441453
}
14451454

14461455
/// Check and update gossip data using negentropy sync
14471456
async fn check_and_update_gossip_sync(
14481457
&self,
1458+
sync_id: u64,
14491459
gossip: &Arc<dyn NostrGossip>,
14501460
gossip_kind: &GossipListKind,
14511461
outdated_public_keys: HashSet<PublicKey>,
@@ -1454,6 +1464,7 @@ impl Client {
14541464
let kind: Kind = gossip_kind.to_event_kind();
14551465

14561466
tracing::debug!(
1467+
sync_id,
14571468
public_keys = outdated_public_keys.len(),
14581469
"Syncing outdated gossip data."
14591470
);
@@ -1499,6 +1510,7 @@ impl Client {
14991510
/// Try to fetch the new gossip events from the relays that failed the negentropy sync
15001511
async fn check_and_update_gossip_fetch(
15011512
&self,
1513+
sync_id: u64,
15021514
gossip: &Arc<dyn NostrGossip>,
15031515
gossip_kind: &GossipListKind,
15041516
output: &Output<Reconciliation>,
@@ -1533,11 +1545,15 @@ impl Client {
15331545
}
15341546

15351547
if filters.is_empty() {
1536-
tracing::debug!("Skipping gossip fetch, as it's no longer required.");
1548+
tracing::debug!(
1549+
sync_id,
1550+
"Skipping gossip fetch, as it's no longer required."
1551+
);
15371552
return Ok(());
15381553
}
15391554

15401555
tracing::debug!(
1556+
sync_id,
15411557
filters = filters.len(),
15421558
"Fetching outdated gossip data from relays."
15431559
);
@@ -1569,6 +1585,7 @@ impl Client {
15691585
/// Try to fetch the gossip events for the missing public keys from the relays that failed the negentropy sync
15701586
async fn check_and_update_gossip_missing(
15711587
&self,
1588+
sync_id: u64,
15721589
gossip: &Arc<dyn NostrGossip>,
15731590
gossip_kind: &GossipListKind,
15741591
output: &Output<Reconciliation>,
@@ -1578,6 +1595,7 @@ impl Client {
15781595
let kind: Kind = gossip_kind.to_event_kind();
15791596

15801597
tracing::debug!(
1598+
sync_id,
15811599
public_keys = missing_public_keys.len(),
15821600
"Fetching missing gossip data from relays."
15831601
);

crates/nostr-sdk/src/gossip/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use std::collections::{BTreeSet, HashMap, HashSet};
66
use std::ops::Deref;
7+
use std::sync::atomic::{AtomicU64, Ordering};
78
use std::sync::Arc;
89

910
use nostr::prelude::*;
@@ -27,6 +28,7 @@ pub enum BrokenDownFilters {
2728
#[derive(Debug, Clone)]
2829
pub(crate) struct GossipWrapper {
2930
gossip: Arc<dyn NostrGossip>,
31+
sync_counter: Arc<AtomicU64>,
3032
}
3133

3234
impl Deref for GossipWrapper {
@@ -41,7 +43,15 @@ impl Deref for GossipWrapper {
4143
impl GossipWrapper {
4244
#[inline]
4345
pub(crate) fn new(gossip: Arc<dyn NostrGossip>) -> Self {
44-
Self { gossip }
46+
Self {
47+
gossip,
48+
sync_counter: Arc::new(AtomicU64::new(0)),
49+
}
50+
}
51+
52+
#[inline]
53+
pub(crate) fn next_sync_id(&self) -> u64 {
54+
self.sync_counter.fetch_add(1, Ordering::SeqCst)
4555
}
4656

4757
pub(crate) async fn get_relays<'a, I>(

0 commit comments

Comments
 (0)