Expand description
§fast-down
fast-down is a fast, concurrent file downloader library built on top of the
fast_pull pull/push engine, with first-class
HTTP support.
Official Website (Simplified Chinese)
§What this crate provides
fast-down re-exports the entire fast_pull
pull/push engine (the download_single / download_multi entry points, the Puller
and Pusher traits, and the file/memory pushers) and adds the following on top of it:
- HTTP / reqwest puller —
FastDownPuller(withFastDownPullerOptions) is the defaultPullerfor HTTP(S) sources. It builds on aSmartRedirectClientthat follows redirects manually so it can honor theReferrer-Policyheader and strip resource-specific headers (Origin/Authorization/Cookie) on cross-origin hops, per RFC 9110 §15.4. - URL info resolution —
UrlInfoandFileIdcapture a resource’s size, suggested filename, content type, range support, and a stable identity derived from theETag/Last-Modifiedheaders, which powers incremental and resumable downloads. - Proxy support — the
Proxyenum selects no proxy, the system proxy, or a custom proxy URL for outgoing requests. - Result + event stream — download functions return a
DownloadResultwhoseevent_chainreceiver disconnects once the pipeline has fully flushed; awaiting that disconnect is the completion signal (no separate join handle).
Supporting building blocks (behind feature flags) include the backend-agnostic
http module (HttpClient traits, HttpPuller, Prefetch, ContentDisposition,
HttpError) and the reqwest module (SmartRedirectClient,
ManualRedirectRequestBuilder).
§Example
Download a file concurrently to disk. This requires the reqwest feature
(which also enables http and fast-puller) and a network connection, so the
block is marked ignore to keep doctests hermetic:
use std::sync::Arc;
use std::time::Duration;
use url::Url;
use fast_down::{FastDownPuller, FastDownPullerOptions, FileId, Proxy};
use fast_pull::StdFilePusher;
use fast_pull::multi::DownloadOptions;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let url = Url::parse("https://example.com/large.bin")?;
let file = tokio::fs::File::create("large.bin").await?;
// Pre-size the file; pass `true` to fsync on flush.
let pusher = StdFilePusher::new(file, /* size */ 0, /* sync_all */ false).await?;
let puller = FastDownPuller::new(FastDownPullerOptions {
url,
headers: Arc::new(Default::default()),
proxy: Proxy::System,
accept_invalid_certs: false,
accept_invalid_hostnames: false,
cookie_store: false,
file_id: FileId::default(),
resp: None,
available_ips: Arc::from(Vec::<std::net::IpAddr>::new()),
max_redirects: 10,
})?;
let result = fast_pull::download_multi(
puller,
pusher,
DownloadOptions {
download_chunks: vec![0..u64::MAX].into_iter(),
concurrent: 8,
retry_gap: Duration::from_secs(1),
pull_timeout: Duration::from_secs(30),
push_queue_cap: 1024,
min_chunk_size: 1 << 20,
max_speculative: 3,
},
);
while result.event_chain().recv().await.is_ok() {}
Ok(())
}For a sequential, single-threaded download, swap download_multi for
fast_pull::download_single and use fast_pull::single::DownloadOptions
(which only has retry_gap and push_queue_cap).
Modules§
- mock
- A deterministic in-memory
Pullerfor tests, plus a helper to build mock payloads. - multi
- Multi-threaded concurrent download with work-stealing.
- single
- Single-threaded sequential download.
Structs§
- BoxPusher
- A type-erased pusher that boxes both the pusher and its error type.
- BufWriter
Pusher - Pusher decorator that provides
std::io::BufWriter-style linear write buffering. - Cache
Direct Pusher - Pusher wrapper that buffers chunks and flushes large contiguous runs without merging.
- Cache
Merge Pusher - Pusher wrapper that buffers chunks and merges each flush run into a single
Bytes. - Cache
SeqPusher - Pusher wrapper that reorders out-of-order chunks into sequential order.
- Download
Result - Handle to an active download session.
- FileId
- File identity used for incremental and resumable downloads.
- Invert
Iter - Iterator that yields the gaps (non-downloaded ranges) from a list of
ProgressEntrys. - MemPusher
- In-memory pusher for testing or buffer-based workflows.
- UrlInfo
- Metadata about a downloadable resource, gathered from the initial HTTP request.
Enums§
- Event
- Events emitted during a download session, received via
DownloadResult::event_chain. - Proxy
- Proxy configuration for outgoing HTTP requests.
Traits§
- AnyError
- Marker trait for type-erased error types.
- Merge
- Trait for merging a new
ProgressEntryinto a sorted list of existing entries. - Pull
Stream - A pull stream that yields
Byteschunks. - Puller
- Abstraction over a data source that can be pulled (downloaded) in chunks.
- Puller
Error - Extension trait for pull errors, distinguishing recoverable from irrecoverable failures.
- Pusher
- Abstraction over a data sink that receives pushed byte chunks.
- Total
- Trait for computing the total size from one or more
ProgressEntryvalues.
Functions§
- invert
window: when aProgressEntrylength is less thanwindow, it is merged into the gap to reduce progress fragmentation.
Type Aliases§
- Progress
Entry - A byte-range representing downloaded or to-be-downloaded progress.
- Progress
Listener - A callback invoked whenever a chunk has been successfully written to its destination (disk / memory).
- Pull
Result - Result type returned by pulling operations.
- Worker
Id - Numeric identifier assigned to each worker thread/task.