service_inner_loop.rs

Overview

This file implements the core loop of a blob sharing and downloading service that operates on files identified by ResourceId. It provides asynchronous mechanisms for sharing local blobs (files) and loading blobs from external sources, handling retries and timeouts as specified. The service runs in a scoped thread environment, processing commands received through a channel and spawning worker threads to perform share or load operations concurrently.

The main functionality centers around the service_inner_loop function, which listens for Command messages to either share a blob by storing it locally or to download a blob from known external URLs with retry and timeout logic. This file interacts with the submodules download_blob and share_blob that encapsulate the actual file transfer logic.


Types and Data Structures

ShareCallback

type ShareCallback = Box<dyn FnOnce(anyhow::Result<()>) + Send + Sync + 'static>;

LoadSuccessCallback

type LoadSuccessCallback = Box<dyn FnOnce(&mut dyn std::io::Read) + Send + Sync + 'static>;

LoadErrCallback

type LoadErrCallback = Box<dyn FnOnce(anyhow::Error) + Send + Sync + 'static>;

KnownExternalFileshares

type KnownExternalFileshares = Vec<url::Url>;

DownloadOptions

pub struct DownloadOptions {
    pub max_tries: u8,
    pub retry_timeout: Option<std::time::Duration>,
    pub deadline: Option<std::time::Instant>,
}

Command

pub(super) enum Command {
    Share(ResourceId, Box<dyn std::io::Read + Send + Sync + 'static>, ShareCallback),
    Load(
        ResourceId,
        KnownExternalFileshares,
        DownloadOptions,
        LoadSuccessCallback,
        LoadErrCallback,
    ),
}

Functions

service_inner_loop

pub(super) fn service_inner_loop(
    local_storage_share_base_path: PathBuf,
    control: InstrumentedReceiver<Command>,
)

Interaction with Other Modules


Important Implementation Algorithms and Details


Workflow Diagram

flowchart TD
A[Start service_inner_loop] --> B{Receive Command}
B --> |Share| C[Spawn share thread]
B --> |Load| D[Spawn load thread]
C --> E[Construct share path]
E --> F[Call share_blob]
F --> G[Invoke ShareCallback]
G --> B
D --> H[Shuffle URLs]
H --> I[Construct local file path]
I --> J[Call download_blob with retry & timeout]
J --> K{Download success?}
K --> |Yes| L[Open downloaded file]
L --> M[Invoke LoadSuccessCallback]
K --> |No| N[Invoke LoadErrCallback]
M --> B
N --> B

This flowchart illustrates the main control flow of the service_inner_loop function handling Share and Load commands, showing the steps and callbacks involved in each process.