Skip to content

Commit

Permalink
chore: comment the why of thread::spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
carpenat authored and carpenat committed May 14, 2022
1 parent d1d69b3 commit b82934a
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion angelsharkd/src/routes/extensions/simple_search/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,18 @@ async fn search(
/// Immediately returns. Spawns an asynchronous task to complete the haystack
/// refresh in the background.
async fn refresh(haystack: Haystack) -> Result<impl Reply, Infallible> {
// Note: why use `thread::spawn()`? Previously I had used `tokio::spawn()`
// here. The difference is that `tokio::spawn()` spawns the task in a thread
// from Tokio's dedicated threadpool. This means that this thread is no
// longer available to answer requests from the HTTP server. This
// effectively meant that during a refresh, large blocking portions of code
// were preventing people from running searches, and they would receive 504
// Gateway Timeouts from the reverse proxy. Using a plain `thread::spawn()`,
// we ensure that a thread outside of Tokio's or Rayon's threadpools.
// Therefore, searches can still happen during even the most intensive of
// refreshes.

// Run refresh as a background task and immediately return.
// TODO: notes on why this is the way it is
thread::spawn(move || {
if let Err(e) = haystack.refresh() {
error!("{}", e.to_string());
Expand Down

0 comments on commit b82934a

Please sign in to comment.