1use crate::error::{Error, Result};
5use crate::resource::{
6 ResourceCategory, ResourceDimension, ResourceLimitError, ResourceOperation, ResourceStage,
7};
8
9const MIB: u64 = 1024 * 1024;
10const GIB: u64 = 1024 * MIB;
11
12pub const DEFAULT_SAVE_OUTPUT_BYTES: u64 = 512 * MIB;
14pub const DEFAULT_SAVE_XML_PART_BYTES: u64 = 128 * MIB;
16pub const DEFAULT_SAVE_ROWS: u64 = 10_000_000;
18pub const DEFAULT_SAVE_CELLS: u64 = 10_000_000;
20
21pub const DEFAULT_STREAM_ROW_XML_BYTES: u64 = 16 * MIB;
23pub const DEFAULT_STREAM_OUTPUT_BYTES: u64 = 512 * MIB;
25pub const DEFAULT_STREAM_ROWS: u64 = 1_048_576;
27pub const DEFAULT_STREAM_CELLS: u64 = 10_000_000;
29pub const DEFAULT_STREAM_CONFIG_COUNT: u64 = 100_000;
31pub const DEFAULT_STREAM_CONFIG_BYTES: u64 = 16 * MIB;
33
34const HARD_SAVE_OUTPUT_BYTES: u64 = 2 * GIB;
35const HARD_SAVE_XML_PART_BYTES: u64 = 512 * MIB;
36const HARD_SAVE_ROWS: u64 = 100_000_000;
37const HARD_SAVE_CELLS: u64 = 100_000_000;
38const HARD_STREAM_ROW_XML_BYTES: u64 = 128 * MIB;
39const HARD_STREAM_OUTPUT_BYTES: u64 = 2 * GIB;
40const HARD_STREAM_ROWS: u64 = 1_048_576;
41const HARD_STREAM_CELLS: u64 = 100_000_000;
42const HARD_STREAM_CONFIG_COUNT: u64 = 1_000_000;
43const HARD_STREAM_CONFIG_BYTES: u64 = 128 * MIB;
44
45#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47pub struct SaveLimits {
48 pub output_bytes: u64,
50 pub xml_part_bytes: u64,
52 pub row_count: u64,
54 pub cell_count: u64,
56}
57
58impl Default for SaveLimits {
59 fn default() -> Self {
60 Self {
61 output_bytes: DEFAULT_SAVE_OUTPUT_BYTES,
62 xml_part_bytes: DEFAULT_SAVE_XML_PART_BYTES,
63 row_count: DEFAULT_SAVE_ROWS,
64 cell_count: DEFAULT_SAVE_CELLS,
65 }
66 }
67}
68
69impl SaveLimits {
70 pub const fn hard_ceiling() -> Self {
72 Self {
73 output_bytes: HARD_SAVE_OUTPUT_BYTES,
74 xml_part_bytes: HARD_SAVE_XML_PART_BYTES,
75 row_count: HARD_SAVE_ROWS,
76 cell_count: HARD_SAVE_CELLS,
77 }
78 }
79
80 pub fn validate(self) -> Result<Self> {
82 validate_limits(
83 ResourceOperation::WorkbookSave,
84 &[
85 (
86 ResourceDimension::OutputBytes,
87 self.output_bytes,
88 HARD_SAVE_OUTPUT_BYTES,
89 ),
90 (
91 ResourceDimension::XmlBytes,
92 self.xml_part_bytes,
93 HARD_SAVE_XML_PART_BYTES,
94 ),
95 (ResourceDimension::RowCount, self.row_count, HARD_SAVE_ROWS),
96 (
97 ResourceDimension::CellCount,
98 self.cell_count,
99 HARD_SAVE_CELLS,
100 ),
101 ],
102 )?;
103 Ok(self)
104 }
105}
106
107#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
129pub struct SaveOptions {
130 pub durable: bool,
132}
133
134#[derive(Debug, Clone, Copy, PartialEq, Eq)]
136pub struct StreamLimits {
137 pub row_count: u64,
139 pub cell_count: u64,
141 pub row_xml_bytes: u64,
143 pub output_bytes: u64,
145 pub config_count: u64,
147 pub config_bytes: u64,
149}
150
151impl Default for StreamLimits {
152 fn default() -> Self {
153 Self {
154 row_count: DEFAULT_STREAM_ROWS,
155 cell_count: DEFAULT_STREAM_CELLS,
156 row_xml_bytes: DEFAULT_STREAM_ROW_XML_BYTES,
157 output_bytes: DEFAULT_STREAM_OUTPUT_BYTES,
158 config_count: DEFAULT_STREAM_CONFIG_COUNT,
159 config_bytes: DEFAULT_STREAM_CONFIG_BYTES,
160 }
161 }
162}
163
164impl StreamLimits {
165 pub const fn hard_ceiling() -> Self {
167 Self {
168 row_count: HARD_STREAM_ROWS,
169 cell_count: HARD_STREAM_CELLS,
170 row_xml_bytes: HARD_STREAM_ROW_XML_BYTES,
171 output_bytes: HARD_STREAM_OUTPUT_BYTES,
172 config_count: HARD_STREAM_CONFIG_COUNT,
173 config_bytes: HARD_STREAM_CONFIG_BYTES,
174 }
175 }
176
177 pub fn validate(self) -> Result<Self> {
179 validate_limits(
180 ResourceOperation::StreamWrite,
181 &[
182 (
183 ResourceDimension::RowCount,
184 self.row_count,
185 HARD_STREAM_ROWS,
186 ),
187 (
188 ResourceDimension::CellCount,
189 self.cell_count,
190 HARD_STREAM_CELLS,
191 ),
192 (
193 ResourceDimension::XmlBytes,
194 self.row_xml_bytes,
195 HARD_STREAM_ROW_XML_BYTES,
196 ),
197 (
198 ResourceDimension::OutputBytes,
199 self.output_bytes,
200 HARD_STREAM_OUTPUT_BYTES,
201 ),
202 (
203 ResourceDimension::CollectionCount,
204 self.config_count,
205 HARD_STREAM_CONFIG_COUNT,
206 ),
207 (
208 ResourceDimension::RetainedBytes,
209 self.config_bytes,
210 HARD_STREAM_CONFIG_BYTES,
211 ),
212 ],
213 )?;
214 Ok(self)
215 }
216}
217
218pub(crate) fn resource_limit(
219 operation: ResourceOperation,
220 category: ResourceCategory,
221 dimension: ResourceDimension,
222 limit: u64,
223 observed: u64,
224 stage: ResourceStage,
225) -> Error {
226 ResourceLimitError::exceeded(
227 operation,
228 category,
229 dimension,
230 limit,
231 observed.max(limit.saturating_add(1)),
232 Some(stage),
233 )
234 .map(Error::from)
235 .unwrap_or_else(|| Error::Internal("invalid save resource limit state".to_string()))
236}
237
238fn validate_limits(
239 operation: ResourceOperation,
240 limits: &[(ResourceDimension, u64, u64)],
241) -> Result<()> {
242 for &(dimension, value, maximum) in limits {
243 if value == 0 || value > maximum {
244 return Err(resource_limit(
245 operation,
246 ResourceCategory::Output,
247 dimension,
248 maximum,
249 value.max(maximum.saturating_add(1)),
250 ResourceStage::Validate,
251 ));
252 }
253 }
254 Ok(())
255}
256
257#[cfg(test)]
258mod tests {
259 use super::*;
260
261 #[test]
262 fn defaults_are_finite_and_hard_ceilings_are_enforced() {
263 SaveLimits::default().validate().unwrap();
264 StreamLimits::default().validate().unwrap();
265
266 let error = SaveLimits {
267 output_bytes: HARD_SAVE_OUTPUT_BYTES + 1,
268 ..SaveLimits::default()
269 }
270 .validate()
271 .unwrap_err();
272 assert_eq!(
273 error.resource_metadata().unwrap().dimension(),
274 ResourceDimension::OutputBytes
275 );
276
277 let error = StreamLimits {
278 row_xml_bytes: HARD_STREAM_ROW_XML_BYTES + 1,
279 ..StreamLimits::default()
280 }
281 .validate()
282 .unwrap_err();
283 assert_eq!(
284 error.resource_metadata().unwrap().operation(),
285 ResourceOperation::StreamWrite
286 );
287 }
288}