coven_foundation/files/
file_budget.rs1use std::{io, num::NonZeroUsize, path::Path, sync::Arc};
13use tokio::sync::{OwnedSemaphorePermit, Semaphore};
14
15#[path = "file_budget_tally.rs"]
16mod tally;
17#[cfg(any(test, feature = "test-utils"))]
18pub use tally::FileBudgetWatch;
19use tally::Tally;
20
21const UNLIMITED_DESCRIPTORS: u64 = 10240;
24
25#[derive(Clone)]
29pub struct FileBudget {
30 shared: Arc<BudgetShared>,
31}
32
33struct BudgetShared {
34 places: Arc<Semaphore>,
35 size: NonZeroUsize,
36 tally: Tally,
37}
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
42#[error("a lease of {requested} places asks for more than the budget's {size}")]
43pub struct LeaseTooLarge {
44 pub requested: usize,
46 pub size: usize,
48}
49
50#[derive(Clone)]
54pub struct FileLease {
55 shared: Arc<LeaseShared>,
56}
57
58struct LeaseShared {
59 places: Arc<Semaphore>,
60 count: usize,
61 budget: Arc<BudgetShared>,
62 _held: OwnedSemaphorePermit,
64}
65
66pub struct LeasePlace {
70 _permit: OwnedSemaphorePermit,
72 lease: Arc<LeaseShared>,
73}
74
75pub struct LeasedFile<F> {
78 file: F,
81 _place: LeasePlace,
82}
83
84impl FileBudget {
85 pub fn new(size: NonZeroUsize) -> Self {
88 let size = size.min(NonZeroUsize::new(u32::MAX as usize).expect("u32::MAX is not zero"));
89 Self {
90 shared: Arc::new(BudgetShared {
91 places: Arc::new(Semaphore::new(size.get())),
92 size,
93 tally: Tally::new(),
94 }),
95 }
96 }
97
98 pub fn under_descriptor_limit() -> Self {
110 Self::new(size_for_limit(soft_descriptor_limit()))
111 }
112
113 pub fn size(&self) -> usize {
115 self.shared.size.get()
116 }
117
118 pub async fn lease(&self, count: usize) -> Result<FileLease, LeaseTooLarge> {
123 let size = self.size();
124 let wanted = u32::try_from(count)
125 .ok()
126 .filter(|_| count <= size)
127 .ok_or(LeaseTooLarge { requested: count, size })?;
128 let mut acquire = Box::pin(self.shared.places.clone().acquire_many_owned(wanted));
132 let held = match futures_lite::future::poll_once(&mut acquire).await {
133 Some(held) => held,
134 None => {
135 self.shared.tally.waited();
136 acquire.await
137 }
138 }
139 .expect("the budget's semaphore is never closed");
140 Ok(self.granted(count, held))
141 }
142
143 pub fn lease_blocking(&self, count: usize) -> Result<FileLease, LeaseTooLarge> {
147 futures_lite::future::block_on(self.lease(count))
148 }
149
150 fn granted(&self, count: usize, held: OwnedSemaphorePermit) -> FileLease {
151 self.shared.tally.leased(count);
152 FileLease {
153 shared: Arc::new(LeaseShared {
154 places: Arc::new(Semaphore::new(count)),
155 count,
156 budget: self.shared.clone(),
157 _held: held,
158 }),
159 }
160 }
161
162 #[cfg(any(test, feature = "test-utils"))]
164 pub fn watch(&self) -> FileBudgetWatch {
165 FileBudgetWatch::of(self.shared.clone())
166 }
167}
168
169#[cfg(any(test, feature = "in-memory-keychain"))]
171const TEST_PLACES: usize = 64;
172
173#[cfg(any(test, feature = "in-memory-keychain"))]
174impl FileBudget {
175 pub fn for_test() -> Self {
178 Self::new(NonZeroUsize::new(TEST_PLACES).expect("64 is not zero"))
179 }
180}
181
182#[cfg(any(test, feature = "test-utils"))]
183impl FileLease {
184 pub async fn for_test() -> Self {
187 FileBudget::for_test().lease(TEST_PLACES).await.expect("a test budget holds its own size")
188 }
189}
190
191#[cfg(any(test, feature = "test-utils"))]
192impl LeasePlace {
193 pub async fn for_test() -> Self {
196 FileLease::for_test().await.place().await
197 }
198}
199
200impl std::fmt::Debug for FileBudget {
201 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
202 f.debug_struct("FileBudget").field("size", &self.shared.size).finish_non_exhaustive()
203 }
204}
205
206impl FileLease {
207 pub async fn place(&self) -> LeasePlace {
210 let permit = self
211 .shared
212 .places
213 .clone()
214 .acquire_owned()
215 .await
216 .expect("a lease's semaphore is never closed");
217 LeasePlace::taken(permit, self.shared.clone())
218 }
219
220 pub fn place_blocking(&self) -> LeasePlace {
223 futures_lite::future::block_on(self.place())
224 }
225
226 pub fn count(&self) -> usize {
228 self.shared.count
229 }
230}
231
232impl std::fmt::Debug for FileLease {
233 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
234 f.debug_struct("FileLease").field("count", &self.shared.count).finish_non_exhaustive()
235 }
236}
237
238impl Drop for LeaseShared {
239 fn drop(&mut self) {
240 self.budget.tally.returned(self.count);
241 }
242}
243
244impl LeasePlace {
245 fn taken(permit: OwnedSemaphorePermit, lease: Arc<LeaseShared>) -> Self {
246 lease.budget.tally.holding();
247 Self { _permit: permit, lease }
248 }
249
250 pub fn open(self, path: &Path) -> io::Result<LeasedFile<std::fs::File>> {
253 Ok(LeasedFile { file: std::fs::File::open(path)?, _place: self })
254 }
255
256 pub async fn open_async(self, path: &Path) -> io::Result<LeasedFile<tokio::fs::File>> {
258 Ok(LeasedFile { file: tokio::fs::File::open(path).await?, _place: self })
259 }
260}
261
262impl Drop for LeasePlace {
263 fn drop(&mut self) {
264 self.lease.budget.tally.released();
265 }
266}
267
268impl<F> std::ops::Deref for LeasedFile<F> {
269 type Target = F;
270 fn deref(&self) -> &F {
271 &self.file
272 }
273}
274
275impl<F> std::ops::DerefMut for LeasedFile<F> {
276 fn deref_mut(&mut self) -> &mut F {
277 &mut self.file
278 }
279}
280
281fn soft_descriptor_limit() -> Option<u64> {
284 #[cfg(unix)]
285 {
286 rustix::process::getrlimit(rustix::process::Resource::Nofile).current
287 }
288 #[cfg(not(unix))]
289 {
290 None
291 }
292}
293
294fn size_for_limit(limit: Option<u64>) -> NonZeroUsize {
297 let places = limit.unwrap_or(UNLIMITED_DESCRIPTORS) / 8;
298 let places = usize::try_from(places).unwrap_or(usize::MAX);
299 NonZeroUsize::new(places).unwrap_or(NonZeroUsize::MIN)
300}
301
302#[cfg(test)]
303#[path = "file_budget_tests.rs"]
304mod tests;