Skip to main content

ActiveSessionManager

Struct ActiveSessionManager 

Source
pub struct ActiveSessionManager {
    pub session_path: PathBuf,
    pub auto_save_interval: Duration,
    /* private fields */
}
Expand description

活跃会话管理器 - 负责会话的加载、保存和自动保存

Fields§

§session_path: PathBuf

会话文件路径

§auto_save_interval: Duration

自动保存间隔

Implementations§

Source§

impl ActiveSessionManager

Source

pub fn new(session_path: PathBuf, auto_save_interval: Duration) -> Self

创建新的活跃会话管理器

§参数
  • session_path: 会话文件保存路径
  • auto_save_interval: 自动保存的时间间隔
§示例
let manager = ActiveSessionManager::new(
    PathBuf::from("/tmp/session.txt"),
    Duration::from_secs(60),
);
Source

pub async fn load_session(&self) -> Result<Vec<SessionEntry>, String>

从文件加载会话数据

如果文件不存在,返回空的 Vec(不视为错误)

§返回值
  • Ok(Vec<SessionEntry>): 成功加载的会话条目列表
  • Err(String): 加载失败时的错误信息
Source

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

保存所有下载组的状态到会话文件

使用原子写入策略:先写入临时文件 (.sess.tmp),然后重命名为目标文件。 这确保在写入过程中如果发生崩溃,不会损坏原有的会话文件。

§参数
  • groups: 需要保存状态的下载组列表
§返回值
  • Ok(usize): 成功保存的条目数量
  • Err(String): 保存失败时的错误信息
Source

pub fn mark_dirty(&self)

标记会话为脏状态(有未保存的更改)

Source

pub fn is_dirty(&self) -> bool

检查会话是否有未保存的更改

Source

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

启动自动保存后台任务

在后台循环中定期检查脏标记,如果有未保存的更改则自动保存。 该方法会在后台启动一个 Tokio 任务,不会阻塞当前线程。

§参数
  • self: 必须通过 Arc 包装,以便在后台任务中共享
  • groups: 所有活动下载组的共享引用
§注意事项
  • 此方法会启动一个无限循环的后台任务
  • 只有当 dirty flag 为 true 时才会执行保存操作
  • 保存成功后会清除 dirty flag

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