2020
2121use crate :: {
2222 codec:: ProtocolCodec ,
23- protocol:: libp2p:: kademlia:: handle:: {
24- IncomingRecordValidationMode , KademliaCommand , KademliaEvent , KademliaHandle ,
25- RoutingTableUpdateMode ,
23+ protocol:: libp2p:: kademlia:: {
24+ handle:: {
25+ IncomingRecordValidationMode , KademliaCommand , KademliaEvent , KademliaHandle ,
26+ RoutingTableUpdateMode ,
27+ } ,
28+ store:: MemoryStoreConfig ,
2629 } ,
2730 types:: protocol:: ProtocolName ,
2831 PeerId , DEFAULT_CHANNEL_SIZE ,
@@ -40,12 +43,27 @@ use std::{
4043/// Default TTL for the records.
4144const DEFAULT_TTL : Duration = Duration :: from_secs ( 36 * 60 * 60 ) ;
4245
43- /// Default provider record TTL.
44- pub ( super ) const DEFAULT_PROVIDER_TTL : Duration = Duration :: from_secs ( 48 * 60 * 60 ) ;
46+ /// Default max number of records.
47+ pub ( super ) const DEFAULT_MAX_RECORDS : usize = 1024 ;
48+
49+ /// Default max record size.
50+ pub ( super ) const DEFAULT_MAX_RECORD_SIZE_BYTES : usize = 65 * 1024 ;
51+
52+ /// Default max provider keys.
53+ pub ( super ) const DEFAULT_MAX_PROVIDER_KEYS : usize = 1024 ;
54+
55+ /// Default max provider addresses.
56+ pub ( super ) const DEFAULT_MAX_PROVIDER_ADDRESSES : usize = 30 ;
57+
58+ /// Default max providers per key.
59+ pub ( super ) const DEFAULT_MAX_PROVIDERS_PER_KEY : usize = 20 ;
4560
4661/// Default provider republish interval.
4762pub ( super ) const DEFAULT_PROVIDER_REFRESH_INTERVAL : Duration = Duration :: from_secs ( 22 * 60 * 60 ) ;
4863
64+ /// Default provider record TTL.
65+ pub ( super ) const DEFAULT_PROVIDER_TTL : Duration = Duration :: from_secs ( 48 * 60 * 60 ) ;
66+
4967/// Protocol name.
5068const PROTOCOL_NAME : & str = "/ipfs/kad/1.0.0" ;
5169
@@ -82,10 +100,7 @@ pub struct Config {
82100 pub ( super ) record_ttl : Duration ,
83101
84102 /// Provider record TTL.
85- pub ( super ) provider_ttl : Duration ,
86-
87- /// Provider republish interval.
88- pub ( super ) provider_refresh_interval : Duration ,
103+ pub ( super ) memory_store_config : MemoryStoreConfig ,
89104
90105 /// TX channel for sending events to `KademliaHandle`.
91106 pub ( super ) event_tx : Sender < KademliaEvent > ,
@@ -105,8 +120,7 @@ impl Config {
105120 update_mode : RoutingTableUpdateMode ,
106121 validation_mode : IncomingRecordValidationMode ,
107122 record_ttl : Duration ,
108- provider_ttl : Duration ,
109- provider_refresh_interval : Duration ,
123+ memory_store_config : MemoryStoreConfig ,
110124 max_message_size : usize ,
111125 ) -> ( Self , KademliaHandle ) {
112126 let ( cmd_tx, cmd_rx) = channel ( DEFAULT_CHANNEL_SIZE ) ;
@@ -124,8 +138,7 @@ impl Config {
124138 update_mode,
125139 validation_mode,
126140 record_ttl,
127- provider_ttl,
128- provider_refresh_interval,
141+ memory_store_config,
129142 codec : ProtocolCodec :: UnsignedVarint ( Some ( max_message_size) ) ,
130143 replication_factor,
131144 known_peers,
@@ -146,8 +159,7 @@ impl Config {
146159 RoutingTableUpdateMode :: Automatic ,
147160 IncomingRecordValidationMode :: Automatic ,
148161 DEFAULT_TTL ,
149- DEFAULT_PROVIDER_TTL ,
150- DEFAULT_PROVIDER_REFRESH_INTERVAL ,
162+ Default :: default ( ) ,
151163 DEFAULT_MAX_MESSAGE_SIZE ,
152164 )
153165 }
@@ -174,11 +186,8 @@ pub struct ConfigBuilder {
174186 /// Default TTL for the records.
175187 pub ( super ) record_ttl : Duration ,
176188
177- /// TTL for the provider records.
178- pub ( super ) provider_ttl : Duration ,
179-
180- /// Republish interval for the provider records.
181- pub ( super ) provider_refresh_interval : Duration ,
189+ /// Memory store configuration.
190+ pub ( super ) memory_store_config : MemoryStoreConfig ,
182191
183192 /// Maximum message size.
184193 pub ( crate ) max_message_size : usize ,
@@ -200,8 +209,7 @@ impl ConfigBuilder {
200209 update_mode : RoutingTableUpdateMode :: Automatic ,
201210 validation_mode : IncomingRecordValidationMode :: Automatic ,
202211 record_ttl : DEFAULT_TTL ,
203- provider_ttl : DEFAULT_PROVIDER_TTL ,
204- provider_refresh_interval : DEFAULT_PROVIDER_REFRESH_INTERVAL ,
212+ memory_store_config : Default :: default ( ) ,
205213 max_message_size : DEFAULT_MAX_MESSAGE_SIZE ,
206214 }
207215 }
@@ -255,19 +263,59 @@ impl ConfigBuilder {
255263 self
256264 }
257265
266+ /// Set maximum number of records in the memory store.
267+ ///
268+ /// If unspecified, the default maximum number of records is 1024.
269+ pub fn with_max_records ( mut self , max_records : usize ) -> Self {
270+ self . memory_store_config . max_records = max_records;
271+ self
272+ }
273+
274+ /// Set maximum record size in bytes.
275+ ///
276+ /// If unspecified, the default maximum record size is 65 KiB.
277+ pub fn with_max_record_size ( mut self , max_record_size_bytes : usize ) -> Self {
278+ self . memory_store_config . max_record_size_bytes = max_record_size_bytes;
279+ self
280+ }
281+
282+ /// Set maximum number of provider keys in the memory store.
283+ ///
284+ /// If unspecified, the default maximum number of provider keys is 1024.
285+ pub fn with_max_provider_keys ( mut self , max_provider_keys : usize ) -> Self {
286+ self . memory_store_config . max_provider_keys = max_provider_keys;
287+ self
288+ }
289+
290+ /// Set maximum number of provider addresses per provider in the memory store.
291+ ///
292+ /// If unspecified, the default maximum number of provider addresses is 30.
293+ pub fn with_max_provider_addresses ( mut self , max_provider_addresses : usize ) -> Self {
294+ self . memory_store_config . max_provider_addresses = max_provider_addresses;
295+ self
296+ }
297+
298+ /// Set maximum number of providers per key in the memory store.
299+ ///
300+ /// If unspecified, the default maximum number of providers per key is 20.
301+ pub fn with_max_providers_per_key ( mut self , max_providers_per_key : usize ) -> Self {
302+ self . memory_store_config . max_providers_per_key = max_providers_per_key;
303+ self
304+ }
305+
258306 /// Set TTL for the provider records. Recommended value is 2 * (refresh interval) + 10%.
259307 ///
260308 /// If unspecified, the default TTL is 48 hours.
261309 pub fn with_provider_record_ttl ( mut self , provider_record_ttl : Duration ) -> Self {
262- self . provider_ttl = provider_record_ttl;
310+ self . memory_store_config . provider_ttl = provider_record_ttl;
263311 self
264312 }
265313
266314 /// Set the refresh (republish) interval for provider records.
267315 ///
268316 /// If unspecified, the default interval is 22 hours.
269317 pub fn with_provider_refresh_interval ( mut self , provider_refresh_interval : Duration ) -> Self {
270- self . provider_refresh_interval = provider_refresh_interval;
318+ self . memory_store_config . provider_refresh_interval = provider_refresh_interval;
271319 self
272320 }
273321
@@ -289,8 +337,7 @@ impl ConfigBuilder {
289337 self . update_mode ,
290338 self . validation_mode ,
291339 self . record_ttl ,
292- self . provider_ttl ,
293- self . provider_refresh_interval ,
340+ self . memory_store_config ,
294341 self . max_message_size ,
295342 )
296343 }
0 commit comments