Skip to content

Commit

Permalink
update config
Browse files Browse the repository at this point in the history
  • Loading branch information
neonphog committed Dec 18, 2024
1 parent 879bda6 commit f28ec8c
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 92 deletions.
1 change: 1 addition & 0 deletions crates/api/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl Builder {
kitsune,
space,
peer_store,
bootstrap,
fetch,
transport,
} = &mut self;
Expand Down
12 changes: 6 additions & 6 deletions crates/core/src/factories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ pub use mem_peer_store::MemPeerStoreFactory;
#[cfg(test)]
pub(crate) use mem_peer_store::test_utils;

mod mem_bootstrap;
pub use mem_bootstrap::*;
pub mod mem_bootstrap;
pub use mem_bootstrap::MemBootstrapFactory;

mod core_bootstrap;
pub use core_bootstrap::*;
pub mod core_bootstrap;
pub use core_bootstrap::CoreBootstrapFactory;

mod core_fetch;
pub use core_fetch::*;
pub mod core_fetch;
pub use core_fetch::CoreFetchFactory;

mod mem_transport;
pub use mem_transport::*;
88 changes: 48 additions & 40 deletions crates/core/src/factories/core_bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,56 @@
use kitsune2_api::{bootstrap::*, config::*, *};
use std::sync::Arc;

const MOD_NAME: &str = "CoreBootstrap";

