Skip to content

Commit 477e5b5

Browse files
refactor(http): flatten nested conditionals and use is_multiple_of in cron
Replace nested if-let chains with let-chains in admin auth check and cron scheduler. Replace modulo checks with is_multiple_of() for clarity. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
1 parent 301857a commit 477e5b5

2 files changed

Lines changed: 29 additions & 30 deletions

File tree

crates/http/src/handlers/cron.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,29 @@ pub async fn start_cron_scheduler(state: Arc<AppState>) {
2222

2323
loop_count = loop_count.wrapping_add(1);
2424

25-
if loop_count % 60 == 0 {
26-
if let Some(ref infinite_mem) = state.infinite_mem {
27-
tracing::debug!("Cron: running infinite memory compression...");
28-
let mem = Arc::clone(infinite_mem);
29-
state.background_tasks.lock().await.spawn(async move {
30-
match mem.run_full_compression().await {
31-
Ok((five_min, hour, day)) => {
32-
if five_min > 0 || hour > 0 || day > 0 {
33-
tracing::info!(
34-
"Cron: created {} 5min, {} hour, {} day summaries",
35-
five_min,
36-
hour,
37-
day,
38-
);
39-
}
25+
if loop_count.is_multiple_of(60)
26+
&& let Some(ref infinite_mem) = state.infinite_mem
27+
{
28+
tracing::debug!("Cron: running infinite memory compression...");
29+
let mem = Arc::clone(infinite_mem);
30+
state.background_tasks.lock().await.spawn(async move {
31+
match mem.run_full_compression().await {
32+
Ok((five_min, hour, day)) => {
33+
if five_min > 0 || hour > 0 || day > 0 {
34+
tracing::info!(
35+
"Cron: created {} 5min, {} hour, {} day summaries",
36+
five_min,
37+
hour,
38+
day,
39+
);
4040
}
41-
Err(e) => tracing::warn!("Cron: infinite memory error: {e:?}"),
4241
}
43-
});
44-
}
42+
Err(e) => tracing::warn!("Cron: infinite memory error: {e:?}"),
43+
}
44+
});
4545
}
4646

47-
if loop_count % 180 == 0 {
47+
if loop_count.is_multiple_of(180) {
4848
tracing::debug!("Cron: running embedding backfill...");
4949
let state_clone = Arc::clone(&state);
5050
state.background_tasks.lock().await.spawn(async move {
@@ -58,7 +58,7 @@ pub async fn start_cron_scheduler(state: Arc<AppState>) {
5858
});
5959
}
6060

61-
if loop_count % 360 == 0 {
61+
if loop_count.is_multiple_of(360) {
6262
let state_clone = Arc::clone(&state);
6363
state.background_tasks.lock().await.spawn(async move {
6464
match state_clone.observation_service.run_dedup_sweep().await {
@@ -71,7 +71,7 @@ pub async fn start_cron_scheduler(state: Arc<AppState>) {
7171
});
7272
}
7373

74-
if loop_count % 720 == 0 {
74+
if loop_count.is_multiple_of(720) {
7575
let state_clone = Arc::clone(&state);
7676
state.background_tasks.lock().await.spawn(async move {
7777
if let Err(e) = state_clone
@@ -84,7 +84,7 @@ pub async fn start_cron_scheduler(state: Arc<AppState>) {
8484
});
8585
}
8686

87-
if loop_count % 17280 == 0 {
87+
if loop_count.is_multiple_of(17280) {
8888
let ttl_secs = state.config.dlq_ttl_secs();
8989
let state_clone = Arc::clone(&state);
9090
state.background_tasks.lock().await.spawn(async move {
@@ -102,7 +102,7 @@ pub async fn start_cron_scheduler(state: Arc<AppState>) {
102102
});
103103
}
104104

105-
if loop_count % 2160 == 0 {
105+
if loop_count.is_multiple_of(2160) {
106106
let state_clone = Arc::clone(&state);
107107
state.background_tasks.lock().await.spawn(async move {
108108
match state_clone
@@ -125,7 +125,7 @@ pub async fn start_cron_scheduler(state: Arc<AppState>) {
125125
});
126126
}
127127

128-
if loop_count % 360 == 0 {
128+
if loop_count.is_multiple_of(360) {
129129
let state_clone = Arc::clone(&state);
130130
state.background_tasks.lock().await.spawn(async move {
131131
match state_clone

crates/http/src/handlers/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ pub(crate) fn check_admin_access(
1717
headers: &axum::http::HeaderMap,
1818
config: &opencode_mem_core::AppConfig,
1919
) -> bool {
20-
if let Some(ref token) = config.admin_token {
21-
if let Some(provided) = headers.get("x-admin-token").and_then(|h| h.to_str().ok()) {
22-
if subtle::ConstantTimeEq::ct_eq(provided.as_bytes(), token.as_bytes()).into() {
23-
return true;
24-
}
25-
}
20+
if let Some(ref token) = config.admin_token
21+
&& let Some(provided) = headers.get("x-admin-token").and_then(|h| h.to_str().ok())
22+
&& subtle::ConstantTimeEq::ct_eq(provided.as_bytes(), token.as_bytes()).into()
23+
{
24+
return true;
2625
}
2726

2827
if config.admin_token.is_none() && addr.ip().is_loopback() {

0 commit comments

Comments
 (0)