Skip to content

Commit 5ca0757

Browse files
refactor: extract none_if_empty helper and RESULTS_CACHE_TTL_SECS constant per code review
Agent-Logs-Url: https://github.com/Cameroon-Developer-Network/Zenvra/sessions/9a472664-a192-4747-8e2c-fd4a9380606f Co-authored-by: chojuninengu <180976849+chojuninengu@users.noreply.github.com>
1 parent dd8e5e8 commit 5ca0757

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

crates/server/src/main.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ const MAX_SCAN_BODY_BYTES: usize = 512 * 1024;
3131
const RATE_WINDOW: Duration = Duration::from_secs(60);
3232
/// Maximum scan requests per IP per window.
3333
const RATE_LIMIT: u32 = 10;
34+
/// How long completed scan results are cached for late SSE subscribers (seconds).
35+
const RESULTS_CACHE_TTL_SECS: u64 = 300;
36+
37+
/// Return `None` when `s` is empty, otherwise wrap it in `Some`.
38+
fn none_if_empty(s: &str) -> Option<&str> {
39+
if s.is_empty() { None } else { Some(s) }
40+
}
3441

3542
#[derive(Parser)]
3643
#[command(name = "zenvra-server")]
@@ -357,9 +364,9 @@ async fn run_scan(
357364
.bind(finding.severity.to_string())
358365
.bind(&finding.title)
359366
.bind(&finding.description)
360-
.bind(if finding.explanation.is_empty() { None } else { Some(&finding.explanation) })
367+
.bind(none_if_empty(&finding.explanation))
361368
.bind(&finding.vulnerable_code)
362-
.bind(if finding.fixed_code.is_empty() { None } else { Some(&finding.fixed_code) })
369+
.bind(none_if_empty(&finding.fixed_code))
363370
.bind(finding.line_start as i32)
364371
.bind(finding.line_end as i32)
365372
.bind(&finding.file_path)
@@ -402,8 +409,8 @@ async fn run_scan(
402409
state_task.scans.remove(&scan_id);
403410
state_task.results.insert(scan_id, all_events);
404411

405-
// Clean up results cache after 5 minutes
406-
tokio::time::sleep(tokio::time::Duration::from_secs(300)).await;
412+
// Clean up results cache after TTL
413+
tokio::time::sleep(tokio::time::Duration::from_secs(RESULTS_CACHE_TTL_SECS)).await;
407414
state_task.results.remove(&scan_id);
408415
});
409416

@@ -512,9 +519,9 @@ async fn run_workspace_scan(
512519
.bind(finding.severity.to_string())
513520
.bind(&finding.title)
514521
.bind(&finding.description)
515-
.bind(if finding.explanation.is_empty() { None } else { Some(&finding.explanation) })
522+
.bind(none_if_empty(&finding.explanation))
516523
.bind(&finding.vulnerable_code)
517-
.bind(if finding.fixed_code.is_empty() { None } else { Some(&finding.fixed_code) })
524+
.bind(none_if_empty(&finding.fixed_code))
518525
.bind(finding.line_start as i32)
519526
.bind(finding.line_end as i32)
520527
.bind(&finding.file_path)
@@ -546,7 +553,7 @@ async fn run_workspace_scan(
546553
state_task.scans.remove(&scan_id);
547554
state_task.results.insert(scan_id, all_events);
548555

549-
tokio::time::sleep(tokio::time::Duration::from_secs(300)).await;
556+
tokio::time::sleep(tokio::time::Duration::from_secs(RESULTS_CACHE_TTL_SECS)).await;
550557
state_task.results.remove(&scan_id);
551558
});
552559

0 commit comments

Comments
 (0)