MCPcopy Create free account
hub / github.com/ermak-dev/cloudpub / wait_for_capacity

Method wait_for_capacity

common/src/data.rs:33–83  ·  view source on GitHub ↗
(&self, required: u32)

Source from the content-addressed store, hash-verified

31 }
32
33 pub async fn wait_for_capacity(&self, required: u32) -> Result<(), mpsc::error::SendError<()>> {
34 loop {
35 if self.closed.load(Ordering::SeqCst) {
36 trace!("{:?} channel is closed, cannot wait for capacity", self);
37 return Err(mpsc::error::SendError(()));
38 }
39 let current = self.capacity.load(Ordering::SeqCst);
40 trace!(
41 "{:?} checking capacity: {} bytes available, required: {} bytes",
42 self,
43 current,
44 required
45 );
46 if current >= required {
47 // Atomically subtract only if capacity hasn't changed
48 match self.capacity.compare_exchange_weak(
49 current,
50 current - required,
51 Ordering::SeqCst,
52 Ordering::SeqCst,
53 ) {
54 Ok(_) => {
55 trace!(
56 "{:?} has sufficient capacity: {} bytes available, consuming {} bytes",
57 self,
58 current,
59 required
60 );
61 return Ok(());
62 }
63 Err(_) => continue, // Retry if another thread modified capacity
64 }
65 }
66
67 trace!(
68 "{:?} insufficient capacity: {} bytes available, waiting for {} more",
69 self,
70 current,
71 required
72 );
73
74 tokio::select! {
75 _ = self.notify.notified() => {
76 trace!("{:?} notified, checking capacity again", self);
77 }
78 _ = self.data_tx.closed() => {
79 return Err(mpsc::error::SendError(()));
80 }
81 }
82 }
83 }
84
85 pub fn close(&self) {
86 trace!("{:?} closing channel", self);

Callers

nothing calls this directly

Calls 1

loadMethod · 0.80

Tested by

no test coverage detected