Skip to content

Commit

Permalink
Merge pull request AleoNet#1954 from ljedrz/reqwest_client
Browse files Browse the repository at this point in the history
Don't keep recreating the reqwest Client while syncing
  • Loading branch information
howardwu authored Oct 16, 2022
2 parents 293effd + 1b77e63 commit 9490cb6
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions snarkos/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,13 @@ impl<N: Network> Ledger<N> {
// Fetch the ledger height.
let ledger_height = self.ledger.read().latest_height();

// Create a Client to maintain a connection pool throughout the sync.
let client = reqwest::Client::builder().build()?;

// Fetch the latest height.
let latest_height = reqwest::get(format!("http://{leader_ip}/testnet3/latest/height"))
let latest_height = client
.get(format!("http://{leader_ip}/testnet3/latest/height"))
.send()
.await?
.text()
.await?
Expand All @@ -310,17 +315,21 @@ impl<N: Network> Ledger<N> {
trace!("Requesting block {height} of {latest_height}");

// Download the block with an exponential backoff retry policy.
handle_dispatch_error(move || async move {
// Get the URL for the block download.
let block_url = format!("{TARGET_URL}{height}.block");
let client_clone = client.clone();
handle_dispatch_error(move || {
let client = client_clone.clone();
async move {
// Get the URL for the block download.
let block_url = format!("{TARGET_URL}{height}.block");

// Fetch the bytes from the given url
let block_bytes = reqwest::get(block_url).await?.bytes().await?;
// Fetch the bytes from the given url
let block_bytes = client.get(block_url).send().await?.bytes().await?;

// Parse the block.
let block = task::spawn_blocking(move || Block::from_bytes_le(&block_bytes)).await.unwrap()?;
// Parse the block.
let block = task::spawn_blocking(move || Block::from_bytes_le(&block_bytes)).await.unwrap()?;

std::future::ready(Ok(block)).await
std::future::ready(Ok(block)).await
}
})
})
.buffered(CONCURRENT_REQUESTS)
Expand Down

0 comments on commit 9490cb6

Please sign in to comment.