download_blob.rs

Overview

This file provides functionality to download a binary large object (blob) from one or more specified URLs and save it to a local file system path. It handles retries, timeout management, temporary file handling, and error reporting to ensure robust and reliable downloading of blobs.

The main exported function download_blob orchestrates the downloading process, and it relies on the private helper function download_file to perform the actual HTTP download from a single URL.


Functions

download_blob

pub fn download_blob(
    share_full_path: &PathBuf,
    tmp_dir_path: &Path,
    urls: &[url::Url],
    max_tries: u8,
    retry_timeout: Option<std::time::Duration>,
    deadline: Option<std::time::Instant>,
) -> anyhow::Result<()>

Description

Downloads a blob from a list of URLs and saves it to the specified share_full_path. The download is attempted multiple times (max_tries) across the provided URLs. The function supports specifying an optional retry timeout between attempts and an absolute deadline after which attempts are aborted.

Parameters

Return Value

Usage Example

let urls = vec![
    url::Url::parse("https://example.com/blob1").unwrap(),
    url::Url::parse("https://backup.example.com/blob1").unwrap(),
];
download_blob(&PathBuf::from("/data/blob1"), Path::new("/tmp"), &urls, 3, Some(std::time::Duration::from_secs(5)), None)?;

Implementation Details


download_file

fn download_file(
    url: &url::Url,
    file: &mut std::fs::File,
    deadline: Option<std::time::Instant>,
) -> anyhow::Result<()>

Description

Performs a blocking HTTP GET request to download the contents of the specified URL and writes the data directly into the provided file handle.

Parameters

Return Value

Usage Example

let mut file = std::fs::File::create("/tmp/downloaded_blob")?;
download_file(&url::Url::parse("https://example.com/blob")?, &mut file, None)?;

Implementation Details


Important Constants


Interactions with Other System Components


Algorithm and Workflow Summary

  1. Check if the target file exists: If yes, skip downloading.

  2. Create temporary file: Generate a temporary path and create the file, ensuring parent directories exist.

  3. Download attempts:

    • Iterate up to max_tries.

    • For each try, iterate over all URLs.

    • Check if the deadline is exceeded, bail if yes.

    • Attempt to download from the current URL.

    • On failure, log error, truncate the temp file, and retry.

    • On success, break loops.

  4. If all attempts fail, return error.

  5. Rename the temporary file to the final destination atomically.


File Structure and Function Relationship Diagram

flowchart TD
A[download_blob] --> B[get_temp_file_path]
A --> C[download_file]
C --> D[reqwest::blocking::Client]
A --> E[std::fs::File]
C --> E
A --> F[std::fs::rename]

This file is essential for reliable downloading of blobs with retry and timeout mechanisms, ensuring atomic writes and safe file handling. It encapsulates network interaction and file system operations in a fault-tolerant manner.