neon_arch commited on
Commit
28231d9
·
1 Parent(s): 4afc07f

:zap: perf: move the random delay code in the `search` function (#603)

Browse files
Files changed (1) hide show
  1. src/server/routes/search.rs +10 -2
src/server/routes/search.rs CHANGED
@@ -14,7 +14,8 @@ use crate::{
14
  use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse};
15
  use itertools::Itertools;
16
  use regex::Regex;
17
- use std::borrow::Cow;
 
18
  use tokio::{
19
  fs::File,
20
  io::{AsyncBufReadExt, BufReader},
@@ -83,6 +84,13 @@ pub async fn search(
83
  let previous_page = page.saturating_sub(1);
84
  let next_page = page + 1;
85
 
 
 
 
 
 
 
 
86
  let results: (SearchResults, String, bool);
87
  if page != previous_page {
88
  let (previous_results, current_results, next_results) = join!(
@@ -97,7 +105,7 @@ pub async fn search(
97
  [previous_results?, results.clone(), next_results?]
98
  .into_iter()
99
  .filter_map(|(result, cache_key, flag)| {
100
- dbg!(flag).then_some((result, cache_key))
101
  })
102
  .multiunzip();
103
 
 
14
  use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse};
15
  use itertools::Itertools;
16
  use regex::Regex;
17
+ use std::time::{SystemTime, UNIX_EPOCH};
18
+ use std::{borrow::Cow, time::Duration};
19
  use tokio::{
20
  fs::File,
21
  io::{AsyncBufReadExt, BufReader},
 
84
  let previous_page = page.saturating_sub(1);
85
  let next_page = page + 1;
86
 
87
+ // Add a random delay before making the request.
88
+ if config.aggregator.random_delay || !config.debug {
89
+ let nanos = SystemTime::now().duration_since(UNIX_EPOCH)?.subsec_nanos() as f32;
90
+ let delay = ((nanos / 1_0000_0000 as f32).floor() as u64) + 1;
91
+ tokio::time::sleep(Duration::from_secs(delay)).await;
92
+ }
93
+
94
  let results: (SearchResults, String, bool);
95
  if page != previous_page {
96
  let (previous_results, current_results, next_results) = join!(
 
105
  [previous_results?, results.clone(), next_results?]
106
  .into_iter()
107
  .filter_map(|(result, cache_key, flag)| {
108
+ flag.then_some((result, cache_key))
109
  })
110
  .multiunzip();
111