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

[identify] Add configurable automatic push on listen addr changes. #2004

Merged
merged 3 commits into from
Mar 22, 2021
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
4 changes: 4 additions & 0 deletions protocols/identify/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 0.29.0 [unreleased]

- Add support for configurable automatic push to connected peers
on listen addr changes. Disabled by default.
[PR 2004](https://github.com/libp2p/rust-libp2p/pull/2004)

- Implement the `/ipfs/id/push/1.0.0` protocol.
cf. https://github.com/libp2p/specs/tree/master/identify#identifypush
[PR 1999](https://github.com/libp2p/rust-libp2p/pull/1999)
Expand Down
35 changes: 33 additions & 2 deletions protocols/identify/src/identify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ pub struct IdentifyConfig {
///
/// Defaults to 5 minutes.
pub interval: Duration,

/// Whether new or expired listen addresses of the local node should
/// trigger an active push of an identify message to all connected peers.
///
/// Enabling this option can result in connected peers being informed
/// earlier about new or expired listen addresses of the local node,
/// i.e. before the next periodic identify request with each peer.
///
/// Disabled by default.
pub push_listen_addr_updates: bool,
}

impl IdentifyConfig {
Expand All @@ -119,6 +129,7 @@ impl IdentifyConfig {
local_public_key,
initial_delay: Duration::from_millis(500),
interval: Duration::from_secs(5 * 60),
push_listen_addr_updates: false,
}
}

Expand All @@ -141,6 +152,14 @@ impl IdentifyConfig {
self.interval = d;
self
}

/// Configures whether new or expired listen addresses of the local
/// node should trigger an active push of an identify message to all
/// connected peers.
pub fn with_push_listen_addr_updates(mut self, b: bool) -> Self {
self.push_listen_addr_updates = b;
self
}
}

impl Identify {
Expand Down Expand Up @@ -194,7 +213,7 @@ impl NetworkBehaviour for Identify {
ConnectedPoint::Listener { send_back_addr, .. } => send_back_addr.clone(),
};

self.connected.entry(*peer_id).or_default().insert(*conn, addr.clone());
self.connected.entry(*peer_id).or_default().insert(*conn, addr);
}

fn inject_connection_closed(&mut self, peer_id: &PeerId, conn: &ConnectionId, _: &ConnectedPoint) {
Expand All @@ -214,6 +233,18 @@ impl NetworkBehaviour for Identify {
self.pending_push.remove(peer_id);
}

fn inject_new_listen_addr(&mut self, _addr: &Multiaddr) {
if self.config.push_listen_addr_updates {
self.pending_push.extend(self.connected.keys());
}
}

fn inject_expired_listen_addr(&mut self, _addr: &Multiaddr) {
if self.config.push_listen_addr_updates {
self.pending_push.extend(self.connected.keys());
}
}

fn inject_event(
&mut self,
peer_id: PeerId,
Expand Down Expand Up @@ -341,7 +372,7 @@ impl NetworkBehaviour for Identify {
Poll::Ready(Err(err)) => {
let event = IdentifyEvent::Error {
peer_id: peer,
error: ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(err.into()))
error: ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(err))
};
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
},
Expand Down