Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add batching support for AddAddress #117

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ where
self.network.add_address(peer, addr)
}

/// Adds a batch of known `Multiaddr` & `PeerId` pairs.
pub fn add_addresses(&mut self, addresses: Vec<(PeerId, Multiaddr)>) {
self.network.add_addresses(addresses)
}

/// Removes a `Multiaddr` for a `PeerId`.
pub fn remove_address(&mut self, peer: PeerId, addr: Multiaddr) {
self.network.remove_address(peer, addr)
Expand Down
12 changes: 12 additions & 0 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub enum NetworkCommand {
ListenOn(Multiaddr, UnboundedSender<ListenerEvent>),
AddExternalAddress(Multiaddr),
AddAddress(PeerId, Multiaddr),
AddAddresses(Vec<(PeerId, Multiaddr)>),
RemoveAddress(PeerId, Multiaddr),
PrunePeers(Duration),
Dial(PeerId),
Expand Down Expand Up @@ -352,6 +353,10 @@ impl NetworkService {
self.cmd(NetworkCommand::AddAddress(peer, addr));
}

pub fn add_addresses(&mut self, addresses: Vec<(PeerId, Multiaddr)>) {
self.cmd(NetworkCommand::AddAddresses(addresses));
}

pub fn remove_address(&mut self, peer: PeerId, addr: Multiaddr) {
self.cmd(NetworkCommand::RemoveAddress(peer, addr));
}
Expand Down Expand Up @@ -661,6 +666,13 @@ async fn poll_swarm<P: StoreParams>(
.behaviour_mut()
.add_address(&peer, addr, AddressSource::User);
}
NetworkCommand::AddAddresses(addresses) => {
for (peer, addr) in addresses {
swarm
.behaviour_mut()
.add_address(&peer, addr, AddressSource::User);
}
}
NetworkCommand::RemoveAddress(peer, addr) => {
swarm.behaviour_mut().remove_address(&peer, &addr);
}
Expand Down
Loading