Skip to main content

SessionPersistence

Struct SessionPersistence 

Source
pub struct SessionPersistence { /* private fields */ }
Expand description

High-level session persistence manager

Coordinates saving and loading of download session state using the ResumeData JSON format. Manages both individual command states (.aria2 files) and global session options.

§Examples

use aria2_core::session::session_persistence::SessionPersistence;
use std::path::Path;

let session = SessionPersistence::new(Path::new("/tmp/aria2_session"));

// Save current state
let count = session.save_state(&groups).await?;
println!("Saved {} downloads", count);

// Load saved state
let count = session.load_state(&mut groups).await?;
println!("Restored {} downloads", count);

Implementations§

Source§

impl SessionPersistence

Source

pub fn new(session_dir: &Path) -> Self

Create a new SessionPersistence instance

§Arguments
  • session_dir - Directory path for storing .aria2 session files
Source

pub fn with_interval(self, interval_secs: u64) -> Self

Create with custom auto-save interval

Source

pub fn without_auto_save(self) -> Self

Disable auto-save (only manual save/load)

Set cookie jar for persistence alongside session data

Source

pub fn cookie_jar_mut(&mut self) -> Option<&mut CookieJar>

Get mutable reference to the cookie jar for adding cookies before saving

Source

pub fn cookie_jar(&self) -> Option<&CookieJar>

Get reference to the cookie jar

Source

pub fn session_dir(&self) -> &Path

Get the session directory path

Source

pub async fn save_state( &self, groups: &[Arc<RwLock<RequestGroup>>], ) -> Result<usize, String>

Save all active/paused/stopped command states to the session directory

Iterates through all RequestGroups, converts each to ResumeData, and writes individual .aria2 files. Also saves global options.

§Arguments
  • groups - Slice of active download groups to persist
§Returns
  • Ok(usize) - Number of successfully saved commands
  • Err(String) - Error message if critical failure occurs
§File Format

Each command is saved as {gid}.aria2 in JSON format. Global options are saved as session_options.json.

Source

pub async fn load_state( &mut self, groups: &mut Vec<Arc<RwLock<RequestGroup>>>, ) -> Result<usize, String>

Load saved states from session directory and restore paused commands

Reads all .aria2 files from the session directory, deserializes them, and creates paused download commands for each valid entry.

§Arguments
  • groups - Mutable reference to the groups vector to restore into
§Returns
  • Ok(usize) - Number of successfully restored commands
  • Err(String) - Error message if critical failure occurs
§Graceful Error Handling
  • Missing session directory returns Ok(0) (not an error)
  • Corrupt/malformed .aria2 files are skipped with a warning
  • Partial restoration is allowed (some files may fail)
Source

pub fn start_auto_save( &self, groups: Arc<RwLock<Vec<Arc<RwLock<RequestGroup>>>>>, ) -> Option<JoinHandle<()>>

Start background auto-save task

Spawns a Tokio task that periodically calls save_state(). The task runs until the returned handle is dropped or cancelled.

§Arguments
  • groups - Arc-wrapped shared reference to the groups vector
§Returns

A JoinHandle that can be used to cancel the auto-save task

Source

pub async fn cleanup(&self) -> Result<(), String>

Clean up all session files (for testing or reset)

Source

pub async fn save_server_stats( &self, stat_man: &ServerStatMan, ) -> Result<usize, String>

Save server statistics to the session directory.

Persists all server performance statistics (download speeds, error counts, etc.) to a JSON file in the session directory. This allows the adaptive URI selector to remember server performance across restarts.

§Arguments
  • stat_man - Reference to the ServerStatMan to save
§Returns
  • Ok(usize) - Number of server stats saved
  • Err(String) - Error message if save fails
§File Location

Stats are saved to {session_dir}/server-stat.json

§Example
use aria2_core::session::session_persistence::SessionPersistence;
use aria2_core::selector::server_stat_man::ServerStatMan;

let persistence = SessionPersistence::new(Path::new("/tmp/aria2_session"));
let stat_man = ServerStatMan::new();
stat_man.update("fast.mirror.com", 10000, false);

let saved = persistence.save_server_stats(&stat_man).await?;
println!("Saved {} server stats", saved);
Source

pub async fn load_server_stats( &self, stat_man: &ServerStatMan, ) -> Result<usize, String>

Load server statistics from the session directory.

Restores previously saved server performance statistics from a JSON file in the session directory. This allows the adaptive URI selector to make informed decisions immediately after startup.

§Arguments
  • stat_man - Reference to the ServerStatMan to load into
§Returns
  • Ok(usize) - Number of server stats loaded
  • Err(String) - Error message if load fails
§Behavior
  • Returns Ok(0) if no server-stat.json file exists (not an error)
  • Returns error if file exists but is invalid
  • Merges with existing stats (doesn’t clear current stats)
§Example
use aria2_core::session::session_persistence::SessionPersistence;
use aria2_core::selector::server_stat_man::ServerStatMan;

let persistence = SessionPersistence::new(Path::new("/tmp/aria2_session"));
let stat_man = ServerStatMan::new();

let loaded = persistence.load_server_stats(&stat_man).await?;
println!("Loaded {} server stats from previous session", loaded);
Source

pub async fn save_active_only( &self, groups: &[Arc<RwLock<RequestGroup>>], ) -> Result<usize, String>

Save only active/in-progress downloads (skip completed/stopped/error).

Filters groups by download status, persisting only those that are actively downloading or waiting in queue.

§Arguments
  • groups - Slice of all download groups to filter
§Returns
  • Ok(usize) - Number of active downloads successfully saved
  • Err(String) - Error message if critical failure occurs
Source

pub async fn save_completed( &self, groups: &[Arc<RwLock<RequestGroup>>], ) -> Result<usize, String>

Save only completed downloads for archival.

Filters groups by completion status, persisting only finished downloads. Useful for creating archives of successful downloads separate from active/pending work.

§Arguments
  • groups - Slice of all download groups to filter
§Returns
  • Ok(usize) - Number of completed downloads successfully saved
  • Err(String) - Error message if critical failure occurs

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