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