Skip to content

Commit 8618470

Browse files
kad: Expose memory store configuration (#407)
Allow changing Kademlia memory store defaults. Required for #405.
1 parent 954d01b commit 8618470

File tree

4 files changed

+88
-43
lines changed

4 files changed

+88
-43
lines changed

src/crypto/noise/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,7 @@ fn parse_and_verify_peer_id(
770770
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
771771
pub enum HandshakeTransport {
772772
Tcp,
773+
#[cfg(feature = "websocket")]
773774
WebSocket,
774775
}
775776

src/protocol/libp2p/kademlia/config.rs

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020

2121
use 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.
4144
const 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.
4762
pub(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.
5068
const 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
}

src/protocol/libp2p/kademlia/mod.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::{
2929
message::KademliaMessage,
3030
query::{QueryAction, QueryEngine},
3131
routing_table::RoutingTable,
32-
store::{MemoryStore, MemoryStoreAction, MemoryStoreConfig},
32+
store::{MemoryStore, MemoryStoreAction},
3333
types::{ConnectionType, KademliaPeer, Key},
3434
},
3535
Direction, TransportEvent, TransportService,
@@ -185,14 +185,7 @@ impl Kademlia {
185185
service.add_known_address(&peer, addresses.into_iter());
186186
}
187187

188-
let store = MemoryStore::with_config(
189-
local_peer_id,
190-
MemoryStoreConfig {
191-
provider_refresh_interval: config.provider_refresh_interval,
192-
provider_ttl: config.provider_ttl,
193-
..Default::default()
194-
},
195-
);
188+
let store = MemoryStore::with_config(local_peer_id, config.memory_store_config);
196189

197190
Self {
198191
service,
@@ -1311,8 +1304,7 @@ mod tests {
13111304
update_mode: RoutingTableUpdateMode::Automatic,
13121305
validation_mode: IncomingRecordValidationMode::Automatic,
13131306
record_ttl: Duration::from_secs(36 * 60 * 60),
1314-
provider_ttl: Duration::from_secs(48 * 60 * 60),
1315-
provider_refresh_interval: Duration::from_secs(22 * 60 * 60),
1307+
memory_store_config: Default::default(),
13161308
event_tx,
13171309
cmd_rx,
13181310
next_query_id,

src/protocol/libp2p/kademlia/store.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
2323
use crate::{
2424
protocol::libp2p::kademlia::{
25-
config::{DEFAULT_PROVIDER_REFRESH_INTERVAL, DEFAULT_PROVIDER_TTL},
25+
config::{
26+
DEFAULT_MAX_PROVIDERS_PER_KEY, DEFAULT_MAX_PROVIDER_ADDRESSES,
27+
DEFAULT_MAX_PROVIDER_KEYS, DEFAULT_MAX_RECORDS, DEFAULT_MAX_RECORD_SIZE_BYTES,
28+
DEFAULT_PROVIDER_REFRESH_INTERVAL, DEFAULT_PROVIDER_TTL,
29+
},
2630
record::{ContentProvider, Key, ProviderRecord, Record},
2731
types::Key as KademliaKey,
2832
},
@@ -352,6 +356,7 @@ impl MemoryStore {
352356
}
353357
}
354358

359+
#[derive(Debug)]
355360
pub struct MemoryStoreConfig {
356361
/// Maximum number of records to store.
357362
pub max_records: usize,
@@ -379,11 +384,11 @@ pub struct MemoryStoreConfig {
379384
impl Default for MemoryStoreConfig {
380385
fn default() -> Self {
381386
Self {
382-
max_records: 1024,
383-
max_record_size_bytes: 65 * 1024,
384-
max_provider_keys: 1024,
385-
max_provider_addresses: 30,
386-
max_providers_per_key: 20,
387+
max_records: DEFAULT_MAX_RECORDS,
388+
max_record_size_bytes: DEFAULT_MAX_RECORD_SIZE_BYTES,
389+
max_provider_keys: DEFAULT_MAX_PROVIDER_KEYS,
390+
max_provider_addresses: DEFAULT_MAX_PROVIDER_ADDRESSES,
391+
max_providers_per_key: DEFAULT_MAX_PROVIDERS_PER_KEY,
387392
provider_refresh_interval: DEFAULT_PROVIDER_REFRESH_INTERVAL,
388393
provider_ttl: DEFAULT_PROVIDER_TTL,
389394
}

0 commit comments

Comments
 (0)