Skip to content

Commit e3e74c0

Browse files
committed
make connection_store a PeerStore component
1 parent 0853f3a commit e3e74c0

File tree

9 files changed

+105
-281
lines changed

9 files changed

+105
-281
lines changed

Cargo.lock

Lines changed: 1 addition & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ members = [
6565
"transports/websocket-websys",
6666
"transports/websocket",
6767
"transports/webtransport-websys",
68-
"wasm-tests/webtransport-tests", "misc/connection-tracker",
68+
"wasm-tests/webtransport-tests",
6969
]
7070
resolver = "2"
7171

misc/connection-tracker/Cargo.toml

Lines changed: 0 additions & 13 deletions
This file was deleted.

misc/connection-tracker/src/behavior.rs

Lines changed: 0 additions & 194 deletions
This file was deleted.

misc/connection-tracker/src/lib.rs

Lines changed: 0 additions & 44 deletions
This file was deleted.

misc/peer-store/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ libp2p-core = { workspace = true }
1313
libp2p-swarm = { workspace = true }
1414
lru = "0.12.3"
1515
libp2p-identity = { workspace = true, optional = true }
16+
tracing = { workspace = true }
1617

1718
[dev-dependencies]
1819
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }

misc/connection-tracker/src/store.rs renamed to misc/peer-store/src/connection_store.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1-
use libp2p_core::PeerId;
2-
use libp2p_swarm::ConnectionId;
31
use std::collections::{HashMap, HashSet};
2+
use libp2p_core::{ConnectedPoint, PeerId};
3+
use libp2p_swarm::ConnectionId;
4+
5+
/// Events emitted by the connection tracker behaviour.
6+
#[derive(Debug, Clone)]
7+
pub enum Event {
8+
/// A peer connected (first connection established).
9+
PeerConnected {
10+
peer_id: PeerId,
11+
connection_id: ConnectionId,
12+
endpoint: ConnectedPoint,
13+
},
14+
15+
/// A peer disconnected (last connection closed).
16+
PeerDisconnected {
17+
peer_id: PeerId,
18+
connection_id: ConnectionId,
19+
},
20+
}
421

522
/// Simple storage for connected peers.
623
#[derive(Debug, Default)]
@@ -19,25 +36,29 @@ impl ConnectionStore {
1936

2037
/// Add a new connection for a peer.
2138
/// Returns `true` if this is the first connection to the peer.
22-
pub fn connection_established(&mut self, peer_id: PeerId, connection_id: ConnectionId) -> bool {
23-
let connections = self.connected.entry(peer_id).or_insert_with(HashSet::new);
39+
pub fn connection_established(
40+
&mut self,
41+
peer_id: &PeerId,
42+
connection_id: &ConnectionId,
43+
) -> bool {
44+
let connections = self.connected.entry(*peer_id).or_insert_with(HashSet::new);
2445
let is_first_connection = connections.is_empty();
25-
connections.insert(connection_id);
46+
connections.insert(*connection_id);
2647
is_first_connection
2748
}
2849

2950
/// Remove a connection for a peer.
3051
/// Returns `true` if this was the last connection to the peer.
3152
pub fn connection_closed(
3253
&mut self,
33-
peer_id: PeerId,
34-
connection_id: ConnectionId,
35-
remaining_established: usize,
54+
peer_id: &PeerId,
55+
connection_id: &ConnectionId,
56+
remaining_established: &usize,
3657
) -> bool {
3758
if let Some(connections) = self.connected.get_mut(&peer_id) {
3859
connections.remove(&connection_id);
3960

40-
if remaining_established == 0 {
61+
if *remaining_established == 0 {
4162
self.connected.remove(&peer_id);
4263
return true;
4364
}
@@ -67,4 +88,4 @@ impl ConnectionStore {
6788
.map(|connections| connections.len())
6889
.unwrap_or(0)
6990
}
70-
}
91+
}

misc/peer-store/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
mod behaviour;
2121
pub mod memory_store;
2222
mod store;
23+
pub mod connection_store;
2324

2425
pub use behaviour::Behaviour;
2526
pub use store::Store;

0 commit comments

Comments
 (0)