diff options
author | Alex Auvolat <alex@adnab.me> | 2022-06-23 17:05:11 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2022-06-23 17:05:11 +0200 |
commit | 59b43914d4a9ae9a50ae79fee61b1a46bff941f9 (patch) | |
tree | 5ca8cdeb754b47a855d142891e41214b77e88c52 /src/util/tranquilizer.rs | |
parent | f82cf164f5ab525560415355503c04315901a739 (diff) | |
download | garage-59b43914d4a9ae9a50ae79fee61b1a46bff941f9.tar.gz garage-59b43914d4a9ae9a50ae79fee61b1a46bff941f9.zip |
(to test) error reporting and throttling at higher layer
Diffstat (limited to 'src/util/tranquilizer.rs')
-rw-r--r-- | src/util/tranquilizer.rs | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/src/util/tranquilizer.rs b/src/util/tranquilizer.rs index 28711387..f0c2b410 100644 --- a/src/util/tranquilizer.rs +++ b/src/util/tranquilizer.rs @@ -3,6 +3,8 @@ use std::time::{Duration, Instant}; use tokio::time::sleep; +use crate::background::WorkerStatus; + /// A tranquilizer is a helper object that is used to make /// background operations not take up too much time. /// @@ -33,7 +35,7 @@ impl Tranquilizer { } } - pub async fn tranquilize(&mut self, tranquility: u32) { + fn tranquilize_internal(&mut self, tranquility: u32) -> Option<Duration> { let observation = Instant::now() - self.last_step_begin; self.observations.push_back(observation); @@ -45,10 +47,25 @@ impl Tranquilizer { if !self.observations.is_empty() { let delay = (tranquility * self.sum_observations) / (self.observations.len() as u32); + Some(delay) + } else { + None + } + } + + pub async fn tranquilize(&mut self, tranquility: u32) { + if let Some(delay) = self.tranquilize_internal(tranquility) { sleep(delay).await; + self.reset(); } + } - self.reset(); + #[must_use] + pub fn tranquilize_worker(&mut self, tranquility: u32) -> WorkerStatus { + match self.tranquilize_internal(tranquility) { + Some(delay) => WorkerStatus::Throttled(delay.as_secs_f32()), + None => WorkerStatus::Busy, + } } pub fn reset(&mut self) { |