Skip to content

Commit

Permalink
feat: add timeout to fusion search (#174)
Browse files Browse the repository at this point in the history
* feat: add timeout to fusion search

* feat: add timeout to fusion search #174
  • Loading branch information
medcl authored Feb 23, 2025
1 parent a25bfd7 commit 65d1e05
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/content.en/docs/release-notes/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Information about release notes of Coco Server is provided here.
## Latest (In development)

### Features
- Add timeout to fusion search #174

### Breaking changes
### Bug fix
- Fix to access deeplink for linux #148
Expand Down
30 changes: 27 additions & 3 deletions src-tauri/src/search/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use crate::common::traits::SearchError;
use futures::stream::FuturesUnordered;
use futures::StreamExt;
use std::collections::HashMap;
use std::collections::HashSet;
use tauri::{AppHandle, Manager, Runtime};
use tokio::time::{timeout, Duration};

#[tauri::command]
pub async fn query_coco_fusion<R: Runtime>(
app_handle: AppHandle<R>,
Expand All @@ -22,6 +25,10 @@ pub async fn query_coco_fusion<R: Runtime>(

let sources_list = sources_future.await;

// Time limit for each query
let timeout_duration = Duration::from_millis(100);

// Push all queries into futures
for query_source in sources_list {
let query_source_type = query_source.get_type().clone();
sources.insert(query_source_type.id.clone(), query_source_type);
Expand All @@ -30,7 +37,11 @@ pub async fn query_coco_fusion<R: Runtime>(
let query_source_clone = query_source.clone(); // Clone Arc to avoid ownership issues

futures.push(tokio::spawn(async move {
query_source_clone.search(query).await
// Timeout each query execution
timeout(timeout_duration, async {
query_source_clone.search(query).await
})
.await
}));
}

Expand All @@ -41,7 +52,7 @@ pub async fn query_coco_fusion<R: Runtime>(

while let Some(result) = futures.next().await {
match result {
Ok(Ok(response)) => {
Ok(Ok(Ok(response))) => {
total_hits += response.total_hits;
let source_id = response.source.id.clone();

Expand Down Expand Up @@ -72,6 +83,19 @@ pub async fn query_coco_fusion<R: Runtime>(
reason: None,
});
}
// Timeout reached, skip this request
Ok(_) => {
failed_requests.push(FailedRequest {
source: QuerySource {
r#type: "N/A".into(),
name: "N/A".into(),
id: "N/A".into(),
},
status: 0,
error: Some("Query source timed out".to_string()),
reason: None,
});
}
Err(_) => {
failed_requests.push(FailedRequest {
source: QuerySource {
Expand Down Expand Up @@ -100,7 +124,7 @@ pub async fn query_coco_fusion<R: Runtime>(
};

let mut final_hits = Vec::new();
let mut seen_docs = std::collections::HashSet::new(); // To track documents we've already added
let mut seen_docs = HashSet::new(); // To track documents we've already added

// Distribute hits fairly across sources
for (_source_id, hits) in &mut hits_per_source {
Expand Down

0 comments on commit 65d1e05

Please sign in to comment.