Commit eb355553 authored by Ian Jackson's avatar Ian Jackson
Browse files

DownloadSchudule: Have NetworkConfig contain Builders

Use sub_builder.  We must do something special for defaults.

This involves moving the actual default values for retry_bootstrap and
retry_microdescs into config.rs, since they need to access the fields
of the un-built version of the structure.  (An alternative would be to
generate "weak setters" which do not override previous settings, but
derive_builder does not offer to generate them and that seems
overkill.)
parent f4a1e34b
Loading
Loading
Loading
Loading
+8 −17
Original line number Original line Diff line number Diff line
@@ -384,7 +384,6 @@ mod test {


    #[test]
    #[test]
    fn builder() {
    fn builder() {
        use tor_dirmgr::DownloadSchedule;
        let sec = std::time::Duration::from_secs(1);
        let sec = std::time::Duration::from_secs(1);


        let auth = dir::Authority::builder()
        let auth = dir::Authority::builder()
@@ -403,22 +402,14 @@ mod test {
        bld.storage()
        bld.storage()
            .cache_dir(CfgPath::new("/var/tmp/foo".to_owned()))
            .cache_dir(CfgPath::new("/var/tmp/foo".to_owned()))
            .state_dir(CfgPath::new("/var/tmp/bar".to_owned()));
            .state_dir(CfgPath::new("/var/tmp/bar".to_owned()));
        bld.download_schedule().retry_certs(
        bld.download_schedule().retry_certs().attempts(10);
            DownloadSchedule::builder()
        bld.download_schedule().retry_certs().initial_delay(sec);
                .attempts(10)
        bld.download_schedule().retry_certs().parallelism(3);
                .initial_delay(sec)
        bld.download_schedule().retry_microdescs().attempts(30);
                .parallelism(3)
        bld.download_schedule()
                .build()
            .retry_microdescs()
                .expect("build schedule"),
            .initial_delay(10 * sec);
        );
        bld.download_schedule().retry_microdescs().parallelism(9);
        bld.download_schedule().retry_microdescs(
            DownloadSchedule::builder()
                .attempts(30)
                .initial_delay(10 * sec)
                .parallelism(9)
                .build()
                .expect("build schedule"),
        );
        bld.override_net_params()
        bld.override_net_params()
            .insert("wombats-per-quokka".to_owned(), 7);
            .insert("wombats-per-quokka".to_owned(), 7);
        bld.path_rules()
        bld.path_rules()
+18 −17
Original line number Original line Diff line number Diff line
@@ -222,7 +222,6 @@ mod test {


    #[test]
    #[test]
    fn builder() {
    fn builder() {
        use arti_client::config::dir::DownloadSchedule;
        use tor_config::CfgPath;
        use tor_config::CfgPath;
        let sec = std::time::Duration::from_secs(1);
        let sec = std::time::Duration::from_secs(1);


@@ -248,22 +247,24 @@ mod test {
            .storage()
            .storage()
            .cache_dir(CfgPath::new("/var/tmp/foo".to_owned()))
            .cache_dir(CfgPath::new("/var/tmp/foo".to_owned()))
            .state_dir(CfgPath::new("/var/tmp/bar".to_owned()));
            .state_dir(CfgPath::new("/var/tmp/bar".to_owned()));
        bld.tor().download_schedule().retry_certs(
        bld.tor().download_schedule().retry_certs().attempts(10);
            DownloadSchedule::builder()
        bld.tor()
                .attempts(10)
            .download_schedule()
                .initial_delay(sec)
            .retry_certs()
                .parallelism(3)
            .initial_delay(sec);
                .build()
        bld.tor().download_schedule().retry_certs().parallelism(3);
                .expect("build download schedule"),
        bld.tor()
        );
            .download_schedule()
        bld.tor().download_schedule().retry_microdescs(
            .retry_microdescs()
            DownloadSchedule::builder()
            .attempts(30);
                .attempts(30)
        bld.tor()
                .initial_delay(10 * sec)
            .download_schedule()
                .parallelism(9)
            .retry_microdescs()
                .build()
            .initial_delay(10 * sec);
                .expect("build download schedule"),
        bld.tor()
        );
            .download_schedule()
            .retry_microdescs()
            .parallelism(9);
        bld.tor()
        bld.tor()
            .override_net_params()
            .override_net_params()
            .insert("wombats-per-quokka".to_owned(), 7);
            .insert("wombats-per-quokka".to_owned(), 7);
+27 −62
Original line number Original line Diff line number Diff line
@@ -19,7 +19,6 @@ use tor_netdoc::doc::netstatus;
use derive_builder::Builder;
use derive_builder::Builder;
use serde::Deserialize;
use serde::Deserialize;
use std::path::PathBuf;
use std::path::PathBuf;
use std::time::Duration;


/// Configuration information about the Tor network itself; used as
/// Configuration information about the Tor network itself; used as
/// part of Arti's configuration.
/// part of Arti's configuration.
@@ -111,46 +110,32 @@ impl NetworkConfigBuilder {
#[builder(derive(Deserialize))]
#[builder(derive(Deserialize))]
pub struct DownloadScheduleConfig {
pub struct DownloadScheduleConfig {
    /// Top-level configuration for how to retry our initial bootstrap attempt.
    /// Top-level configuration for how to retry our initial bootstrap attempt.
    #[serde(default = "default_retry_bootstrap")]
    #[builder(
    #[builder(default = "default_retry_bootstrap()")]
        sub_builder,
        field(build = "self.retry_bootstrap.build_retry_bootstrap()?")
    )]
    #[builder_field_attr(serde(default))]
    retry_bootstrap: DownloadSchedule,
    retry_bootstrap: DownloadSchedule,


    /// Configuration for how to retry a consensus download.
    /// Configuration for how to retry a consensus download.
    #[serde(default)]
    #[builder(sub_builder)]
    #[builder(default)]
    #[builder_field_attr(serde(default))]
    retry_consensus: DownloadSchedule,
    retry_consensus: DownloadSchedule,


    /// Configuration for how to retry an authority cert download.
    /// Configuration for how to retry an authority cert download.
    #[serde(default)]
    #[builder(sub_builder)]
    #[builder(default)]
    #[builder_field_attr(serde(default))]
    retry_certs: DownloadSchedule,
    retry_certs: DownloadSchedule,


    /// Configuration for how to retry a microdescriptor download.
    /// Configuration for how to retry a microdescriptor download.
    #[serde(default = "default_microdesc_schedule")]
    #[builder(
    #[builder(default = "default_microdesc_schedule()")]
        sub_builder,
        field(build = "self.retry_microdescs.build_retry_microdescs()?")
    )]
    #[builder_field_attr(serde(default))]
    retry_microdescs: DownloadSchedule,
    retry_microdescs: DownloadSchedule,
}
}


/// Default value for retry_bootstrap in DownloadScheduleConfig.
fn default_retry_bootstrap() -> DownloadSchedule {
    DownloadScheduleBuilder::default()
        .attempts(128)
        .initial_delay(Duration::new(1, 0))
        .parallelism(1)
        .build()
        .expect("build default_retry_bootstrap")
}

/// Default value for microdesc_bootstrap in DownloadScheduleConfig.
fn default_microdesc_schedule() -> DownloadSchedule {
    let mut ds = DownloadSchedule::builder();
    ds.attempts(3)
        .initial_delay(Duration::new(1, 0))
        .parallelism(4);
    ds.build()
        .expect("failed to build default microdesc schedule")
}

impl Default for DownloadScheduleConfig {
impl Default for DownloadScheduleConfig {
    fn default() -> Self {
    fn default() -> Self {
        Self::builder()
        Self::builder()
@@ -399,39 +384,19 @@ mod test {
        assert_eq!(cfg.retry_microdescs().n_attempts(), 3);
        assert_eq!(cfg.retry_microdescs().n_attempts(), 3);
        assert_eq!(cfg.retry_bootstrap().n_attempts(), 128);
        assert_eq!(cfg.retry_bootstrap().n_attempts(), 128);


        bld.retry_consensus(
        bld.retry_consensus().attempts(7);
            DownloadSchedule::builder()
        bld.retry_consensus().initial_delay(Duration::new(86400, 0));
                .attempts(7)
        bld.retry_consensus().parallelism(1);
                .initial_delay(Duration::new(86400, 0))
        bld.retry_bootstrap().attempts(4);
                .parallelism(1)
        bld.retry_bootstrap().initial_delay(Duration::new(3600, 0));
                .build()
        bld.retry_bootstrap().parallelism(1);
                .unwrap(),

        );
        bld.retry_certs().attempts(5);
        bld.retry_bootstrap(
        bld.retry_certs().initial_delay(Duration::new(3600, 0));
            DownloadSchedule::builder()
        bld.retry_certs().parallelism(1);
                .attempts(4)
        bld.retry_microdescs().attempts(6);
                .initial_delay(Duration::new(3600, 0))
        bld.retry_microdescs().initial_delay(Duration::new(3600, 0));
                .parallelism(1)
        bld.retry_microdescs().parallelism(1);
                .build()
                .unwrap(),
        );

        bld.retry_certs(
            DownloadSchedule::builder()
                .attempts(5)
                .initial_delay(Duration::new(3600, 0))
                .parallelism(1)
                .build()
                .unwrap(),
        );
        bld.retry_microdescs(
            DownloadSchedule::builder()
                .attempts(6)
                .initial_delay(Duration::new(3600, 0))
                .parallelism(1)
                .build()
                .unwrap(),
        );


        let cfg = bld.build().unwrap();
        let cfg = bld.build().unwrap();
        assert_eq!(cfg.retry_microdescs().parallelism(), 1);
        assert_eq!(cfg.retry_microdescs().parallelism(), 1);
+21 −0
Original line number Original line Diff line number Diff line
@@ -49,6 +49,27 @@ pub struct DownloadSchedule {
    parallelism: NonZeroU8,
    parallelism: NonZeroU8,
}
}


impl DownloadScheduleBuilder {
    /// Default value for retry_bootstrap in DownloadScheduleConfig.
    pub fn build_retry_bootstrap(&self) -> Result<DownloadSchedule, ConfigBuildError> {
        let mut bld = self.clone();
        bld.num_retries.get_or_insert(128);
        bld.initial_delay.get_or_insert_with(|| Duration::new(1, 0));
        bld.parallelism.get_or_insert(1);
        bld.build()
    }

    /// Default value for microdesc_bootstrap in DownloadScheduleConfig.
    pub fn build_retry_microdescs(&self) -> Result<DownloadSchedule, ConfigBuildError> {
        let mut bld = self.clone();
        bld.num_retries.get_or_insert(3);
        bld.initial_delay
            .get_or_insert_with(|| (Duration::new(1, 0)));
        bld.parallelism.get_or_insert(4);
        bld.build()
    }
}

impl Default for DownloadSchedule {
impl Default for DownloadSchedule {
    fn default() -> Self {
    fn default() -> Self {
        DownloadSchedule::builder()
        DownloadSchedule::builder()