pub struct ConcurrentSegmentManager { /* private fields */ }Implementations§
Source§impl ConcurrentSegmentManager
impl ConcurrentSegmentManager
Sourcepub fn new(
total_size: u64,
urls: Vec<String>,
segment_size: Option<u64>,
) -> Self
pub fn new( total_size: u64, urls: Vec<String>, segment_size: Option<u64>, ) -> Self
Create a new segment manager with basic round-robin mirror selection.
This is the basic constructor that uses the built-in MirrorState
for mirror tracking. For intelligent mirror selection, use
new_with_selector() instead.
Sourcepub fn new_with_selector(
total_size: u64,
urls: Vec<String>,
segment_size: Option<u64>,
stat_man: Arc<ServerStatMan>,
uri_selector: Box<dyn UriSelector>,
) -> Self
pub fn new_with_selector( total_size: u64, urls: Vec<String>, segment_size: Option<u64>, stat_man: Arc<ServerStatMan>, uri_selector: Box<dyn UriSelector>, ) -> Self
Create a new segment manager with intelligent mirror selection.
This constructor integrates with ServerStatMan and UriSelector
for intelligent mirror selection based on historical performance data.
§Arguments
total_size- Total file size in bytesurls- List of mirror URLssegment_size- Optional segment size (default: 1 MB)stat_man- Shared server statistics manageruri_selector- URI selector for intelligent mirror selection
§Example
use std::sync::Arc;
use aria2_core::selector::server_stat_man::ServerStatMan;
use aria2_core::selector::adaptive_uri_selector::AdaptiveUriSelector;
use aria2_core::engine::concurrent_segment_manager::ConcurrentSegmentManager;
let stat_man = Arc::new(ServerStatMan::new());
let urls = vec!["http://mirror1.com/file".to_string()];
let selector = Box::new(AdaptiveUriSelector::new_with_uris(Arc::clone(&stat_man), urls.clone()));
let mgr = ConcurrentSegmentManager::new_with_selector(
10_000_000,
urls,
Some(1_000_000),
stat_man,
selector,
);pub fn allocate_segments(&mut self)
pub fn next_pending_segment_for_mirror( &mut self, mirror_idx: usize, ) -> Option<(u32, u64, u64)>
pub fn next_pending_segment(&mut self) -> Option<(u32, u64, u64)>
Sourcepub fn allocate_next_index(&self) -> Option<u32>
pub fn allocate_next_index(&self) -> Option<u32>
Atomically allocate the next segment index for lock-free assignment.
Returns the index of the next segment to claim, or None if all segments
have been allocated. This uses fetch_add for O(1) lock-free allocation.
The caller is responsible for checking the segment status and claiming it.
Unlike next_pending_segment_for_mirror,
this method takes only &self and is therefore safe to call concurrently
from multiple threads through an Arc<ConcurrentSegmentManager>.
§Concurrency
Each successful call returns a distinct index. Once all segments have been
issued, subsequent calls return None (the counter keeps growing but is
bounded by the number of callers, so it cannot overflow in practice).
Sourcepub fn reset_allocation_index(&self)
pub fn reset_allocation_index(&self)
Reset the allocation index to 0.
This is useful for retry scenarios where segments that previously failed need to be reconsidered for assignment from the beginning of the vector.
§Safety of use
This must not be called concurrently with allocate_next_index
if unique indices are required; it is intended for reset points where the
caller has exclusive access (e.g. between download attempts).
pub fn complete_segment(&mut self, index: u32, data: Bytes) -> bool
pub fn fail_segment(&mut self, index: u32) -> Option<usize>
pub fn is_complete(&self) -> bool
pub fn has_failed_segments(&self) -> bool
pub fn has_pending_segments(&self) -> bool
pub fn completed_ranges(&self) -> Vec<(u64, u64)>
pub fn assemble(&self) -> Option<Vec<u8>>
pub fn progress(&self) -> f64
pub fn num_segments(&self) -> usize
pub fn segment_status(&self, index: usize) -> Option<SegmentStatus>
pub fn num_mirrors(&self) -> usize
pub fn total_size(&self) -> u64
pub fn completed_bytes(&self) -> u64
pub fn mirror_url(&self, index: usize) -> Option<&str>
pub fn available_mirrors(&self) -> Vec<usize>
pub fn any_mirror_available(&self) -> bool
pub fn set_max_connections_per_mirror(&mut self, max: usize)
Sourcepub fn set_mirror_max_connections(&mut self, mirror_idx: usize, max: usize)
pub fn set_mirror_max_connections(&mut self, mirror_idx: usize, max: usize)
Set the maximum connections for a specific mirror.
This is used for dynamic connection rebalancing based on mirror performance.
§Arguments
mirror_idx- Index of the mirrormax- New maximum connections for this mirror
Sourcepub fn get_mirror_max_connections(&self, mirror_idx: usize) -> Option<usize>
pub fn get_mirror_max_connections(&self, mirror_idx: usize) -> Option<usize>
Get the current maximum connections for a specific mirror.
§Arguments
mirror_idx- Index of the mirror
pub fn set_max_retries(&mut self, retries: u32)
pub fn segment_retry_count(&self, seg_idx: u32) -> u32
pub fn has_permanently_failed_segments(&self) -> bool
pub fn mark_completed_up_to(&mut self, offset: u64, length: u64)
pub fn segment_info(&self, index: usize) -> Option<(u64, u64, &SegmentStatus)>
Sourcepub fn select_mirror_for_next_segment(
&mut self,
) -> Option<(usize, (u32, u64, u64))>
pub fn select_mirror_for_next_segment( &mut self, ) -> Option<(usize, (u32, u64, u64))>
Select a mirror for the next pending segment using UriSelector.
If a UriSelector is configured, this method uses it to intelligently
select the best mirror based on historical performance data. Otherwise,
it falls back to the first available mirror.
§Returns
Some((mirror_idx, segment_info))- Mirror index and segment info (index, offset, length)None- No pending segments or no available mirrors
Sourcepub fn report_segment_complete(
&mut self,
seg_idx: u32,
data: Bytes,
bytes_per_sec: u64,
is_multi_connection: bool,
) -> bool
pub fn report_segment_complete( &mut self, seg_idx: u32, data: Bytes, bytes_per_sec: u64, is_multi_connection: bool, ) -> bool
Report a segment download completion with speed feedback.
This method should be called after a segment is successfully downloaded. It updates the server statistics with the measured download speed, which affects future mirror selection decisions.
§Arguments
seg_idx- Index of the completed segmentdata- Downloaded databytes_per_sec- Measured download speed in bytes per secondis_multi_connection- Whether this was a multi-connection download
§Returns
true if the segment was successfully marked as complete
Sourcepub fn report_segment_failed(
&mut self,
seg_idx: u32,
error_code: u16,
) -> Option<usize>
pub fn report_segment_failed( &mut self, seg_idx: u32, error_code: u16, ) -> Option<usize>
Report a segment download failure.
This method should be called when a segment download fails. It updates the server statistics and may disable the mirror if it has too many consecutive failures.
§Arguments
seg_idx- Index of the failed segmenterror_code- HTTP error code (e.g., 500, 503) or 0 for network errors
§Returns
Some(new_mirror_idx)- Segment reassigned to a new mirrorNone- Segment permanently failed or no alternative mirror
Sourcepub fn get_mirror_url(&self, mirror_idx: usize) -> Option<&str>
pub fn get_mirror_url(&self, mirror_idx: usize) -> Option<&str>
Get the URL for a specific mirror index.
Sourcepub fn mirror_active_segments(&self, mirror_idx: usize) -> usize
pub fn mirror_active_segments(&self, mirror_idx: usize) -> usize
Get the number of active segments for a specific mirror.
Sourcepub fn has_intelligent_selection(&self) -> bool
pub fn has_intelligent_selection(&self) -> bool
Check if the manager is using intelligent mirror selection.