Skip to content

Commit

Permalink
feat(identify): support pushing changes of external addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Nov 17, 2023
1 parent a7d4cb3 commit df1bcd2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions protocols/identify/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 0.44.1 - unreleased

- Support (enabled by default) pushing changes of external addresses.
See [PR 4885](https://github.com/libp2p/rust-libp2p/pull/4885).
- Ensure `Multiaddr` handled and returned by `Behaviour` are `/p2p` terminated.
See [PR 4596](https://github.com/libp2p/rust-libp2p/pull/4596).

Expand Down
23 changes: 22 additions & 1 deletion protocols/identify/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ pub struct Config {
/// Disabled by default.
pub push_listen_addr_updates: bool,

/// Whether new or changes in external 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 external addresses of the local node,
/// i.e. before the next periodic identify request with each peer.
///
/// Enabled by default.
pub push_external_addr_updates: bool,

/// How many entries of discovered peers to keep before we discard
/// the least-recently used one.
///
Expand All @@ -110,6 +120,7 @@ impl Config {
local_public_key,
interval: Duration::from_secs(5 * 60),
push_listen_addr_updates: false,
push_external_addr_updates: true,
cache_size: 100,
}
}
Expand All @@ -135,6 +146,14 @@ impl Config {
self
}

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

/// Configures the size of the LRU cache, caching addresses of discovered peers.
pub fn with_cache_size(mut self, cache_size: usize) -> Self {
self.cache_size = cache_size;
Expand Down Expand Up @@ -357,7 +376,9 @@ impl NetworkBehaviour for Behaviour {
self.events.extend(change_events)
}

if listen_addr_changed && self.config.push_listen_addr_updates {
if (listen_addr_changed && self.config.push_listen_addr_updates)
| (external_addr_changed && self.config.push_external_addr_updates)
{
// trigger an identify push for all connected peers
let push_events = self.connected.keys().map(|peer| ToSwarm::NotifyHandler {
peer_id: *peer,
Expand Down

0 comments on commit df1bcd2

Please sign in to comment.