-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathbackpressure.rs
More file actions
75 lines (67 loc) · 2.16 KB
/
Copy pathbackpressure.rs
File metadata and controls
75 lines (67 loc) · 2.16 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::time::Duration;
use serde::Serialize;
#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
pub enum IngestionStrategy {
/// Full speed ingestion
Normal,
/// Slowed ingestion with artificial delays
Throttled,
/// Stopped ingestion until health improves
Paused,
}
#[derive(Debug, Clone, Serialize)]
pub struct BackpressureStatus {
pub strategy: IngestionStrategy,
pub db_latency_ms: u64,
pub queue_depth: usize,
}
pub struct BackpressureController {
degraded_latency_ms: u64,
critical_latency_ms: u64,
degraded_queue_size: usize,
critical_queue_size: usize,
throttle_delay_ms: u64,
}
impl BackpressureController {
pub fn new(
degraded_latency: u64,
critical_latency: u64,
degraded_queue: usize,
critical_queue: usize,
throttle_delay: u64,
) -> Self {
Self {
degraded_latency_ms: degraded_latency,
critical_latency_ms: critical_latency,
degraded_queue_size: degraded_queue,
critical_queue_size: critical_queue,
throttle_delay_ms: throttle_delay,
}
}
pub fn calculate_status(&self, latency: u64, queue_depth: usize) -> BackpressureStatus {
let strategy = if latency >= self.critical_latency_ms || queue_depth >= self.critical_queue_size {
IngestionStrategy::Paused
} else if latency >= self.degraded_latency_ms || queue_depth >= self.degraded_queue_size {
IngestionStrategy::Throttled
} else {
IngestionStrategy::Normal
};
BackpressureStatus {
strategy,
db_latency_ms: latency,
queue_depth,
}
}
pub fn get_delay(&self, strategy: IngestionStrategy) -> Duration {
match strategy {
IngestionStrategy::Normal => Duration::from_millis(0),
IngestionStrategy::Throttled => Duration::from_millis(self.throttle_delay_ms),
IngestionStrategy::Paused => Duration::from_secs(5), // Re-check interval when paused
}
}
}
impl Default for BackpressureController {
fn default() -> Self {
Self::new(200, 1000, 5000, 10000, 500)
}
}