Skip to main content

ConcurrentSegmentManager

Struct ConcurrentSegmentManager 

Source
pub struct ConcurrentSegmentManager { /* private fields */ }

Implementations§

Source§

impl ConcurrentSegmentManager

Source

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.

Source

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 bytes
  • urls - List of mirror URLs
  • segment_size - Optional segment size (default: 1 MB)
  • stat_man - Shared server statistics manager
  • uri_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,
);
Source

pub fn allocate_segments(&mut self)

Source

pub fn next_pending_segment_for_mirror( &mut self, mirror_idx: usize, ) -> Option<(u32, u64, u64)>

Source

pub fn next_pending_segment(&mut self) -> Option<(u32, u64, u64)>

Source

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).

Source

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).

Source

pub fn complete_segment(&mut self, index: u32, data: Bytes) -> bool

Source

pub fn fail_segment(&mut self, index: u32) -> Option<usize>

Source

pub fn is_complete(&self) -> bool

Source

pub fn has_failed_segments(&self) -> bool

Source

pub fn has_pending_segments(&self) -> bool

Source

pub fn completed_ranges(&self) -> Vec<(u64, u64)>

Source

pub fn assemble(&self) -> Option<Vec<u8>>

Source

pub fn progress(&self) -> f64

Source

pub fn num_segments(&self) -> usize

Source

pub fn segment_status(&self, index: usize) -> Option<SegmentStatus>

Source

pub fn num_mirrors(&self) -> usize

Source

pub fn total_size(&self) -> u64

Source

pub fn completed_bytes(&self) -> u64

Source

pub fn mirror_url(&self, index: usize) -> Option<&str>

Source

pub fn available_mirrors(&self) -> Vec<usize>

Source

pub fn any_mirror_available(&self) -> bool

Source

pub fn set_max_connections_per_mirror(&mut self, max: usize)

Source

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 mirror
  • max - New maximum connections for this mirror
Source

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
Source

pub fn set_max_retries(&mut self, retries: u32)

Source

pub fn segment_retry_count(&self, seg_idx: u32) -> u32

Source

pub fn has_permanently_failed_segments(&self) -> bool

Source

pub fn mark_completed_up_to(&mut self, offset: u64, length: u64)

Source

pub fn segment_info(&self, index: usize) -> Option<(u64, u64, &SegmentStatus)>

Source

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
Source

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 segment
  • data - Downloaded data
  • bytes_per_sec - Measured download speed in bytes per second
  • is_multi_connection - Whether this was a multi-connection download
§Returns

true if the segment was successfully marked as complete

Source

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 segment
  • error_code - HTTP error code (e.g., 500, 503) or 0 for network errors
§Returns
  • Some(new_mirror_idx) - Segment reassigned to a new mirror
  • None - Segment permanently failed or no alternative mirror
Source

pub fn get_mirror_url(&self, mirror_idx: usize) -> Option<&str>

Get the URL for a specific mirror index.

Source

pub fn mirror_active_segments(&self, mirror_idx: usize) -> usize

Get the number of active segments for a specific mirror.

Source

pub fn has_intelligent_selection(&self) -> bool

Check if the manager is using intelligent mirror selection.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more