pipey 0.2.1

A lightweight HTTP-to-WebSocket event delivery service.
Documentation
use crate::tasks::HeartbeatRegistry;
use arifa::prelude::*;
use std::sync::Arc;
use std::time::Duration;

#[derive(Clone)]
pub struct AppState {
    pub arifa: Arc<Arifa>,
    pub heartbeats: HeartbeatRegistry<String>,
}

impl AppState {
    pub async fn new<T: Into<String>>(node_id: T, redis_url: &str) -> Self {
        let node_id: String = node_id.into();
        let arifa = Arc::new(
            Arifa::new(redis_url, &node_id)
                .await
                .expect("Failed to init Arifa"),
        );

        let heartbeats = HeartbeatRegistry::new();
        // Sweep every 15s, evict if no pong in 60s. One task for ALL connections.
        heartbeats.spawn_sweeper(Duration::from_secs(15), Duration::from_secs(60));

        Self { arifa, heartbeats }
    }

    /// Shuts down Arifa's background router and forwarding tasks
    pub async fn shutdown(&self) {
        self.arifa.shutdown().await;
    }
}