primitives/correlated_randomness/stream/buffered/
config.rs1use std::{sync::Arc, time::Duration};
6
7use blanket::blanket;
8use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
9
10use crate::correlated_randomness::stream::CorrelatedStreamError;
11
12#[derive(Debug, Clone)]
14pub struct BufferConfig {
15 capacity: usize,
17 max_request_size: usize,
19 refill_threshold: usize,
21}
22
23impl BufferConfig {
24 pub const UNBOUNDED: usize = usize::MAX;
27
28 pub fn eager(capacity: usize) -> Self {
32 assert!(capacity > 0, "capacity must be greater than 0");
33 Self {
34 capacity,
35 max_request_size: capacity,
36 refill_threshold: capacity,
37 }
38 }
39
40 pub fn eager_with(capacity: usize, max_request_size: usize) -> Self {
43 assert!(capacity > 0, "capacity must be greater than 0");
44 Self {
45 capacity,
46 max_request_size: max_request_size.min(capacity),
47 refill_threshold: capacity,
48 }
49 }
50
51 pub fn lazy(capacity: usize, refill_threshold: usize) -> Self {
56 assert!(capacity > 0, "capacity must be greater than 0");
57 Self {
58 capacity,
59 max_request_size: capacity,
60 refill_threshold: refill_threshold.min(capacity),
61 }
62 }
63
64 pub fn lazy_with(capacity: usize, refill_threshold: usize, max_request_size: usize) -> Self {
70 assert!(capacity > 0, "capacity must be greater than 0");
71 Self {
72 capacity,
73 max_request_size: max_request_size.min(capacity),
74 refill_threshold: refill_threshold.min(capacity),
75 }
76 }
77
78 pub fn shared(self) -> SharedBufferConfig {
80 Arc::new(RwLock::new(self))
81 }
82
83 #[inline]
85 pub fn capacity(&self) -> usize {
86 self.capacity
87 }
88
89 #[inline]
91 pub fn max_request_size(&self) -> usize {
92 self.max_request_size
93 }
94
95 #[inline]
97 pub fn refill_threshold(&self) -> usize {
98 self.refill_threshold
99 }
100
101 pub fn set_capacity(&mut self, capacity: usize) {
107 assert!(capacity > 0, "capacity must be greater than 0");
108 self.capacity = capacity;
109 self.max_request_size = self.max_request_size.min(capacity);
110 self.refill_threshold = self.refill_threshold.min(capacity);
111 }
112
113 pub fn set_max_request_size(&mut self, max_request_size: usize) {
115 self.max_request_size = max_request_size.min(self.capacity);
116 }
117
118 pub fn set_refill_threshold(&mut self, refill_threshold: usize) {
120 self.refill_threshold = refill_threshold.min(self.capacity);
121 }
122}
123
124pub const LOCK_TIMEOUT: Duration = Duration::from_millis(500);
132
133#[inline]
136pub fn try_read_config(
137 m: &RwLock<BufferConfig>,
138) -> Result<RwLockReadGuard<'_, BufferConfig>, CorrelatedStreamError> {
139 m.try_read_for(LOCK_TIMEOUT)
140 .ok_or(CorrelatedStreamError::LockTimeout {
141 timeout_ms: LOCK_TIMEOUT.as_millis() as u64,
142 })
143}
144
145#[inline]
148pub fn try_write_config(
149 m: &RwLock<BufferConfig>,
150) -> Result<RwLockWriteGuard<'_, BufferConfig>, CorrelatedStreamError> {
151 m.try_write_for(LOCK_TIMEOUT)
152 .ok_or(CorrelatedStreamError::LockTimeout {
153 timeout_ms: LOCK_TIMEOUT.as_millis() as u64,
154 })
155}
156
157pub type SharedBufferConfig = Arc<RwLock<BufferConfig>>;
159
160#[blanket(derive(Arc, Ref, Mut))]
167pub trait Buffer {
168 fn config(&self) -> &SharedBufferConfig;
170
171 #[inline]
172 fn capacity(&self) -> Result<usize, CorrelatedStreamError> {
173 Ok(try_read_config(self.config())?.capacity())
174 }
175 #[inline]
176 fn max_request_size(&self) -> Result<usize, CorrelatedStreamError> {
177 Ok(try_read_config(self.config())?.max_request_size())
178 }
179 #[inline]
180 fn refill_threshold(&self) -> Result<usize, CorrelatedStreamError> {
181 Ok(try_read_config(self.config())?.refill_threshold())
182 }
183
184 #[inline]
185 fn set_capacity(&self, capacity: usize) -> Result<(), CorrelatedStreamError> {
186 try_write_config(self.config())?.set_capacity(capacity);
187 Ok(())
188 }
189 #[inline]
190 fn set_max_request_size(&self, n: usize) -> Result<(), CorrelatedStreamError> {
191 try_write_config(self.config())?.set_max_request_size(n);
192 Ok(())
193 }
194 #[inline]
195 fn set_refill_threshold(&self, n: usize) -> Result<(), CorrelatedStreamError> {
196 try_write_config(self.config())?.set_refill_threshold(n);
197 Ok(())
198 }
199}
200
201impl Buffer for SharedBufferConfig {
203 fn config(&self) -> &SharedBufferConfig {
204 self
205 }
206}
207
208impl Buffer for [SharedBufferConfig] {
212 fn config(&self) -> &SharedBufferConfig {
213 self.first().expect("Buffer config slice must be non-empty")
214 }
215 fn set_capacity(&self, capacity: usize) -> Result<(), CorrelatedStreamError> {
216 self.iter().try_for_each(|c| {
217 try_write_config(c)?.set_capacity(capacity);
218 Ok(())
219 })
220 }
221 fn set_max_request_size(&self, n: usize) -> Result<(), CorrelatedStreamError> {
222 self.iter().try_for_each(|c| {
223 try_write_config(c)?.set_max_request_size(n);
224 Ok(())
225 })
226 }
227 fn set_refill_threshold(&self, n: usize) -> Result<(), CorrelatedStreamError> {
228 self.iter().try_for_each(|c| {
229 try_write_config(c)?.set_refill_threshold(n);
230 Ok(())
231 })
232 }
233}