blob: 8e3b98eb17ba9231b99f1f4475499c2b92fd9148 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
use std::sync::atomic::{AtomicUsize, Ordering};
use karyon_core::async_util::CondWait;
/// Manages available inbound and outbound slots.
pub struct ConnectionSlots {
/// A condvar for notifying when a slot become available.
signal: CondWait,
/// The number of occupied slots
slots: AtomicUsize,
/// The maximum number of slots.
max_slots: usize,
}
impl ConnectionSlots {
/// Creates a new ConnectionSlots
pub fn new(max_slots: usize) -> Self {
Self {
signal: CondWait::new(),
slots: AtomicUsize::new(0),
max_slots,
}
}
/// Increases the occupied slots by one.
pub fn add(&self) {
self.slots.fetch_add(1, Ordering::SeqCst);
}
/// Decreases the occupied slots by one and notifies the waiting signal
/// to start accepting/connecting new connections.
pub async fn remove(&self) {
self.slots.fetch_sub(1, Ordering::SeqCst);
if self.slots.load(Ordering::SeqCst) < self.max_slots {
self.signal.signal().await;
}
}
/// Waits for a slot to become available.
pub async fn wait_for_slot(&self) {
if self.slots.load(Ordering::SeqCst) < self.max_slots {
return;
}
// Wait for a signal
self.signal.wait().await;
self.signal.reset().await;
}
}
|