Skip to content

Commit 342ef5f

Browse files
authored
feat: rejection duration after threshold breach (#1783)
1 parent 91183b6 commit 342ef5f

2 files changed

Lines changed: 50 additions & 16 deletions

File tree

‎src/cli.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,15 @@ pub struct Options {
508508
)]
509509
pub resource_check_interval: u64,
510510

511+
#[arg(
512+
long,
513+
env = "P_REJECTION_DURATION",
514+
default_value = "30",
515+
value_parser = validation::validate_seconds,
516+
help = "Post threshold breach, reject requests for"
517+
)]
518+
pub rejection_duration: u64,
519+
511520
#[arg(
512521
long,
513522
env = "P_MEMORY_THRESHOLD",

‎src/handlers/http/resource_check.rs‎

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,27 @@ const PROCESS_METRICS_SAMPLE_INTERVAL: Duration = Duration::from_secs(5);
4040

4141
static SERVER_OK: LazyLock<Arc<AtomicBool>> = LazyLock::new(|| Arc::new(AtomicBool::new(true)));
4242

43+
async fn sample_process_metrics() {
44+
refresh_sys_info();
45+
let process_metrics = tokio::task::spawn_blocking(|| {
46+
let sys = SYS_INFO.lock().unwrap();
47+
let total_mem = if let Some(cgroup) = sys.cgroup_limits() {
48+
cgroup.total_memory
49+
} else {
50+
sys.total_memory()
51+
};
52+
sysinfo::get_current_pid()
53+
.ok()
54+
.and_then(|pid| sys.process(pid))
55+
.map(|process| (process.cpu_usage() as f64, process.memory(), total_mem))
56+
})
57+
.await
58+
.unwrap();
59+
if let Some((cpu_usage, memory_bytes, total_mem)) = process_metrics {
60+
record_process_metrics_sample(cpu_usage, memory_bytes, total_mem);
61+
}
62+
}
63+
4364
/// Spawn a background task to monitor system resources
4465
pub fn spawn_resource_monitor(shutdown_rx: tokio::sync::oneshot::Receiver<()>) {
4566
tokio::spawn(async move {
@@ -80,25 +101,29 @@ pub fn spawn_resource_monitor(shutdown_rx: tokio::sync::oneshot::Receiver<()>) {
80101
info!("Resource utilization back to normal - requests will be accepted");
81102
} else {
82103
warn!("Resource utilization too high - requests will be rejected");
104+
105+
// sleep for rejection duration (+ the check interval)
106+
let rejection_delay = tokio::time::sleep(Duration::from_secs(
107+
PARSEABLE.options.rejection_duration,
108+
));
109+
tokio::pin!(rejection_delay);
110+
111+
loop {
112+
select! {
113+
_ = &mut rejection_delay => break,
114+
_ = process_metrics_interval.tick() => {
115+
sample_process_metrics().await;
116+
},
117+
_ = &mut shutdown_rx => {
118+
trace!("Resource monitor shutting down");
119+
return;
120+
}
121+
}
122+
}
83123
}
84124
},
85125
_ = process_metrics_interval.tick() => {
86-
refresh_sys_info();
87-
let process_metrics = tokio::task::spawn_blocking(|| {
88-
let sys = SYS_INFO.lock().unwrap();
89-
let total_mem = if let Some(cgroup) = sys.cgroup_limits() {
90-
cgroup.total_memory
91-
} else {
92-
sys.total_memory()
93-
};
94-
sysinfo::get_current_pid()
95-
.ok()
96-
.and_then(|pid| sys.process(pid))
97-
.map(|process| (process.cpu_usage() as f64, process.memory(), total_mem))
98-
}).await.unwrap();
99-
if let Some((cpu_usage, memory_bytes, total_mem)) = process_metrics {
100-
record_process_metrics_sample(cpu_usage, memory_bytes, total_mem);
101-
}
126+
sample_process_metrics().await;
102127
},
103128
_ = &mut shutdown_rx => {
104129
trace!("Resource monitor shutting down");

0 commit comments

Comments
 (0)