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

* Add configurable automatic push of listen addr changes.

* Update changelog and cleanup.
This commit is contained in:
Roman Borschel
2021-03-22 10:53:30 +01:00
committed by GitHub
parent 63512e5f16
commit c8d69ab2e2
2 changed files with 37 additions and 2 deletions

View File

@ -1,5 +1,9 @@
# 0.29.0 [unreleased] # 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. - Implement the `/ipfs/id/push/1.0.0` protocol.
cf. https://github.com/libp2p/specs/tree/master/identify#identifypush cf. https://github.com/libp2p/specs/tree/master/identify#identifypush
[PR 1999](https://github.com/libp2p/rust-libp2p/pull/1999) [PR 1999](https://github.com/libp2p/rust-libp2p/pull/1999)

View File

@ -107,6 +107,16 @@ pub struct IdentifyConfig {
/// ///
/// Defaults to 5 minutes. /// Defaults to 5 minutes.
pub interval: Duration, 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 { impl IdentifyConfig {
@ -119,6 +129,7 @@ impl IdentifyConfig {
local_public_key, local_public_key,
initial_delay: Duration::from_millis(500), initial_delay: Duration::from_millis(500),
interval: Duration::from_secs(5 * 60), interval: Duration::from_secs(5 * 60),
push_listen_addr_updates: false,
} }
} }
@ -141,6 +152,14 @@ impl IdentifyConfig {
self.interval = d; self.interval = d;
self 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 { impl Identify {
@ -194,7 +213,7 @@ impl NetworkBehaviour for Identify {
ConnectedPoint::Listener { send_back_addr, .. } => send_back_addr.clone(), 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) { fn inject_connection_closed(&mut self, peer_id: &PeerId, conn: &ConnectionId, _: &ConnectedPoint) {
@ -214,6 +233,18 @@ impl NetworkBehaviour for Identify {
self.pending_push.remove(peer_id); 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( fn inject_event(
&mut self, &mut self,
peer_id: PeerId, peer_id: PeerId,
@ -341,7 +372,7 @@ impl NetworkBehaviour for Identify {
Poll::Ready(Err(err)) => { Poll::Ready(Err(err)) => {
let event = IdentifyEvent::Error { let event = IdentifyEvent::Error {
peer_id: peer, peer_id: peer,
error: ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(err.into())) error: ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(err))
}; };
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)); return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
}, },