coven_foundation/files/
file_budget_tally.rs1#[cfg(any(test, feature = "test-utils"))]
5use super::BudgetShared;
6#[cfg(any(test, feature = "test-utils"))]
7use std::sync::Arc;
8
9#[cfg(any(test, feature = "test-utils"))]
10#[derive(Default)]
11struct Counts {
12 leased: usize,
13 most_leased: usize,
14 holding: usize,
15 most_holding: usize,
16 waits: usize,
17}
18
19pub(super) struct Tally {
22 #[cfg(any(test, feature = "test-utils"))]
23 counts: tokio::sync::watch::Sender<Counts>,
24}
25
26impl Tally {
27 pub(super) fn new() -> Self {
28 Self {
29 #[cfg(any(test, feature = "test-utils"))]
30 counts: tokio::sync::watch::channel(Counts::default()).0,
31 }
32 }
33
34 pub(super) fn waited(&self) {
36 #[cfg(any(test, feature = "test-utils"))]
37 self.counts.send_modify(|counts| counts.waits += 1);
38 }
39
40 pub(super) fn leased(&self, count: usize) {
42 #[cfg(any(test, feature = "test-utils"))]
43 self.counts.send_modify(|counts| {
44 counts.leased += count;
45 counts.most_leased = counts.most_leased.max(counts.leased);
46 });
47 #[cfg(not(any(test, feature = "test-utils")))]
48 let _ = count;
49 }
50
51 pub(super) fn returned(&self, count: usize) {
53 #[cfg(any(test, feature = "test-utils"))]
54 self.counts.send_modify(|counts| counts.leased -= count);
55 #[cfg(not(any(test, feature = "test-utils")))]
56 let _ = count;
57 }
58
59 pub(super) fn holding(&self) {
61 #[cfg(any(test, feature = "test-utils"))]
62 self.counts.send_modify(|counts| {
63 counts.holding += 1;
64 counts.most_holding = counts.most_holding.max(counts.holding);
65 });
66 }
67
68 pub(super) fn released(&self) {
70 #[cfg(any(test, feature = "test-utils"))]
71 self.counts.send_modify(|counts| counts.holding -= 1);
72 }
73}
74
75#[cfg(any(test, feature = "test-utils"))]
79#[derive(Clone)]
80pub struct FileBudgetWatch {
81 shared: Arc<BudgetShared>,
82}
83
84#[cfg(any(test, feature = "test-utils"))]
85impl FileBudgetWatch {
86 pub(super) fn of(shared: Arc<BudgetShared>) -> Self {
87 Self { shared }
88 }
89 pub fn leased(&self) -> usize {
91 self.shared.tally.counts.borrow().leased
92 }
93 pub fn most_leased(&self) -> usize {
95 self.shared.tally.counts.borrow().most_leased
96 }
97 pub fn holding(&self) -> usize {
100 self.shared.tally.counts.borrow().holding
101 }
102 pub fn most_holding(&self) -> usize {
104 self.shared.tally.counts.borrow().most_holding
105 }
106 pub fn lease_waits_from_now(
109 &self,
110 count: usize,
111 ) -> impl std::future::Future<Output = ()> + Send + 'static {
112 let mut counts = self.shared.tally.counts.subscribe();
113 let now = counts.borrow_and_update().waits;
114 async move {
115 crate::clock::within(counts.wait_for(|counts| counts.waits >= now + count))
116 .await
117 .expect("the lease wait")
118 .expect("the budget is kept");
119 }
120 }
121 pub fn empty(&self) -> impl std::future::Future<Output = ()> + Send + 'static {
125 let mut counts = self.shared.tally.counts.subscribe();
126 async move {
127 crate::clock::within(counts.wait_for(|counts| counts.leased == 0))
128 .await
129 .expect("the budget empties")
130 .expect("the budget is kept");
131 }
132 }
133}