rtflow-core 0.7.0

Errors, identifiers, events and metric counters shared by the rtflow crates
Documentation
//! Allocation-conscious control-plane events.
use crate::{
    error::{ErrorCode, Operation},
    identity::{MountId, SessionId, TaskId},
};
use std::{any::Any, fmt, net::SocketAddr, sync::Arc, time::Duration};

#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum FrameworkEvent {
    Server(ServerEvent),
    Session(SessionEvent),
    Mount(MountEvent),
    Proxy(ProxyEvent),
    Client(ClientEvent),
    Pipeline(PipelineEvent),
    Runtime(RuntimeEvent),
    Security(SecurityEvent),
}

#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ServerEvent {
    Started { local_addr: SocketAddr },
    Stopping,
    Stopped,
    AcceptFailed { error: Arc<str> },
}

#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum SessionEvent {
    Accepted {
        id: SessionId,
        peer: SocketAddr,
    },
    Rejected {
        peer: SocketAddr,
        reason: RejectReason,
    },
    Closed {
        id: SessionId,
        peer: SocketAddr,
        elapsed: Duration,
        error: Option<ErrorCode>,
    },
    /// A playback session ended (TEARDOWN, PAUSE or disconnect) after having
    /// played `mount`. Emitted once per distinct mount a session played.
    PlaybackStopped {
        peer: SocketAddr,
        mount: MountId,
        elapsed: Duration,
    },
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RejectReason {
    GlobalLimit,
    PeerLimit,
    AccessDenied,
    RateLimited,
    ShuttingDown,
}

#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum MountEvent {
    Registered { id: MountId },
    Unregistered { id: MountId },
}

#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ProxyEvent {
    Connecting {
        mount: MountId,
        attempt: u32,
    },
    Streaming {
        mount: MountId,
    },
    Backoff {
        mount: MountId,
        attempt: u32,
        delay: Duration,
    },
    Failed {
        mount: MountId,
        code: ErrorCode,
    },
}

/// RTSP client lifecycle events (receive/play sessions).
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ClientEvent {
    Connected {
        id: SessionId,
        transport: &'static str,
        peer: SocketAddr,
    },
    Fallback {
        id: SessionId,
        from: &'static str,
        to: &'static str,
    },
    ReconnectBackoff {
        id: SessionId,
        attempt: u32,
        delay: Duration,
    },
    KeepAliveFailed {
        id: SessionId,
        consecutive: u32,
    },
    Redirect {
        id: SessionId,
        location: String,
    },
    Failed {
        id: SessionId,
        code: ErrorCode,
    },
    Closed {
        id: SessionId,
        elapsed: Duration,
        error: Option<ErrorCode>,
    },
}

#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum PipelineEvent {
    ElementFailed { element: Arc<str>, code: ErrorCode },
}

#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum RuntimeEvent {
    TaskStarted {
        id: TaskId,
        name: Arc<str>,
    },
    TaskFinished {
        id: TaskId,
        name: Arc<str>,
    },
    TaskFailed {
        id: TaskId,
        name: Arc<str>,
        operation: Operation,
        code: ErrorCode,
    },
    TaskPanicked {
        id: TaskId,
        name: Arc<str>,
    },
    TaskCancelled {
        id: TaskId,
        name: Arc<str>,
    },
    TaskAborted {
        id: TaskId,
        name: Arc<str>,
    },
}

#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum SecurityEvent {
    AuthenticationFailed {
        peer: SocketAddr,
        mount: Option<MountId>,
    },
    RateLimited {
        peer: SocketAddr,
    },
    TlsHandshakeFailed {
        peer: SocketAddr,
        error: Arc<str>,
    },
}

pub trait EventSink: Send + Sync + Any + 'static {
    fn record(&self, event: &FrameworkEvent);
    /// Downcast support so infrastructure can extract concrete sinks
    /// (e.g. the metrics registry from a [`MetricsEventSink`](crate::metrics::MetricsEventSink)).
    fn as_any(&self) -> &dyn Any;
}

#[derive(Default)]
pub struct NoopEventSink;
impl EventSink for NoopEventSink {
    fn record(&self, _: &FrameworkEvent) {}
    fn as_any(&self) -> &dyn Any {
        self
    }
}

#[derive(Default)]
pub struct TracingEventSink;
impl EventSink for TracingEventSink {
    fn record(&self, event: &FrameworkEvent) {
        tracing::info!(target: "rtflow::event", event = ?event, "framework event");
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl fmt::Debug for dyn EventSink {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("EventSink")
    }
}

/// Deliver one framework event to a sink (cross-crate event plumbing).
#[doc(hidden)]
pub fn emit(sink: &Arc<dyn EventSink>, event: FrameworkEvent) {
    sink.record(&event);
}