Skip to content

Commit 7ddd854

Browse files
deprecate with_cache_size instead of removing it
1 parent f610b78 commit 7ddd854

File tree

7 files changed

+52
-16
lines changed

7 files changed

+52
-16
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
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
@@ -84,7 +84,7 @@ libp2p-dcutr = { version = "0.12.0", path = "protocols/dcutr" }
8484
libp2p-dns = { version = "0.42.0", path = "transports/dns" }
8585
libp2p-floodsub = { version = "0.45.0", path = "protocols/floodsub" }
8686
libp2p-gossipsub = { version = "0.47.1", path = "protocols/gossipsub" }
87-
libp2p-identify = { version = "0.46.0", path = "protocols/identify" }
87+
libp2p-identify = { version = "0.45.1", path = "protocols/identify" }
8888
libp2p-identity = { version = "0.2.9" }
8989
libp2p-kad = { version = "0.47.0", path = "protocols/kad" }
9090
libp2p-mdns = { version = "0.46.0", path = "protocols/mdns" }

protocols/identify/CHANGELOG.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
## 0.46.0
2-
3-
- Update to the new `PeerAddresses` API with `PeerAddressesConfig`, changing `with_cache_size` to `with_cache_config`.
4-
See [PR 5574](https://github.com/libp2p/rust-libp2p/pull/5574).
5-
61
## 0.45.1
72

83
- Add `hide_listen_addrs` option to prevent leaking (local) listen addresses.
94
See [PR 5507](https://github.com/libp2p/rust-libp2p/pull/5507).
5+
- Add `with_cache_config` to fully configure the identify cache using the new `PeerAddressesConfig`.
6+
Deprecating `with_cache_size` in favor of the new `with_cache_config`.
7+
See [PR 5574](https://github.com/libp2p/rust-libp2p/pull/5574).
108

119
## 0.45.0
1210

protocols/identify/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "libp2p-identify"
33
edition = "2021"
44
rust-version = { workspace = true }
55
description = "Nodes identification protocol for libp2p"
6-
version = "0.46.0"
6+
version = "0.45.1"
77
authors = ["Parity Technologies <admin@parity.io>"]
88
license = "MIT"
99
repository = "https://github.com/libp2p/rust-libp2p"

protocols/identify/src/behaviour.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use libp2p_swarm::{
3434
use libp2p_swarm::{ConnectionId, THandler, THandlerOutEvent};
3535

3636
use std::collections::hash_map::Entry;
37+
use std::num::NonZeroUsize;
3738
use std::{
3839
collections::{HashMap, HashSet, VecDeque},
3940
task::Context,
@@ -195,6 +196,23 @@ impl Config {
195196
self
196197
}
197198

199+
/// Configures the size of the LRU cache, caching addresses of discovered peers.
200+
#[deprecated(since = "0.45.1", note = "Use `Config::with_cache_config` instead.")]
201+
pub fn with_cache_size(mut self, cache_size: usize) -> Self {
202+
match NonZeroUsize::new(cache_size) {
203+
Some(cache_size) => {
204+
if let Some(cache_config) = &mut self.cache_config {
205+
cache_config.number_of_peers = cache_size;
206+
} else {
207+
self.cache_config =
208+
Some(PeerAddressesConfig::default().with_number_of_peers(cache_size))
209+
}
210+
}
211+
None => self.cache_config = None,
212+
}
213+
self
214+
}
215+
198216
/// Configures whether we prevent sending out our listen addresses.
199217
pub fn with_hide_listen_addrs(mut self, b: bool) -> Self {
200218
self.hide_listen_addrs = b;

protocols/identify/tests/smoke.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ async fn emits_unique_listen_addresses() {
162162
identify::Config::new("a".to_string(), identity.public())
163163
.with_agent_version("b".to_string())
164164
.with_interval(Duration::from_secs(1))
165-
.with_cache_config(Some(PeerAddressesConfig {
166-
number_of_peers: NonZeroUsize::new(10).expect("10 != 0"),
167-
..Default::default()
168-
})),
165+
.with_cache_config(Some(
166+
PeerAddressesConfig::default()
167+
.with_number_of_peers(NonZeroUsize::new(10).expect("10 != 0")),
168+
)),
169169
)
170170
});
171171
let mut swarm2 = Swarm::new_ephemeral(|identity| {
@@ -237,7 +237,10 @@ async fn hides_listen_addresses() {
237237
identify::Config::new("a".to_string(), identity.public())
238238
.with_agent_version("b".to_string())
239239
.with_interval(Duration::from_secs(1))
240-
.with_cache_size(10),
240+
.with_cache_config(Some(
241+
PeerAddressesConfig::default()
242+
.with_number_of_peers(NonZeroUsize::new(10).expect("10 != 0")),
243+
)),
241244
)
242245
});
243246
let mut swarm2 = Swarm::new_ephemeral(|identity| {

swarm/src/behaviour/peer_addresses.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,31 @@ pub struct PeerAddressesConfig {
1515
pub number_of_peers: NonZeroUsize,
1616

1717
/// Maximum number of cached addresses per peer.
18-
pub number_of_addresses_by_peer: NonZeroUsize,
18+
pub number_of_addresses_per_peer: NonZeroUsize,
19+
}
20+
21+
impl PeerAddressesConfig {
22+
/// Configure the capacity of the [`PeerAddresses`] cache.
23+
pub fn with_number_of_peers(mut self, number_of_peers: NonZeroUsize) -> Self {
24+
self.number_of_peers = number_of_peers;
25+
self
26+
}
27+
28+
/// Configure the maximum number of cached addresses per peer.
29+
pub fn with_number_of_addresses_by_peer(
30+
mut self,
31+
number_of_addresses_per_peer: NonZeroUsize,
32+
) -> Self {
33+
self.number_of_addresses_per_peer = number_of_addresses_per_peer;
34+
self
35+
}
1936
}
2037

2138
impl Default for PeerAddressesConfig {
2239
fn default() -> Self {
2340
Self {
2441
number_of_peers: NonZeroUsize::new(100).expect("100 != 0"),
25-
number_of_addresses_by_peer: NonZeroUsize::new(10).expect("10 != 0"),
42+
number_of_addresses_per_peer: NonZeroUsize::new(10).expect("10 != 0"),
2643
}
2744
}
2845
}
@@ -76,7 +93,7 @@ impl PeerAddresses {
7693
if let Some(cached) = self.inner.get_mut(&peer) {
7794
cached.put(address, ()).is_none()
7895
} else {
79-
let mut set = LruCache::new(self.config.number_of_addresses_by_peer);
96+
let mut set = LruCache::new(self.config.number_of_addresses_per_peer);
8097
set.put(address, ());
8198
self.inner.put(peer, set);
8299

0 commit comments

Comments
 (0)