Skip to content

Commit

Permalink
Properly handle responses while downloading snapshots (#949)
Browse files Browse the repository at this point in the history
* Properly handle responses while downloading snapshots

* Formatting

* Fix logging level
  • Loading branch information
grtlr committed Jan 13, 2022
1 parent 2b188e6 commit 4200a37
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions bee-ledger/src/workers/snapshot/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,30 @@ async fn download_snapshot_header(download_url: &str) -> Result<SnapshotHeader,

match reqwest::get(download_url).await.and_then(Response::error_for_status) {
Ok(res) => {
let mut stream = res.bytes_stream();
let mut bytes = Vec::<u8>::with_capacity(SnapshotHeader::LENGTH);
if res.status().is_success() {
let mut stream = res.bytes_stream();
let mut bytes = Vec::<u8>::with_capacity(SnapshotHeader::LENGTH);

while let Some(chunk) = stream.next().await {
let mut chunk_reader = chunk.map_err(|_| Error::DownloadingFailed)?.reader();
while let Some(chunk) = stream.next().await {
let mut chunk_reader = chunk.map_err(|_| Error::DownloadingFailed)?.reader();

let mut buf = Vec::new();
chunk_reader.read_to_end(&mut buf)?;
bytes.extend_from_slice(&buf);
let mut buf = Vec::new();
chunk_reader.read_to_end(&mut buf)?;
bytes.extend_from_slice(&buf);

if bytes.len() >= SnapshotHeader::LENGTH {
debug!("Downloaded snapshot header from {}.", download_url);
if bytes.len() >= SnapshotHeader::LENGTH {
debug!("Downloaded snapshot header from {}.", download_url);

let mut slice: &[u8] = &bytes[..SnapshotHeader::LENGTH];
let mut slice: &[u8] = &bytes[..SnapshotHeader::LENGTH];

return Ok(SnapshotHeader::unpack(&mut slice)?);
return Ok(SnapshotHeader::unpack(&mut slice)?);
}
}
} else {
debug!(
"Downloading snapshot header failed with status code {:?}.",
res.status()
);
}
}
Err(e) => debug!("Downloading snapshot header failed: {:?}.", e.to_string()),
Expand Down Expand Up @@ -154,13 +161,17 @@ async fn download_snapshot_file(path: &Path, download_url: &str) -> Result<(), E

match reqwest::get(download_url).await {
Ok(res) => {
tokio::io::copy(
&mut res.bytes().await.map_err(|_| Error::DownloadingFailed)?.as_ref(),
&mut tokio::fs::File::create(path).await?,
)
.await?;
if res.status().is_success() {
tokio::io::copy(
&mut res.bytes().await.map_err(|_| Error::DownloadingFailed)?.as_ref(),
&mut tokio::fs::File::create(path).await?,
)
.await?;
} else {
warn!("Downloading snapshot file failed with status code {:?}.", res.status());
}
}
Err(e) => warn!("Downloading snapshot file failed with status code {:?}.", e.status()),
Err(e) => warn!("Downloading snapshot failed: {:?}.", e.to_string()),
}

Ok(())
Expand Down

0 comments on commit 4200a37

Please sign in to comment.