/// Configuration parameters for [CoreBootstrapFactory].
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CoreBootstrapConfig {
/// The url of the kitsune2 bootstrap server. E.g. `https://boot.kitsu.ne`.
pub server_url: String,

/// Minimum backoff in ms to use for both push and poll retry loops.
/// Default: 5 seconds.
pub backoff_min_ms: u32,

/// Maximum backoff in ms to use for both push and poll retry loops.
/// Default: 5 minutes.
pub backoff_max_ms: u32,
}
/// CoreBootstrap configuration types.
pub mod config {
/// Configuration parameters for [CoreBootstrapFactory](super::CoreBootstrapFactory).
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CoreBootstrapConfig {
/// The url of the kitsune2 bootstrap server. E.g. `https://boot.kitsu.ne`.
pub server_url: String,

/// Minimum backoff in ms to use for both push and poll retry loops.
/// Default: 5 seconds.
pub backoff_min_ms: u32,

/// Maximum backoff in ms to use for both push and poll retry loops.
/// Default: 5 minutes.
pub backoff_max_ms: u32,
}

impl Default for CoreBootstrapConfig {
fn default() -> Self {
Self {
server_url: "<https://your.bootstrap.url>".into(),
backoff_min_ms: 1000 * 5,
backoff_max_ms: 1000 * 60 * 5,
impl Default for CoreBootstrapConfig {
fn default() -> Self {
Self {
server_url: "<https://your.bootstrap.url>".into(),
backoff_min_ms: 1000 * 5,
backoff_max_ms: 1000 * 60 * 5,
}
}
}
}

impl CoreBootstrapConfig {
/// Get the minimum backoff duration.
pub fn backoff_min(&self) -> std::time::Duration {
std::time::Duration::from_millis(self.backoff_min_ms as u64)
impl CoreBootstrapConfig {
/// Get the minimum backoff duration.
pub fn backoff_min(&self) -> std::time::Duration {
std::time::Duration::from_millis(self.backoff_min_ms as u64)
}

/// Get the maximum backoff duration.
pub fn backoff_max(&self) -> std::time::Duration {
std::time::Duration::from_millis(self.backoff_max_ms as u64)
}
}

/// Get the maximum backoff duration.
pub fn backoff_max(&self) -> std::time::Duration {
std::time::Duration::from_millis(self.backoff_max_ms as u64)
/// Module-level configuration for CoreBootstrap.
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CoreBootstrapModConfig {
/// CoreBootstrap configuration.
pub core_bootstrap: CoreBootstrapConfig,
}
}

impl ModConfig for CoreBootstrapConfig {}
use config::*;

/// The core bootstrap implementation provided by Kitsune2.
#[derive(Debug)]
Expand All @@ -59,10 +68,7 @@ impl CoreBootstrapFactory {

impl BootstrapFactory for CoreBootstrapFactory {
fn default_config(&self, config: &mut Config) -> K2Result<()> {
config.add_default_module_config::<CoreBootstrapConfig>(
MOD_NAME.into(),
)?;
Ok(())
config.set_module_config(&CoreBootstrapModConfig::default())
}

fn create(
Expand All @@ -72,11 +78,13 @@ impl BootstrapFactory for CoreBootstrapFactory {
space: SpaceId,
) -> BoxFut<'static, K2Result<DynBootstrap>> {
Box::pin(async move {
let config = builder
.config
.get_module_config::<CoreBootstrapConfig>(MOD_NAME)?;
let config: CoreBootstrapModConfig =
builder.config.get_module_config()?;
let out: DynBootstrap = Arc::new(CoreBootstrap::new(
builder, config, peer_store, space,
builder,
config.core_bootstrap,
peer_store,
space,
));
Ok(out)
})
Expand Down
14 changes: 7 additions & 7 deletions crates/core/src/factories/core_bootstrap/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ struct Test {

impl Test {
pub async fn new(server: &str) -> Self {
let mut builder = builder::Builder {
let builder = builder::Builder {
verifier: Arc::new(TestCrypto),
bootstrap: super::CoreBootstrapFactory::create(),
..crate::default_builder()
};
builder.set_default_config().unwrap();
}
.with_default_config()
.unwrap();
builder
.config
.set_module_config(
super::MOD_NAME.into(),
&super::CoreBootstrapConfig {
.set_module_config(&super::CoreBootstrapModConfig {
core_bootstrap: super::CoreBootstrapConfig {
server_url: server.into(),
backoff_min_ms: 10,
backoff_max_ms: 10,
},
)
})
.unwrap();
let builder = Arc::new(builder);
println!("{}", serde_json::to_string(&builder.config).unwrap());
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/factories/core_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
//! - persisting ops to the data store
//! - removing op ids from in-memory data object
//!
//! ### State object [CoreFetch]
//! ### State object CoreFetch
//!
//! - Exposes public method [CoreFetch::add_ops] that takes a list of op ids and an agent id.
//! - Exposes public method CoreFetch::add_ops that takes a list of op ids and an agent id.
//! - Stores pairs of ([OpId][AgentId]) in a set.
//! - A hash set is used to look up elements by key efficiently. Ops may be added redundantly
//! to the set with different sources to fetch from, so the set is keyed by op and agent id together.
Expand Down
68 changes: 37 additions & 31 deletions crates/core/src/factories/mem_bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,44 @@
use kitsune2_api::{bootstrap::*, config::*, *};
use std::sync::{Arc, Mutex};

const MOD_NAME: &str = "MemBootstrap";

/// Configuration parameters for [MemBootstrapFactory].
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MemBootstrapConfig {
/// Since rust test runs multiple tests in the same process,
/// we cannot just have a single global bootstrap test store.
/// This defaults to the current thread id when this config instance
/// is constructed. This should be sufficient for most needs.
/// However, if you are creating kitsune nodes in tests from
/// different tasks, you may need to pick an explicit id for this value.
pub test_id: String,

/// How often in ms to update the peer store with bootstrap infos.
/// Defaults to 5s.
pub poll_freq_ms: u32,
}
/// MemBootstrap configuration types.
pub mod config {
/// Configuration parameters for [MemBootstrapFactory](super::MemBootstrapFactory).
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MemBootstrapConfig {
/// Since rust test runs multiple tests in the same process,
/// we cannot just have a single global bootstrap test store.
/// This defaults to the current thread id when this config instance
/// is constructed. This should be sufficient for most needs.
/// However, if you are creating kitsune nodes in tests from
/// different tasks, you may need to pick an explicit id for this value.
pub test_id: String,

/// How often in ms to update the peer store with bootstrap infos.
/// Defaults to 5s.
pub poll_freq_ms: u32,
}

impl Default for MemBootstrapConfig {
fn default() -> Self {
Self {
test_id: format!("{:?}", std::thread::current().id()),
poll_freq_ms: 5000,
impl Default for MemBootstrapConfig {
fn default() -> Self {
Self {
test_id: format!("{:?}", std::thread::current().id()),
poll_freq_ms: 5000,
}
}
}

/// Module-level configuration for MemBootstrap.
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MemBootstrapModConfig {
/// MemBootstrap configuration.
pub mem_bootstrap: MemBootstrapConfig,
}
}

impl ModConfig for MemBootstrapConfig {}
use config::*;

/// The mem bootstrap implementation provided by Kitsune2.
#[derive(Debug)]
Expand All @@ -53,9 +62,7 @@ impl MemBootstrapFactory {

impl BootstrapFactory for MemBootstrapFactory {
fn default_config(&self, config: &mut Config) -> K2Result<()> {
config
.add_default_module_config::<MemBootstrapConfig>(MOD_NAME.into())?;
Ok(())
config.set_module_config(&MemBootstrapModConfig::default())
}

fn create(
Expand All @@ -65,11 +72,10 @@ impl BootstrapFactory for MemBootstrapFactory {
_space: SpaceId,
) -> BoxFut<'static, K2Result<DynBootstrap>> {
Box::pin(async move {
let config = builder
.config
.get_module_config::<MemBootstrapConfig>(MOD_NAME)?;
let config: MemBootstrapModConfig =
builder.config.get_module_config()?;
let out: DynBootstrap =
Arc::new(MemBootstrap::new(config, peer_store));
Arc::new(MemBootstrap::new(config.mem_bootstrap, peer_store));
Ok(out)
})
}
Expand Down
14 changes: 8 additions & 6 deletions crates/core/src/factories/mem_bootstrap/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ struct Test {

impl Test {
pub async fn new() -> Self {
let mut builder = builder::Builder {
verifier: Arc::new(TestCrypto),
..crate::default_builder()
};
builder.set_default_config().unwrap();
let builder = Arc::new(builder);
let builder = Arc::new(
builder::Builder {
verifier: Arc::new(TestCrypto),
..crate::default_builder()
}
.with_default_config()
.unwrap(),
);
println!("{}", serde_json::to_string(&builder.config).unwrap());

let peer_store =
Expand Down

0 comments on commit f28ec8c

Please sign in to comment.