build(deps): Update lru to 0.8.0 (#2908)

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
Thomas Eizinger 2022-09-27 11:39:10 +10:00 committed by GitHub
parent d747537911
commit b28ab2c6bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 58 additions and 12 deletions

View File

@ -64,6 +64,8 @@
- Update to [`libp2p-metrics` `v0.10.0`](misc/metrics/CHANGELOG.md#0100).
- Update to [`libp2p-kad` `v0.41.0`](protocols/kad/CHANGELOG.md#0410).
-
- Update to [`libp2p-identify` `v0.39.1`](protocols/identify/CHANGELOG.md#0400).
- Update to [`libp2p-noise` `v0.39.1`](transports/noise/CHANGELOG.md#0391).

View File

@ -84,7 +84,7 @@ libp2p-autonat = { version = "0.7.0", path = "protocols/autonat", optional = tru
libp2p-core = { version = "0.36.1", path = "core", default-features = false }
libp2p-dcutr = { version = "0.6.0", path = "protocols/dcutr", optional = true }
libp2p-floodsub = { version = "0.39.1", path = "protocols/floodsub", optional = true }
libp2p-identify = { version = "0.39.0", path = "protocols/identify", optional = true }
libp2p-identify = { version = "0.39.1", path = "protocols/identify", optional = true }
libp2p-kad = { version = "0.41.0", path = "protocols/kad", optional = true }
libp2p-metrics = { version = "0.10.0", path = "misc/metrics", optional = true }
libp2p-mplex = { version = "0.36.1", path = "muxers/mplex", optional = true }

View File

@ -1,6 +1,8 @@
# 0.10.0 [unreleased]
- Update to `libp2p-kad` `v0.41.0`.
-
- Update to `libp2p-identify` `v0.39.1`.
# 0.9.0

View File

@ -21,7 +21,7 @@ dcutr = ["libp2p-dcutr"]
[dependencies]
libp2p-core = { version = "0.36.0", path = "../../core", default-features = false }
libp2p-dcutr = { version = "0.6.0", path = "../../protocols/dcutr", optional = true }
libp2p-identify = { version = "0.39.0", path = "../../protocols/identify", optional = true }
libp2p-identify = { version = "0.39.1", path = "../../protocols/identify", optional = true }
libp2p-kad = { version = "0.41.0", path = "../../protocols/kad", optional = true }
libp2p-ping = { version = "0.39.0", path = "../../protocols/ping", optional = true }
libp2p-relay = { version = "0.12.0", path = "../../protocols/relay", optional = true }

View File

@ -1,3 +1,7 @@
# 0.39.1 [unreleased]
- Update dependencies.
# 0.39.0
- Update to `libp2p-swarm` `v0.39.0`.

View File

@ -3,7 +3,7 @@ name = "libp2p-identify"
edition = "2021"
rust-version = "1.56.1"
description = "Nodes identifcation protocol for libp2p"
version = "0.39.0"
version = "0.39.1"
authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
@ -17,7 +17,7 @@ futures-timer = "3.0.2"
libp2p-core = { version = "0.36.0", path = "../../core", default-features = false }
libp2p-swarm = { version = "0.39.0", path = "../../swarm" }
log = "0.4.1"
lru = "0.7.2"
lru = "0.8.0"
prost-codec = { version = "0.2", path = "../../misc/prost-codec" }
prost = "0.11"
smallvec = "1.6.1"

View File

@ -31,6 +31,7 @@ use libp2p_swarm::{
NotifyHandler, PollParameters,
};
use lru::LruCache;
use std::num::NonZeroUsize;
use std::{
collections::{HashMap, HashSet, VecDeque},
iter::FromIterator,
@ -58,7 +59,7 @@ pub struct Identify {
/// the local peer should be sent.
pending_push: HashSet<PeerId>,
/// The addresses of all peers that we have discovered.
discovered_peers: LruCache<PeerId, HashSet<Multiaddr>>,
discovered_peers: PeerCache,
}
/// A pending reply to an inbound identification request.
@ -175,7 +176,10 @@ impl IdentifyConfig {
impl Identify {
/// Creates a new `Identify` network behaviour.
pub fn new(config: IdentifyConfig) -> Self {
let discovered_peers = LruCache::new(config.cache_size);
let discovered_peers = match NonZeroUsize::new(config.cache_size) {
None => PeerCache::disabled(),
Some(size) => PeerCache::enabled(size),
};
Identify {
config,
@ -303,7 +307,7 @@ impl NetworkBehaviour for Identify {
// Replace existing addresses to prevent other peer from filling up our memory.
self.discovered_peers
.put(peer_id, HashSet::from_iter(info.listen_addrs.clone()));
.put(peer_id, info.listen_addrs.iter().cloned());
let observed = info.observed_addr.clone();
self.events.push_back(NetworkBehaviourAction::GenerateEvent(
@ -441,11 +445,7 @@ impl NetworkBehaviour for Identify {
}
fn addresses_of_peer(&mut self, peer: &PeerId) -> Vec<Multiaddr> {
self.discovered_peers
.get(peer)
.cloned()
.map(Vec::from_iter)
.unwrap_or_default()
self.discovered_peers.get(peer)
}
}
@ -506,6 +506,44 @@ fn multiaddr_matches_peer_id(addr: &Multiaddr, peer_id: &PeerId) -> bool {
true
}
struct PeerCache(Option<LruCache<PeerId, HashSet<Multiaddr>>>);
impl PeerCache {
fn disabled() -> Self {
Self(None)
}
fn enabled(size: NonZeroUsize) -> Self {
Self(Some(LruCache::new(size)))
}
fn get_mut(&mut self, peer: &PeerId) -> Option<&mut HashSet<Multiaddr>> {
self.0.as_mut()?.get_mut(peer)
}
fn put(&mut self, peer: PeerId, addresses: impl Iterator<Item = Multiaddr>) {
let cache = match self.0.as_mut() {
None => return,
Some(cache) => cache,
};
cache.put(peer, HashSet::from_iter(addresses));
}
fn get(&mut self, peer: &PeerId) -> Vec<Multiaddr> {
let cache = match self.0.as_mut() {
None => return Vec::new(),
Some(cache) => cache,
};
cache
.get(peer)
.cloned()
.map(Vec::from_iter)
.unwrap_or_default()
}
}
#[cfg(test)]
mod tests {
use super::*;