| 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 progress( |
| 12 | &mut self, |
| 13 | _stage: Stage, |
| 14 | _bytes: u64, |
| 15 | _total: Option<u64>, |
| 16 | _entries: u32, |
| 17 | ) -> Result<()> { |
| 18 | self.check() |
| 19 | } |
| 20 | fn activity(&mut self) -> Result<()> { |
| 21 | self.check() |
| 22 | } |
| 23 | fn observe(&mut self, _usage: ResourceUsage, _storage: Option<&std::fs::File>) -> Result<()> { |
| 24 | self.check() |
| 25 | } |
| 26 | } |
| 27 | impl<F: FnMut() -> Result<()>> Monitor for F { |
| 28 | fn check(&mut self) -> Result<()> { |
| 29 | self() |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | pub(crate) fn completed<T>(result: Result<T>, check: &mut impl Monitor) -> Result<T> { |
| 34 | let value = result?; |
| 35 | check.activity()?; |
| 36 | Ok(value) |
| 37 | } |
| 38 | |
| 39 | pub(crate) struct MetadataMonitor<'a, M>(pub &'a mut M); |
| 40 | impl<M: Monitor> Monitor for MetadataMonitor<'_, M> { |
| 41 | fn metadata_limits(&self) -> crate::archive::MetadataLimits { |
| 42 | self.0.metadata_limits() |
| 43 | } |
| 44 | fn check(&mut self) -> Result<()> { |
| 45 | self.0.check() |
| 46 | } |
| 47 | fn activity(&mut self) -> Result<()> { |
| 48 | self.0.activity() |
| 49 | } |
| 50 | fn observe(&mut self, usage: ResourceUsage, _storage: Option<&std::fs::File>) -> Result<()> { |
| 51 | self.check()?; |
| 52 | // 압축 헤더는 메타데이터 상한만 적용하며 파일 출력량에 합치지 않음. |
| 53 | if usage |
| 54 | .estimated_bytes |
| 55 | .into_iter() |
| 56 | .chain(usage.actual_bytes) |
| 57 | .any(|bytes| bytes > self.metadata_limits().bytes as u64) |
| 58 | { |
| 59 | return Err(ErrorCode::Limit); |
| 60 | } |
| 61 | Ok(()) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | pub(crate) struct Budget { |
| 66 | pub soft: Limits, |
| 67 | pub ceiling: Limits, |
| 68 | pub granted: ResourceScope, |
| 69 | } |
| 70 | impl Budget { |
| 71 | pub fn metadata_limits(&self) -> crate::archive::MetadataLimits { |
| 72 | if self.granted.any() { |
| 73 | crate::archive::MetadataLimits::approved() |
| 74 | } else { |
| 75 | crate::archive::MetadataLimits::default() |
| 76 | } |
| 77 | } |
| 78 | pub fn required(&self, usage: ResourceUsage) -> Result<ResourceScope> { |
| 79 | let bytes = usage |
| 80 | .estimated_bytes |
| 81 | .into_iter() |
| 82 | .chain(usage.actual_bytes) |
| 83 | .max(); |
| 84 | let entries = usage |
| 85 | .estimated_entries |
| 86 | .into_iter() |
| 87 | .chain(usage.actual_entries) |
| 88 | .max(); |
| 89 | if bytes.is_some_and(|n| n > self.ceiling.output) |
| 90 | || entries.is_some_and(|n| n > self.ceiling.entries) |
| 91 | { |
| 92 | return Err(ErrorCode::Limit); |
| 93 | } |
| 94 | let ratio = match (usage.ratio_output_bytes, usage.compressed_bytes) { |
| 95 | (Some(output), Some(input)) => { |
| 96 | output >= 256 * 1024 * 1024 && u128::from(output) > u128::from(input) * 1000 |
| 97 | } |
| 98 | _ => false, |
| 99 | }; |
| 100 | Ok(ResourceScope { |
| 101 | output: !self.granted.output && bytes.is_some_and(|n| n > self.soft.output), |
| 102 | entries: !self.granted.entries && entries.is_some_and(|n| n > self.soft.entries), |
| 103 | ratio: !self.granted.ratio && ratio, |
| 104 | }) |
| 105 | } |
| 106 | pub fn grant(&mut self, scope: ResourceScope) { |
| 107 | self.granted.output |= scope.output; |
| 108 | self.granted.entries |= scope.entries; |
| 109 | self.granted.ratio |= scope.ratio; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | pub(crate) struct Watchdog { |
| 114 | last_activity: Duration, |
| 115 | approved: bool, |
| 116 | } |
| 117 | impl Watchdog { |
| 118 | pub fn new() -> Self { |
| 119 | Self { |
| 120 | last_activity: Duration::ZERO, |
| 121 | approved: false, |
| 122 | } |
| 123 | } |
| 124 | pub fn check(&self, elapsed: Duration) -> Result<()> { |
| 125 | if if self.approved { |
| 126 | elapsed.saturating_sub(self.last_activity) >= Duration::from_secs(60) |
| 127 | } else { |
| 128 | elapsed > Duration::from_secs(300) |
| 129 | } { |
| 130 | Err(ErrorCode::Limit) |
| 131 | } else { |
| 132 | Ok(()) |
| 133 | } |
| 134 | } |
| 135 | pub fn activity(&mut self, elapsed: Duration) -> Result<()> { |
| 136 | self.check(elapsed)?; |
| 137 | self.last_activity = elapsed; |
| 138 | Ok(()) |
| 139 | } |
| 140 | pub fn resume(&mut self, elapsed: Duration, approved: bool) { |
| 141 | self.approved |= approved; |
| 142 | self.last_activity = elapsed; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | #[cfg(test)] |
| 147 | #[path = "../../../tests/rust-archive-worker/resource_budget.rs"] |
| 148 | mod tests; |