settings.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 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. use structopt::StructOpt;
  19. #[derive(Clone, Debug)]
  20. pub struct DhtSettings {
  21. /// Number of nodes in a bucket
  22. pub k: usize,
  23. /// Number of lookup requests in a burst
  24. pub alpha: usize,
  25. /// Maximum number of parallel lookup requests
  26. pub concurrency: usize,
  27. /// Timeout in seconds
  28. pub timeout: u64,
  29. /// Timeout in seconds for inbound connections
  30. pub inbound_timeout: u64,
  31. }
  32. impl Default for DhtSettings {
  33. fn default() -> Self {
  34. Self { k: 16, alpha: 4, concurrency: 10, timeout: 5, inbound_timeout: 30 }
  35. }
  36. }
  37. #[derive(Clone, Debug, serde::Deserialize, structopt::StructOpt, structopt_toml::StructOptToml)]
  38. #[structopt()]
  39. #[serde(rename = "dht")]
  40. pub struct DhtSettingsOpt {
  41. /// Number of nodes in a DHT bucket
  42. #[structopt(long)]
  43. pub dht_k: Option<usize>,
  44. /// Number of DHT lookup requests in a burst
  45. #[structopt(long)]
  46. pub dht_alpha: Option<usize>,
  47. /// Maximum number of parallel DHT lookup requests
  48. #[structopt(long)]
  49. pub dht_concurrency: Option<usize>,
  50. /// Timeout in seconds
  51. #[structopt(long)]
  52. pub dht_timeout: Option<u64>,
  53. /// Timeout in seconds for inbound connections
  54. #[structopt(long)]
  55. pub dht_inbound_timeout: Option<u64>,
  56. }
  57. impl From<DhtSettingsOpt> for DhtSettings {
  58. fn from(opt: DhtSettingsOpt) -> Self {
  59. let def = DhtSettings::default();
  60. Self {
  61. k: opt.dht_k.unwrap_or(def.k),
  62. alpha: opt.dht_alpha.unwrap_or(def.alpha),
  63. concurrency: opt.dht_concurrency.unwrap_or(def.concurrency),
  64. timeout: opt.dht_timeout.unwrap_or(def.timeout),
  65. inbound_timeout: opt.dht_inbound_timeout.unwrap_or(def.inbound_timeout),
  66. }
  67. }
  68. }