Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

Commit

Permalink
Merge #479
Browse files Browse the repository at this point in the history
479: Return time since connection if no round trip yet r=koivunej a=jmmaloney4

Fixes #178.


Co-authored-by: Jack Maloney <[email protected]>
Co-authored-by: Joonas Koivunen <[email protected]>
  • Loading branch information
3 people committed Apr 6, 2022
2 parents 2ca9704 + 1cc0e52 commit 275b761
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* fix: strict ordering for DAG-CBOR-encoded map keys [#493]
* feat: upgrade libp2p to v0.43.0 [#499]
* feat(http): default values for --bits and --profile [#502]
* feat: return time since connecting began instead of null for latency [#479]

[#429]: https://github.com/rs-ipfs/rust-ipfs/pull/429
[#428]: https://github.com/rs-ipfs/rust-ipfs/pull/428
Expand All @@ -33,6 +34,7 @@
[#493]: https://github.com/rs-ipfs/rust-ipfs/pull/493
[#499]: https://github.com/rs-ipfs/rust-ipfs/pull/499
[#502]: https://github.com/rs-ipfs/rust-ipfs/pull/502
[#479]: https://github.com/rs-ipfs/rust-ipfs/pull/479

# 0.2.1

Expand Down
14 changes: 12 additions & 2 deletions src/p2p/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use libp2p::swarm::{
};
use std::collections::{hash_map::Entry, HashMap, HashSet, VecDeque};
use std::convert::{TryFrom, TryInto};
use std::time::Duration;
use std::time::{Duration, Instant};

/// A description of currently active connection.
#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -51,6 +51,7 @@ pub struct SwarmApi {
connections: HashMap<MultiaddrWithoutPeerId, PeerId>,
roundtrip_times: HashMap<PeerId, Duration>,
connected_peers: HashMap<PeerId, Vec<MultiaddrWithoutPeerId>>,
connected_times: HashMap<PeerId, Instant>,

/// The connections which have been requested, but the swarm/network is yet to ask for
/// addresses; currently filled in the order of adding, with the default size of one.
Expand Down Expand Up @@ -80,7 +81,11 @@ impl SwarmApi {
self.connected_peers
.iter()
.filter_map(move |(peer, conns)| {
let rtt = self.roundtrip_times.get(peer).cloned();
let rtt = self.roundtrip_times.get(peer).cloned().or_else(|| {
// If no rountrip time yet, just return time since connected.
// See: https://github.com/rs-ipfs/rust-ipfs/issues/178
self.connected_times.get(peer).map(Instant::elapsed)
});

conns.first().map(|any| Connection {
addr: MultiaddrWithPeerId::from((any.clone(), *peer)),
Expand Down Expand Up @@ -121,6 +126,9 @@ impl SwarmApi {
handler,
});

// store this for returning the time since connecting started before ping is available
self.connected_times.insert(addr.peer_id, Instant::now());

self.pending_addresses
.entry(addr.peer_id)
.or_insert_with(|| Vec::with_capacity(1))
Expand Down Expand Up @@ -291,6 +299,8 @@ impl NetworkBehaviour for SwarmApi {
closed_addr
);

self.connected_times.remove(peer_id);

if let ConnectedPoint::Dialer { .. } = endpoint {
let addr = MultiaddrWithPeerId::from((closed_addr, peer_id.to_owned()));

Expand Down

0 comments on commit 275b761

Please sign in to comment.