alibabacloud-rum 0.1.0

Alibaba Cloud RUM SDK for native Rust applications.
Documentation
use std::time::{Duration, Instant, SystemTime};

use uuid::Uuid;

use super::uuid_string;

const SESSION_INACTIVITY_TIMEOUT: Duration = Duration::from_secs(15 * 60);

pub(crate) struct SessionState {
    session_id: Uuid,
    #[allow(dead_code)]
    started_at: SystemTime,
    last_activity_at: Instant,
    #[allow(dead_code)]
    active: bool,
}

impl SessionState {
    pub fn new() -> Self {
        Self {
            session_id: Uuid::new_v4(),
            started_at: SystemTime::now(),
            last_activity_at: Instant::now(),
            active: true,
        }
    }

    pub fn touch(&mut self) {
        let now = Instant::now();
        if now.duration_since(self.last_activity_at) > SESSION_INACTIVITY_TIMEOUT {
            self.session_id = Uuid::new_v4();
            self.started_at = SystemTime::now();
        }
        self.last_activity_at = now;
    }

    pub fn id_string(&self) -> String {
        uuid_string(self.session_id)
    }
}