@@ -52,6 +52,9 @@ const PROTOCOL_NAME: &str = "/ipfs/kad/1.0.0";
52
52
/// Kademlia replication factor.
53
53
const REPLICATION_FACTOR : usize = 20usize ;
54
54
55
+ /// Kademlia maximum message size. Should fit 64 KiB value + 4 KiB key.
56
+ const DEFAULT_MAX_MESSAGE_SIZE : usize = 70 * 1024 ;
57
+
55
58
/// Kademlia configuration.
56
59
#[ derive( Debug ) ]
57
60
pub struct Config {
@@ -104,6 +107,7 @@ impl Config {
104
107
record_ttl : Duration ,
105
108
provider_ttl : Duration ,
106
109
provider_refresh_interval : Duration ,
110
+ max_message_size : usize ,
107
111
) -> ( Self , KademliaHandle ) {
108
112
let ( cmd_tx, cmd_rx) = channel ( DEFAULT_CHANNEL_SIZE ) ;
109
113
let ( event_tx, event_rx) = channel ( DEFAULT_CHANNEL_SIZE ) ;
@@ -122,7 +126,7 @@ impl Config {
122
126
record_ttl,
123
127
provider_ttl,
124
128
provider_refresh_interval,
125
- codec : ProtocolCodec :: UnsignedVarint ( None ) ,
129
+ codec : ProtocolCodec :: UnsignedVarint ( Some ( max_message_size ) ) ,
126
130
replication_factor,
127
131
known_peers,
128
132
cmd_rx,
@@ -144,6 +148,7 @@ impl Config {
144
148
DEFAULT_TTL ,
145
149
DEFAULT_PROVIDER_TTL ,
146
150
DEFAULT_PROVIDER_REFRESH_INTERVAL ,
151
+ DEFAULT_MAX_MESSAGE_SIZE ,
147
152
)
148
153
}
149
154
}
@@ -174,6 +179,9 @@ pub struct ConfigBuilder {
174
179
175
180
/// Republish interval for the provider records.
176
181
pub ( super ) provider_refresh_interval : Duration ,
182
+
183
+ /// Maximum message size.
184
+ pub ( crate ) max_message_size : usize ,
177
185
}
178
186
179
187
impl Default for ConfigBuilder {
@@ -194,6 +202,7 @@ impl ConfigBuilder {
194
202
record_ttl : DEFAULT_TTL ,
195
203
provider_ttl : DEFAULT_PROVIDER_TTL ,
196
204
provider_refresh_interval : DEFAULT_PROVIDER_REFRESH_INTERVAL ,
205
+ max_message_size : DEFAULT_MAX_MESSAGE_SIZE ,
197
206
}
198
207
}
199
208
@@ -262,6 +271,15 @@ impl ConfigBuilder {
262
271
self
263
272
}
264
273
274
+ /// Set the maximum Kademlia message size.
275
+ ///
276
+ /// Should fit `MemoryStore` max record size. If unspecified, the default maximum message size
277
+ /// is 70 KiB.
278
+ pub fn with_max_message_size ( mut self , max_message_size : usize ) -> Self {
279
+ self . max_message_size = max_message_size;
280
+ self
281
+ }
282
+
265
283
/// Build Kademlia [`Config`].
266
284
pub fn build ( self ) -> ( Config , KademliaHandle ) {
267
285
Config :: new (
@@ -273,6 +291,7 @@ impl ConfigBuilder {
273
291
self . record_ttl ,
274
292
self . provider_ttl ,
275
293
self . provider_refresh_interval ,
294
+ self . max_message_size ,
276
295
)
277
296
}
278
297
}
0 commit comments