9
9
//! ```
10
10
11
11
use std:: {
12
- collections:: { HashMap , VecDeque } ,
12
+ collections:: VecDeque ,
13
13
num:: NonZeroUsize ,
14
14
task:: { Poll , Waker } ,
15
15
} ;
@@ -49,10 +49,9 @@ pub enum Event {
49
49
50
50
/// A in-memory store that uses LRU cache for bounded storage of addresses
51
51
/// and a frequency-based ordering of addresses.
52
- #[ derive( Default ) ]
53
52
pub struct MemoryStore < T = ( ) > {
54
53
/// The internal store.
55
- records : HashMap < PeerId , PeerRecord < T > > ,
54
+ records : LruCache < PeerId , PeerRecord < T > > ,
56
55
/// Events to emit to [`Behaviour`](crate::Behaviour) and [`Swarm`](libp2p_swarm::Swarm).
57
56
pending_events : VecDeque < Event > ,
58
57
/// Config of the store.
@@ -65,8 +64,8 @@ impl<T> MemoryStore<T> {
65
64
/// Create a new [`MemoryStore`] with the given config.
66
65
pub fn new ( config : Config ) -> Self {
67
66
Self {
67
+ records : LruCache :: new ( config. peer_capacity ( ) . get ( ) ) ,
68
68
config,
69
- records : HashMap :: new ( ) ,
70
69
pending_events : VecDeque :: default ( ) ,
71
70
waker : None ,
72
71
}
@@ -137,7 +136,7 @@ impl<T> MemoryStore<T> {
137
136
138
137
/// Get a reference to a peer's custom data.
139
138
pub fn get_custom_data ( & self , peer : & PeerId ) -> Option < & T > {
140
- self . records . get ( peer) . and_then ( |r| r. get_custom_data ( ) )
139
+ self . records . peek ( peer) . and_then ( |r| r. get_custom_data ( ) )
141
140
}
142
141
143
142
/// Take ownership of the internal data, leaving `None` in its place.
@@ -240,7 +239,7 @@ impl<T> Store for MemoryStore<T> {
240
239
}
241
240
242
241
fn addresses_of_peer ( & self , peer : & PeerId ) -> Option < impl Iterator < Item = & Multiaddr > > {
243
- self . records . get ( peer) . map ( |record| record. addresses ( ) )
242
+ self . records . peek ( peer) . map ( |record| record. addresses ( ) )
244
243
}
245
244
246
245
fn poll ( & mut self , cx : & mut std:: task:: Context < ' _ > ) -> Poll < Self :: Event > {
@@ -257,26 +256,40 @@ impl<T> Store for MemoryStore<T> {
257
256
/// Config for [`MemoryStore`]. The available options are documented via their setters.
258
257
#[ derive( Debug , Clone ) ]
259
258
pub struct Config {
259
+ peer_capacity : NonZeroUsize ,
260
260
record_capacity : NonZeroUsize ,
261
261
remove_addr_on_dial_error : bool ,
262
262
}
263
263
264
264
impl Default for Config {
265
265
fn default ( ) -> Self {
266
266
Self {
267
+ peer_capacity : NonZeroUsize :: try_from ( 1000 ) . expect ( "1000 > 0" ) ,
267
268
record_capacity : NonZeroUsize :: try_from ( 8 ) . expect ( "8 > 0" ) ,
268
269
remove_addr_on_dial_error : true ,
269
270
}
270
271
}
271
272
}
272
273
273
274
impl Config {
275
+ pub fn peer_capacity ( & self ) -> & NonZeroUsize {
276
+ & self . peer_capacity
277
+ }
278
+ /// The capacity of the address store per peer.
279
+ ///
280
+ /// The least recently updated peer will be discarded to make room for a new peer.
281
+ ///
282
+ /// `1000` by default.
283
+ pub fn set_peer_capacity ( mut self , capacity : NonZeroUsize ) -> Self {
284
+ self . peer_capacity = capacity;
285
+ self
286
+ }
274
287
pub fn record_capacity ( & self ) -> & NonZeroUsize {
275
288
& self . record_capacity
276
289
}
277
- /// The capacity of an address store.
290
+ /// The capacity of the address store per peer .
278
291
///
279
- /// The least active address will be discarded to make room for new address.
292
+ /// The least active address will be discarded to make room for a new address.
280
293
///
281
294
/// `8` by default.
282
295
pub fn set_record_capacity ( mut self , capacity : NonZeroUsize ) -> Self {
0 commit comments