Skip to content

Commit

Permalink
Add spawn_blocking around the get_blocks GET request
Browse files Browse the repository at this point in the history
  • Loading branch information
raychu86 committed May 9, 2024
1 parent 9a6ac11 commit 05a3fa4
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions node/rest/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,18 @@ impl<N: Network, C: ConsensusStorage<N>, R: Routing<N>> Rest<N, C, R> {
)));
}

let blocks = cfg_into_iter!((start_height..end_height))
.map(|height| rest.ledger.get_block(height))
.collect::<Result<Vec<_>, _>>()?;

Ok(ErasedJson::pretty(blocks))
// Fetch the blocks from ledger.
match tokio::task::spawn_blocking(move || {
cfg_into_iter!((start_height..end_height))
.map(|height| rest.ledger.get_block(height))
.collect::<Result<Vec<_>, _>>()
})
.await
{
Ok(Ok(blocks)) => Ok(ErasedJson::pretty(blocks)),
Ok(Err(err)) => Err(RestError(format!("Failed to get blocks '{start_height}..{end_height}' - {err}"))),
Err(err) => Err(RestError(format!("Failed to get blocks '{start_height}..{end_height}' - {err}"))),
}
}

// GET /<network>/height/{blockHash}
Expand Down

0 comments on commit 05a3fa4

Please sign in to comment.