darkfi/dht/
settings.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2026 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use structopt::StructOpt;
20
21#[derive(Clone, Debug)]
22pub struct DhtSettings {
23    /// Number of nodes in a bucket
24    pub k: usize,
25    /// Number of lookup requests in a burst
26    pub alpha: usize,
27    /// Maximum number of parallel lookup requests
28    pub concurrency: usize,
29    /// Timeout in seconds
30    pub timeout: u64,
31    /// Timeout in seconds for inbound connections
32    pub inbound_timeout: u64,
33}
34
35impl Default for DhtSettings {
36    fn default() -> Self {
37        Self { k: 16, alpha: 4, concurrency: 10, timeout: 5, inbound_timeout: 30 }
38    }
39}
40
41#[derive(Clone, Debug, serde::Deserialize, structopt::StructOpt, structopt_toml::StructOptToml)]
42#[structopt()]
43#[serde(rename = "dht")]
44pub struct DhtSettingsOpt {
45    /// Number of nodes in a DHT bucket
46    #[structopt(long)]
47    pub dht_k: Option<usize>,
48
49    /// Number of DHT lookup requests in a burst
50    #[structopt(long)]
51    pub dht_alpha: Option<usize>,
52
53    /// Maximum number of parallel DHT lookup requests
54    #[structopt(long)]
55    pub dht_concurrency: Option<usize>,
56
57    /// Timeout in seconds
58    #[structopt(long)]
59    pub dht_timeout: Option<u64>,
60
61    /// Timeout in seconds for inbound connections
62    #[structopt(long)]
63    pub dht_inbound_timeout: Option<u64>,
64}
65
66impl From<DhtSettingsOpt> for DhtSettings {
67    fn from(opt: DhtSettingsOpt) -> Self {
68        let def = DhtSettings::default();
69
70        Self {
71            k: opt.dht_k.unwrap_or(def.k),
72            alpha: opt.dht_alpha.unwrap_or(def.alpha),
73            concurrency: opt.dht_concurrency.unwrap_or(def.concurrency),
74            timeout: opt.dht_timeout.unwrap_or(def.timeout),
75            inbound_timeout: opt.dht_inbound_timeout.unwrap_or(def.inbound_timeout),
76        }
77    }
78}