mirror of
https://github.com/neon-mmd/websurfx.git
synced 2024-11-22 05:58:21 -05:00
⚡ perf: move the random delay code in the search
function (#603)
This commit is contained in:
parent
4afc07f1d5
commit
28231d99b8
@ -14,7 +14,8 @@ use crate::{
|
|||||||
use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse};
|
use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::borrow::Cow;
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
use std::{borrow::Cow, time::Duration};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{AsyncBufReadExt, BufReader},
|
io::{AsyncBufReadExt, BufReader},
|
||||||
@ -83,6 +84,13 @@ pub async fn search(
|
|||||||
let previous_page = page.saturating_sub(1);
|
let previous_page = page.saturating_sub(1);
|
||||||
let next_page = page + 1;
|
let next_page = page + 1;
|
||||||
|
|
||||||
|
// Add a random delay before making the request.
|
||||||
|
if config.aggregator.random_delay || !config.debug {
|
||||||
|
let nanos = SystemTime::now().duration_since(UNIX_EPOCH)?.subsec_nanos() as f32;
|
||||||
|
let delay = ((nanos / 1_0000_0000 as f32).floor() as u64) + 1;
|
||||||
|
tokio::time::sleep(Duration::from_secs(delay)).await;
|
||||||
|
}
|
||||||
|
|
||||||
let results: (SearchResults, String, bool);
|
let results: (SearchResults, String, bool);
|
||||||
if page != previous_page {
|
if page != previous_page {
|
||||||
let (previous_results, current_results, next_results) = join!(
|
let (previous_results, current_results, next_results) = join!(
|
||||||
@ -97,7 +105,7 @@ pub async fn search(
|
|||||||
[previous_results?, results.clone(), next_results?]
|
[previous_results?, results.clone(), next_results?]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|(result, cache_key, flag)| {
|
.filter_map(|(result, cache_key, flag)| {
|
||||||
dbg!(flag).then_some((result, cache_key))
|
flag.then_some((result, cache_key))
|
||||||
})
|
})
|
||||||
.multiunzip();
|
.multiunzip();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user