Skip to content

web search done right

webgraph spawn_blocking to handle batch requests concurrently

Mikkel Denker8567b8ebf465
diff --git a/crates/core/src/distributed/sonic/service.rs b/crates/core/src/distributed/sonic/service.rs
index bb247277..38258339 100644
--- a/crates/core/src/distributed/sonic/service.rs
+++ b/crates/core/src/distributed/sonic/service.rs
@@ -1,5 +1,5 @@
// Stract is an open source web search engine.
-// Copyright (C) 2023 Stract ApS
+// Copyright (C) 2024 Stract ApS
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
@@ -71,9 +71,11 @@ impl<S: Service> Server<S> {
let mut res = Vec::new();
for req in bodies {
- res.push(S::handle(req, &service).await);
+ res.push(S::handle(req, &service));
}
+ let res = futures::future::join_all(res).await;
+
if let Err(e) = req.respond(OneOrMany::Many(res)).await {
tracing::error!("failed to respond to request: {}", e);
}
diff --git a/crates/core/src/entrypoint/webgraph_server.rs b/crates/core/src/entrypoint/webgraph_server.rs
index 5c25478b..4d898934 100644
--- a/crates/core/src/entrypoint/webgraph_server.rs
+++ b/crates/core/src/entrypoint/webgraph_server.rs
@@ -1,5 +1,5 @@
// Stract is an open source web search engine.
-// Copyright (C) 2023 Stract ApS
+// Copyright (C) 2024 Stract ApS
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
@@ -94,7 +94,12 @@ macro_rules! impl_search {
type Response = Result<<<$q as webgraph::Query>::Collector as webgraph::Collector>::Fruit, EncodedError>;
async fn handle(self, server: &WebGraphService) -> Self::Response {
- server.graph.search_initial(&self).map_err(|e| EncodedError { msg: e.to_string() })
+ let graph = Arc::clone(&server.graph);
+ tokio::task::spawn_blocking(move || {
+ graph.search_initial(&self).map_err(|e| EncodedError { msg: e.to_string() })
+ })
+ .await
+ .unwrap()
}
}
@@ -109,7 +114,12 @@ macro_rules! impl_search {
impl Message<WebGraphService> for [<$q Retrieve>] {
type Response = Result<<$q as webgraph::Query>::IntermediateOutput, EncodedError>;
async fn handle(self, server: &WebGraphService) -> Self::Response {
- server.graph.retrieve(&self.query, self.fruit).map_err(|e| EncodedError { msg: e.to_string() })
+ let graph = Arc::clone(&server.graph);
+ tokio::task::spawn_blocking(move || {
+ graph.retrieve(&self.query, self.fruit).map_err(|e| EncodedError { msg: e.to_string() })
+ })
+ .await
+ .unwrap()
}
}
diff --git a/crates/core/src/searcher/api/mod.rs b/crates/core/src/searcher/api/mod.rs
index 07def39d..fc6f850c 100644
--- a/crates/core/src/searcher/api/mod.rs
+++ b/crates/core/src/searcher/api/mod.rs
@@ -229,7 +229,7 @@ where
T: Graph,
{
async fn batch_ingoing(&self, nodes: &[webgraph::NodeID]) -> Vec<Vec<webgraph::NodeID>> {
- self.batch_raw_ingoing_hosts(nodes, EdgeLimit::Limit(1024))
+ self.batch_raw_ingoing_hosts(nodes, EdgeLimit::Limit(512))
.await
.into_iter()
.map(|edges| {