diff --git a/Cargo.lock b/Cargo.lock index 57e0342..4bcb33f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1249,6 +1249,12 @@ version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +[[package]] +name = "libm" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" + [[package]] name = "libredox" version = "0.1.3" @@ -1291,6 +1297,7 @@ dependencies = [ "openssl", "parking_lot", "rand", + "rand_distr", "regex", "reqwest", "rlimit", @@ -1655,6 +1662,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" dependencies = [ "autocfg", + "libm", ] [[package]] @@ -1937,6 +1945,16 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rand_distr" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32cb0b9bc82b0a0876c2dd994a7e7a2683d3e7390ca40e6886785ef0c7e3ee31" +dependencies = [ + "num-traits", + "rand", +] + [[package]] name = "redox_syscall" version = "0.4.1" diff --git a/crates/librqbit/Cargo.toml b/crates/librqbit/Cargo.toml index 75db80e..9a237b0 100644 --- a/crates/librqbit/Cargo.toml +++ b/crates/librqbit/Cargo.toml @@ -71,6 +71,7 @@ bytes = "1.5.0" rlimit = "0.10.1" async-stream = "0.3.5" memmap2 = "0.9.4" +rand_distr = "0.4.3" [dev-dependencies] futures = { version = "0.3" } diff --git a/crates/librqbit/src/storage/slow.rs b/crates/librqbit/src/storage/slow.rs index 9443a1d..c4e0397 100644 --- a/crates/librqbit/src/storage/slow.rs +++ b/crates/librqbit/src/storage/slow.rs @@ -1,6 +1,7 @@ use std::time::Duration; use rand::Rng; +use rand_distr::Distribution; use super::{StorageFactory, TorrentStorage}; @@ -30,9 +31,23 @@ pub struct SlowStorage { underlying: U, } +fn random_duration() -> Duration { + use rand_distr::StandardNormal; + + let s = StandardNormal {}; + + let sl: f64 = s.sample(&mut rand::thread_rng()); + // let sl = Duration::from_secs_f64(sl); + // tracing::trace!(duration = ?sl, "sleeping"); + // std::thread::sleep(sl) + // + let micros = 340f64 + sl * 200.; + let micros = micros.max(0.001) * 2.; + Duration::from_micros(micros as u64) +} + fn random_sleep() { - let sl = rand::thread_rng().gen_range(0f64..0.1f64); - let sl = Duration::from_secs_f64(sl); + let sl = random_duration(); tracing::trace!(duration = ?sl, "sleeping"); std::thread::sleep(sl) } diff --git a/crates/rqbit/src/main.rs b/crates/rqbit/src/main.rs index 793bae5..40d1ca5 100644 --- a/crates/rqbit/src/main.rs +++ b/crates/rqbit/src/main.rs @@ -8,7 +8,8 @@ use librqbit::{ http_api::{HttpApi, HttpApiOptions}, http_api_client, librqbit_spawn, storage::{ - filesystem::FilesystemStorageFactory, timing::TimingStorageFactory, StorageFactoryExt, + filesystem::FilesystemStorageFactory, slow::SlowStorageFactory, + timing::TimingStorageFactory, StorageFactoryExt, }, tracing_subscriber_config_utils::{init_logging, InitLoggingOptions}, AddTorrent, AddTorrentOptions, AddTorrentResponse, Api, ListOnlyResponse, @@ -382,7 +383,7 @@ async fn async_main(opts: Opts) -> anyhow::Result<()> { disable_trackers: download_opts.disable_trackers, storage_factory: Some({ let sf = FilesystemStorageFactory::default(); - // let sf = SlowStorageFactory::new(sf); + let sf = SlowStorageFactory::new(sf); TimingStorageFactory::new("fs".to_owned(), sf).boxed() }), ..Default::default()