fluxus_core/error_handling/
backpressure.rs1use std::time::Duration;
2
3#[derive(Debug, Clone)]
5pub enum BackpressureStrategy {
6 Block,
8 DropOldest,
10 DropNewest,
12 Throttle {
14 high_watermark: usize,
15 low_watermark: usize,
16 backoff: Duration,
17 },
18}
19
20pub struct BackpressureController {
22 strategy: BackpressureStrategy,
23 current_load: usize,
24}
25
26impl BackpressureController {
27 pub fn new(strategy: BackpressureStrategy) -> Self {
29 Self {
30 strategy,
31 current_load: 0,
32 }
33 }
34
35 pub fn should_apply_backpressure(&self) -> bool {
37 match &self.strategy {
38 BackpressureStrategy::Block => self.current_load > 0,
39 BackpressureStrategy::DropOldest | BackpressureStrategy::DropNewest => false,
40 BackpressureStrategy::Throttle { high_watermark, .. } => {
41 self.current_load >= *high_watermark
42 }
43 }
44 }
45
46 pub fn get_backoff(&self) -> Option<Duration> {
48 match &self.strategy {
49 BackpressureStrategy::Throttle { backoff, .. } => Some(*backoff),
50 _ => None,
51 }
52 }
53
54 pub fn update_load(&mut self, load: usize) {
56 self.current_load = load;
57 }
58
59 pub fn can_accept(&self) -> bool {
61 !self.should_apply_backpressure()
62 }
63}