pub struct CdnClient { /* private fields */ }Expand description
CDN client for downloading content with automatic fallback
Implementations§
Source§impl CdnClient
impl CdnClient
Sourcepub fn with_client(client: Client) -> Self
pub fn with_client(client: Client) -> Self
Create a new CDN client with custom HTTP client
Sourcepub fn builder() -> CdnClientBuilder
pub fn builder() -> CdnClientBuilder
Create a builder for configuring the CDN client
Sourcepub fn add_primary_host(&self, host: impl Into<String>)
pub fn add_primary_host(&self, host: impl Into<String>)
Add a primary CDN host
Primary hosts are tried before fallback hosts
Sourcepub fn add_primary_hosts(
&self,
hosts: impl IntoIterator<Item = impl Into<String>>,
)
pub fn add_primary_hosts( &self, hosts: impl IntoIterator<Item = impl Into<String>>, )
Add multiple primary CDN hosts
Sourcepub fn set_primary_hosts(
&self,
hosts: impl IntoIterator<Item = impl Into<String>>,
)
pub fn set_primary_hosts( &self, hosts: impl IntoIterator<Item = impl Into<String>>, )
Set primary CDN hosts, replacing any existing ones
Sourcepub fn add_fallback_host(&self, host: impl Into<String>)
pub fn add_fallback_host(&self, host: impl Into<String>)
Add a fallback CDN host
Sourcepub fn add_fallback_hosts(
&self,
hosts: impl IntoIterator<Item = impl Into<String>>,
)
pub fn add_fallback_hosts( &self, hosts: impl IntoIterator<Item = impl Into<String>>, )
Add multiple fallback CDN hosts
Sourcepub fn clear_primary_hosts(&self)
pub fn clear_primary_hosts(&self)
Clear all primary hosts
Sourcepub fn clear_fallback_hosts(&self)
pub fn clear_fallback_hosts(&self)
Clear all fallback hosts
Sourcepub fn disable_fallbacks(&self)
pub fn disable_fallbacks(&self)
Disable fallback hosts (only use primary)
Sourcepub fn get_all_hosts(&self) -> Vec<String>
pub fn get_all_hosts(&self) -> Vec<String>
Get all configured hosts (primary first, then fallback)
Sourcepub fn with_max_retries(self, max_retries: u32) -> Self
pub fn with_max_retries(self, max_retries: u32) -> Self
Set the maximum number of retries
Sourcepub fn with_initial_backoff_ms(self, initial_backoff_ms: u64) -> Self
pub fn with_initial_backoff_ms(self, initial_backoff_ms: u64) -> Self
Set the initial backoff duration in milliseconds
Sourcepub fn with_max_backoff_ms(self, max_backoff_ms: u64) -> Self
pub fn with_max_backoff_ms(self, max_backoff_ms: u64) -> Self
Set the maximum backoff duration in milliseconds
Sourcepub fn with_backoff_multiplier(self, backoff_multiplier: f64) -> Self
pub fn with_backoff_multiplier(self, backoff_multiplier: f64) -> Self
Set the backoff multiplier
Sourcepub fn with_jitter_factor(self, jitter_factor: f64) -> Self
pub fn with_jitter_factor(self, jitter_factor: f64) -> Self
Set the jitter factor (0.0 to 1.0)
Sourcepub fn with_user_agent(self, user_agent: impl Into<String>) -> Self
pub fn with_user_agent(self, user_agent: impl Into<String>) -> Self
Set a custom user agent string
Sourcepub fn calculate_backoff(&self, attempt: u32) -> Duration
pub fn calculate_backoff(&self, attempt: u32) -> Duration
Calculate backoff duration with exponential backoff and jitter
Sourcepub fn build_url(cdn_host: &str, path: &str, hash: &str) -> Result<String>
pub fn build_url(cdn_host: &str, path: &str, hash: &str) -> Result<String>
Build a CDN URL for a content hash
CDN URLs follow the pattern:
http://{cdn_host}/{path}/{hash[0:2]}/{hash[2:4]}/{hash}
Sourcepub async fn check_exists(
&self,
cdn_host: &str,
path: &str,
hash: &str,
) -> Result<bool>
pub async fn check_exists( &self, cdn_host: &str, path: &str, hash: &str, ) -> Result<bool>
Check if content exists on CDN using HEAD request
Sourcepub async fn download(
&self,
cdn_host: &str,
path: &str,
hash: &str,
) -> Result<Response>
pub async fn download( &self, cdn_host: &str, path: &str, hash: &str, ) -> Result<Response>
Download content from CDN by hash with automatic fallback
Sourcepub async fn download_build_config(
&self,
cdn_host: &str,
path: &str,
hash: &str,
) -> Result<Response>
pub async fn download_build_config( &self, cdn_host: &str, path: &str, hash: &str, ) -> Result<Response>
Download BuildConfig from CDN
BuildConfig files are stored at {path}/config/{hash}
Sourcepub async fn download_cdn_config(
&self,
cdn_host: &str,
path: &str,
hash: &str,
) -> Result<Response>
pub async fn download_cdn_config( &self, cdn_host: &str, path: &str, hash: &str, ) -> Result<Response>
Download CDNConfig from CDN
CDNConfig files are stored at {path}/config/{hash}
Sourcepub async fn download_product_config(
&self,
cdn_host: &str,
config_path: &str,
hash: &str,
) -> Result<Response>
pub async fn download_product_config( &self, cdn_host: &str, config_path: &str, hash: &str, ) -> Result<Response>
Download ProductConfig from CDN
ProductConfig files are stored at {config_path}/{hash}
Note: This uses the config_path from CDN response, not the regular path
Sourcepub async fn download_key_ring(
&self,
cdn_host: &str,
path: &str,
hash: &str,
) -> Result<Response>
pub async fn download_key_ring( &self, cdn_host: &str, path: &str, hash: &str, ) -> Result<Response>
Download KeyRing from CDN
KeyRing files are stored at {path}/config/{hash}
Sourcepub async fn download_data(
&self,
cdn_host: &str,
path: &str,
hash: &str,
) -> Result<Response>
pub async fn download_data( &self, cdn_host: &str, path: &str, hash: &str, ) -> Result<Response>
Download data file from CDN
Data files are stored at {path}/data/{hash}
Sourcepub async fn download_patch(
&self,
cdn_host: &str,
path: &str,
hash: &str,
) -> Result<Response>
pub async fn download_patch( &self, cdn_host: &str, path: &str, hash: &str, ) -> Result<Response>
Download patch file from CDN
Patch files are stored at {path}/patch/{hash}
Sourcepub async fn download_parallel(
&self,
cdn_host: &str,
path: &str,
hashes: &[String],
max_concurrent: Option<usize>,
) -> Vec<Result<Vec<u8>>> ⓘ
pub async fn download_parallel( &self, cdn_host: &str, path: &str, hashes: &[String], max_concurrent: Option<usize>, ) -> Vec<Result<Vec<u8>>> ⓘ
Download multiple files in parallel
Returns a vector of results in the same order as the input hashes. Failed downloads will be represented as Err values in the vector.
§Arguments
cdn_host- CDN host to download frompath- Base path on the CDNhashes- List of content hashes to downloadmax_concurrent- Maximum number of concurrent downloads (None = unlimited)
Sourcepub async fn download_batched(
&self,
cdn_host: &str,
path: &str,
hashes: &[String],
) -> Vec<Result<Vec<u8>>> ⓘ
pub async fn download_batched( &self, cdn_host: &str, path: &str, hashes: &[String], ) -> Vec<Result<Vec<u8>>> ⓘ
Download multiple files using HTTP/2 request batching for optimal performance
This method uses HTTP/2 multiplexing to batch requests efficiently, providing significant performance improvements over traditional parallel downloads.
§Arguments
cdn_host- CDN host to download frompath- Base path on the CDNhashes- List of content hashes to download
§Returns
Vector of results in the same order as input hashes, containing downloaded data or errors.
§Performance
HTTP/2 multiplexing allows multiple requests over a single connection, reducing:
- Connection overhead
- Network latency
- Server connection pressure
Expected improvement: 2-5x faster than traditional parallel downloads for batches >10 files.
Sourcepub async fn download_parallel_with_progress<F>(
&self,
cdn_host: &str,
path: &str,
hashes: &[String],
max_concurrent: Option<usize>,
progress: F,
) -> Vec<Result<Vec<u8>>> ⓘ
pub async fn download_parallel_with_progress<F>( &self, cdn_host: &str, path: &str, hashes: &[String], max_concurrent: Option<usize>, progress: F, ) -> Vec<Result<Vec<u8>>> ⓘ
Download multiple files in parallel with progress tracking
Returns a vector of results in the same order as the input hashes. The progress callback is called after each successful download.
§Arguments
cdn_host- CDN host to download frompath- Base path on the CDNhashes- List of content hashes to downloadmax_concurrent- Maximum number of concurrent downloads (None = unlimited)progress- Callback function called with (completed_count, total_count) after each download
Sourcepub async fn download_data_parallel(
&self,
cdn_host: &str,
path: &str,
hashes: &[String],
max_concurrent: Option<usize>,
) -> Vec<Result<Vec<u8>>> ⓘ
pub async fn download_data_parallel( &self, cdn_host: &str, path: &str, hashes: &[String], max_concurrent: Option<usize>, ) -> Vec<Result<Vec<u8>>> ⓘ
Download multiple data files in parallel
Sourcepub async fn download_config_parallel(
&self,
cdn_host: &str,
path: &str,
hashes: &[String],
max_concurrent: Option<usize>,
) -> Vec<Result<Vec<u8>>> ⓘ
pub async fn download_config_parallel( &self, cdn_host: &str, path: &str, hashes: &[String], max_concurrent: Option<usize>, ) -> Vec<Result<Vec<u8>>> ⓘ
Download multiple config files in parallel
Sourcepub async fn download_patch_parallel(
&self,
cdn_host: &str,
path: &str,
hashes: &[String],
max_concurrent: Option<usize>,
) -> Vec<Result<Vec<u8>>> ⓘ
pub async fn download_patch_parallel( &self, cdn_host: &str, path: &str, hashes: &[String], max_concurrent: Option<usize>, ) -> Vec<Result<Vec<u8>>> ⓘ
Download multiple patch files in parallel
Sourcepub async fn download_data_batched(
&self,
cdn_host: &str,
path: &str,
hashes: &[String],
) -> Vec<Result<Vec<u8>>> ⓘ
pub async fn download_data_batched( &self, cdn_host: &str, path: &str, hashes: &[String], ) -> Vec<Result<Vec<u8>>> ⓘ
Download multiple data files using HTTP/2 request batching (optimized)
This method provides superior performance for downloading multiple data files by leveraging HTTP/2 multiplexing to reduce connection overhead and latency.
Sourcepub async fn download_config_batched(
&self,
cdn_host: &str,
path: &str,
hashes: &[String],
) -> Vec<Result<Vec<u8>>> ⓘ
pub async fn download_config_batched( &self, cdn_host: &str, path: &str, hashes: &[String], ) -> Vec<Result<Vec<u8>>> ⓘ
Download multiple config files using HTTP/2 request batching (optimized)
This method provides superior performance for downloading multiple config files by leveraging HTTP/2 multiplexing to reduce connection overhead and latency.
Sourcepub async fn download_patch_batched(
&self,
cdn_host: &str,
path: &str,
hashes: &[String],
) -> Vec<Result<Vec<u8>>> ⓘ
pub async fn download_patch_batched( &self, cdn_host: &str, path: &str, hashes: &[String], ) -> Vec<Result<Vec<u8>>> ⓘ
Download multiple patch files using HTTP/2 request batching (optimized)
This method provides superior performance for downloading multiple patch files by leveraging HTTP/2 multiplexing to reduce connection overhead and latency.
Sourcepub async fn get_batch_stats(&self) -> Option<BatchStats>
pub async fn get_batch_stats(&self) -> Option<BatchStats>
Get statistics for request batching performance
Returns detailed metrics about HTTP/2 batching performance including batch sizes, timing, and HTTP/2 connection usage.
Sourcepub async fn download_parallel_to_writers<W>(
&self,
cdn_host: &str,
path: &str,
hashes: &[String],
writers: Vec<W>,
max_concurrent: Option<usize>,
) -> Vec<Result<u64>> ⓘ
pub async fn download_parallel_to_writers<W>( &self, cdn_host: &str, path: &str, hashes: &[String], writers: Vec<W>, max_concurrent: Option<usize>, ) -> Vec<Result<u64>> ⓘ
Download multiple files in parallel with streaming to writers (memory-efficient)
This method downloads multiple files concurrently while streaming each file directly to its corresponding writer. This prevents loading all downloads into memory simultaneously, making it suitable for large file batches.
§Arguments
cdn_host- CDN host to download frompath- Base path for downloadshashes- Vector of file hashes to downloadwriters- Vector of writers (must match hashes length)max_concurrent- Maximum number of concurrent downloads
§Returns
Vector of results with bytes written for each file
Sourcepub async fn download_streaming<W>(
&self,
cdn_host: &str,
path: &str,
hash: &str,
writer: W,
) -> Result<u64>where
W: AsyncWrite + Unpin,
pub async fn download_streaming<W>(
&self,
cdn_host: &str,
path: &str,
hash: &str,
writer: W,
) -> Result<u64>where
W: AsyncWrite + Unpin,
Download content and stream it to a writer
This is useful for large files to avoid loading them entirely into memory.
Sourcepub async fn download_chunked<F>(
&self,
cdn_host: &str,
path: &str,
hash: &str,
callback: F,
) -> Result<u64>
pub async fn download_chunked<F>( &self, cdn_host: &str, path: &str, hash: &str, callback: F, ) -> Result<u64>
Download content and process it in chunks
This allows processing large files without loading them entirely into memory. The callback is called for each chunk received.
Sourcepub fn create_resumable_download(
&mut self,
cdn_host: &str,
path: &str,
hash: &str,
output_file: &Path,
) -> Result<ResumableDownload>
pub fn create_resumable_download( &mut self, cdn_host: &str, path: &str, hash: &str, output_file: &Path, ) -> Result<ResumableDownload>
Create a resumable download for a data file
This creates a resumable download that can be paused and resumed from interruption. Progress is saved to disk and the download can survive application crashes.
§Arguments
cdn_host- CDN server hostnamepath- Base path on the CDN (e.g., “tpr/wow”)hash- Content hash to downloadoutput_file- Local file path where content should be saved
§Returns
Returns a ResumableDownload instance that can be used to start, pause, and resume the download.
§Example
use ngdp_cdn::CdnClient;
use std::path::PathBuf;
let mut client = CdnClient::new()?;
let mut resumable = client.create_resumable_download(
"blzddist1-a.akamaihd.net",
"tpr/wow",
"2e9c1e3b5f5a0c9d9e8f1234567890ab",
&PathBuf::from("game_file.bin")
)?;
// Start or resume the download
resumable.start_or_resume().await?;
// Clean up progress file when complete
resumable.cleanup_completed().await?;Sourcepub async fn resume_download(
&mut self,
progress_file: &Path,
) -> Result<ResumableDownload>
pub async fn resume_download( &mut self, progress_file: &Path, ) -> Result<ResumableDownload>
Resume an existing download from a progress file
This method loads an existing progress file and creates a ResumableDownload
instance that can continue from where the previous download left off.
§Arguments
progress_file- Path to the.downloadprogress file
§Returns
Returns a ResumableDownload instance ready to resume the download.
§Example
use ngdp_cdn::CdnClient;
use std::path::PathBuf;
let mut client = CdnClient::new()?;
// Resume from existing progress file
let mut resumable = client.resume_download(&PathBuf::from("file.bin.download")).await?;
resumable.start_or_resume().await?;
resumable.cleanup_completed().await?;Sourcepub async fn find_resumable_downloads(
&self,
directory: &Path,
) -> Result<Vec<DownloadProgress>>
pub async fn find_resumable_downloads( &self, directory: &Path, ) -> Result<Vec<DownloadProgress>>
Find all resumable downloads in a directory
This is a convenience method that scans a directory for .download progress files
and returns information about incomplete downloads.
§Arguments
directory- Directory to scan for progress files
§Returns
Returns a vector of DownloadProgress instances for incomplete downloads.
§Example
use ngdp_cdn::CdnClient;
use std::path::PathBuf;
let client = CdnClient::new()?;
// Find all resumable downloads in Downloads folder
let downloads = client.find_resumable_downloads(&PathBuf::from("Downloads")).await?;
for download in downloads {
println!("Found: {} - {}", download.file_hash, download.progress_string());
}Sourcepub async fn cleanup_old_progress_files(
&self,
directory: &Path,
max_age_hours: u64,
) -> Result<usize>
pub async fn cleanup_old_progress_files( &self, directory: &Path, max_age_hours: u64, ) -> Result<usize>
Clean up old completed progress files in a directory
This method scans for .download progress files that are marked as completed
and are older than the specified age, then removes them to free up disk space.
§Arguments
directory- Directory to scan and clean upmax_age_hours- Maximum age in hours for completed progress files
§Returns
Returns the number of files that were cleaned up.
§Example
use ngdp_cdn::CdnClient;
use std::path::PathBuf;
let client = CdnClient::new()?;
// Clean up progress files older than 24 hours
let cleaned = client.cleanup_old_progress_files(&PathBuf::from("Downloads"), 24).await?;
println!("Cleaned up {} old progress files", cleaned);