Skip to content

Commit

Permalink
seed: consider if tracking relationship was already established
Browse files Browse the repository at this point in the history
When calling `Node::track_project`, it might very well be that the
project/peer is already tracked, which would result in unnecessary
broadcast traffic. Thus, don't emit any events in this case.

Also, change the order of `track` and `replicate` to ensure we
do actually fetch the newly-tracked peer's view. Additionally, we can
skip `replicate` if we already tracked the peer, as subsequent broadcast
messages won't be considered uninteresting.

Ref #734
Signed-off-by: Kim Altintop <[email protected]>
  • Loading branch information
kim committed Aug 19, 2021
1 parent 94f407f commit f937165
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions seed/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ impl Node {

if mode.is_trackable(peer_id, urn) {
// Attempt to track, but keep going if it fails.
if Node::track_project(api, urn, provider).await.is_ok() {
if let Ok(true) = Node::track_project(api, urn, provider).await {
let event = Event::project_tracked(urn.clone(), *peer_id, api).await?;
api.announce(Payload {
urn: urn.clone(),
Expand All @@ -425,11 +425,14 @@ impl Node {
}

/// Attempt to track a project.
///
/// Returns `false` is the project was already tracked (so this call had no
/// effect), `true` otherwise.
async fn track_project(
api: &Peer<Signer>,
urn: &Urn,
peer_info: &PeerInfo<std::net::SocketAddr>,
) -> Result<(), Error> {
) -> Result<bool, Error> {
let peer_id = peer_info.peer_id;
let addr_hints = peer_info.seen_addrs.iter().copied().collect::<Vec<_>>();

Expand All @@ -441,17 +444,20 @@ impl Node {
.build(storage)
.map_err(|e| Error::MkFetcher(e.into()))??;
replication::replicate(storage, fetcher, cfg, None)?;
tracking::track(storage, &urn, peer_id)?;
let was_updated = tracking::track(storage, &urn, peer_id)?;

Ok::<_, Error>(())
Ok::<_, Error>(was_updated)
})
.await?
};

match &result {
Ok(()) => {
Ok(true) => {
tracing::info!("Successfully tracked project {} from peer {}", urn, peer_id);
},
Ok(false) => {
tracing::info!("Project {} from peer {} already tracked", urn, peer_id);
},
Err(err) => {
tracing::info!(
"Error tracking project {} from peer {}: {}",
Expand Down

0 comments on commit f937165

Please sign in to comment.