Skip to main content

tycho_network/dht/
config.rs

1use std::time::Duration;
2
3use bytesize::ByteSize;
4use serde::{Deserialize, Serialize};
5use tycho_util::serde_helpers;
6
7// TODO: add max storage item size
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(default)]
10pub struct DhtConfig {
11    /// DHT K parameter.
12    ///
13    /// Default: 6.
14    pub max_k: usize,
15
16    /// Maximum time to live for peer info.
17    ///
18    /// Default: 1 hour.
19    #[serde(with = "serde_helpers::humantime")]
20    pub max_peer_info_ttl: Duration,
21
22    /// Maximum time to live for stored values.
23    ///
24    /// Default: 1 hour.
25    #[serde(with = "serde_helpers::humantime")]
26    pub max_stored_value_ttl: Duration,
27
28    /// Maximum storage capacity (number of entries).
29    ///
30    /// Default: 16 MiB.
31    pub max_storage_capacity: ByteSize,
32
33    /// Time until a stored item is considered idle and can be removed.
34    ///
35    /// Default: unlimited.
36    #[serde(with = "serde_helpers::humantime")]
37    pub storage_item_time_to_idle: Option<Duration>,
38
39    /// A period of refreshing the local peer info.
40    ///
41    /// Default: 1 minute.
42    #[serde(with = "serde_helpers::humantime")]
43    pub local_info_refresh_period: Duration,
44
45    /// A period of storing the local peer info into the DHT.
46    ///
47    /// Default: 10 minutes.
48    #[serde(with = "serde_helpers::humantime")]
49    pub local_info_announce_period: Duration,
50
51    /// A maximum value of a random jitter for the peer announce period.
52    ///
53    /// Default: 1 minute.
54    #[serde(with = "serde_helpers::humantime")]
55    pub local_info_announce_period_max_jitter: Duration,
56
57    /// A period of updating and populating the routing table.
58    ///
59    /// Default: 10 minutes.
60    #[serde(with = "serde_helpers::humantime")]
61    pub routing_table_refresh_period: Duration,
62
63    /// A maximum value of a random jitter for the routing table refresh period.
64    ///
65    /// Default: 1 minutes.
66    #[serde(with = "serde_helpers::humantime")]
67    pub routing_table_refresh_period_max_jitter: Duration,
68
69    /// The capacity of the announced peers channel.
70    ///
71    /// Default: 10.
72    pub announced_peers_channel_capacity: usize,
73
74    /// A period of refilling bootstrap peers in the routing table.
75    ///
76    /// Default: 1 minutes.
77    #[serde(with = "serde_helpers::humantime")]
78    pub bootstrap_peers_refill_period: Option<Duration>,
79
80    /// Timeout for DHT query request.
81    ///
82    /// Default: 500 ms.
83    #[serde(with = "serde_helpers::humantime")]
84    pub request_timeout: Duration,
85}
86
87impl Default for DhtConfig {
88    fn default() -> Self {
89        Self {
90            max_k: 6,
91            max_peer_info_ttl: Duration::from_secs(3600),
92            max_stored_value_ttl: Duration::from_secs(3600),
93            max_storage_capacity: ByteSize::mib(16),
94            storage_item_time_to_idle: None,
95            local_info_refresh_period: Duration::from_secs(60),
96            local_info_announce_period: Duration::from_secs(600),
97            local_info_announce_period_max_jitter: Duration::from_secs(60),
98            routing_table_refresh_period: Duration::from_secs(600),
99            routing_table_refresh_period_max_jitter: Duration::from_secs(60),
100            announced_peers_channel_capacity: 10,
101            bootstrap_peers_refill_period: Some(Duration::from_secs(60)),
102            request_timeout: Duration::from_millis(500),
103        }
104    }
105}