| 1 | use crate::protocol::*; |
| 2 | use std::time::Duration; |
| 3 | |
| 4 | type Result<T> = std::result::Result<T, ErrorCode>; |
| 5 | |
| 6 | pub trait Monitor { |
| 7 | fn check(&mut self) -> Result<()>; |
| 8 | fn activity(&mut self) -> Result<()> { |
| 9 | self.check() |
| 10 | } |
| 11 | fn observe(&mut self, _usage: ResourceUsage, _storage: Option<&std::fs::File>) -> Result<()> { |
| 12 | self.check() |
| 13 | } |
| 14 | } |
| 15 | impl<F: FnMut() -> Result<()>> Monitor for F { |
| 16 | fn check(&mut self) -> Result<()> { |
| 17 | self() |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | pub(crate) struct Budget { |
| 22 | pub soft: Limits, |
| 23 | pub ceiling: Limits, |
| 24 | pub granted: ResourceScope, |
| 25 | } |
| 26 | impl Budget { |
| 27 | pub fn required(&self, usage: ResourceUsage) -> Result<ResourceScope> { |
| 28 | let bytes = usage |
| 29 | .estimated_bytes |
| 30 | .into_iter() |
| 31 | .chain(usage.actual_bytes) |
| 32 | .max(); |
| 33 | let entries = usage |
| 34 | .estimated_entries |
| 35 | .into_iter() |
| 36 | .chain(usage.actual_entries) |
| 37 | .max(); |
| 38 | if bytes.is_some_and(|n| n > self.ceiling.output) |
| 39 | || entries.is_some_and(|n| n > self.ceiling.entries) |
| 40 | { |
| 41 | return Err(ErrorCode::Limit); |
| 42 | } |
| 43 | let ratio = match (usage.ratio_output_bytes, usage.compressed_bytes) { |
| 44 | (Some(output), Some(input)) => { |
| 45 | output >= 256 * 1024 * 1024 && u128::from(output) > u128::from(input) * 1000 |
| 46 | } |
| 47 | _ => false, |
| 48 | }; |
| 49 | Ok(ResourceScope { |
| 50 | output: !self.granted.output && bytes.is_some_and(|n| n > self.soft.output), |
| 51 | entries: !self.granted.entries && entries.is_some_and(|n| n > self.soft.entries), |
| 52 | ratio: !self.granted.ratio && ratio, |
| 53 | }) |
| 54 | } |
| 55 | pub fn grant(&mut self, scope: ResourceScope) { |
| 56 | self.granted.output |= scope.output; |
| 57 | self.granted.entries |= scope.entries; |
| 58 | self.granted.ratio |= scope.ratio; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pub(crate) struct Watchdog { |
| 63 | last_activity: Duration, |
| 64 | approved: bool, |
| 65 | } |
| 66 | impl Watchdog { |
| 67 | pub fn new() -> Self { |
| 68 | Self { |
| 69 | last_activity: Duration::ZERO, |
| 70 | approved: false, |
| 71 | } |
| 72 | } |
| 73 | pub fn check(&self, elapsed: Duration) -> Result<()> { |
| 74 | if if self.approved { |
| 75 | elapsed.saturating_sub(self.last_activity) >= Duration::from_secs(60) |
| 76 | } else { |
| 77 | elapsed > Duration::from_secs(300) |
| 78 | } { |
| 79 | Err(ErrorCode::Limit) |
| 80 | } else { |
| 81 | Ok(()) |
| 82 | } |
| 83 | } |
| 84 | pub fn activity(&mut self, elapsed: Duration) -> Result<()> { |
| 85 | self.check(elapsed)?; |
| 86 | self.last_activity = elapsed; |
| 87 | Ok(()) |
| 88 | } |
| 89 | pub fn resume(&mut self, elapsed: Duration, approved: bool) { |
| 90 | self.approved |= approved; |
| 91 | self.last_activity = elapsed; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | #[cfg(test)] |
| 96 | #[path = "../../../tests/rust-archive-worker/resource_budget.rs"] |
| 97 | mod tests; |