pub struct AppState { /* private fields */ }Expand description
Shared application state for metrics and status tracking.
This struct holds all the counters and state needed for the status endpoint. All fields are thread-safe and can be accessed concurrently.
§Thread Safety
start_time: Immutable after creationcaptures_processed: AtomicU64 for lock-free incrementsactive_sse_connections: AtomicU64 for connection trackinglatency_histogram: RwLock-wrapped for efficient reads
§Usage
use std::sync::Arc;
use reasonkit_web::handlers::AppState;
let state = Arc::new(AppState::new());
// Record a capture
state.record_capture();
// Track SSE connection
state.increment_sse_connections();
// ... later
state.decrement_sse_connections();
// Record request latency
state.record_latency_us(12500); // 12.5msImplementations§
Source§impl AppState
impl AppState
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new AppState instance with initial values.
The start time is set to the current instant.
Sourcepub fn uptime_seconds(&self) -> u64
pub fn uptime_seconds(&self) -> u64
Get the server uptime in seconds.
Sourcepub fn start_time(&self) -> Instant
pub fn start_time(&self) -> Instant
Get the server start time.
Sourcepub fn captures_processed(&self) -> u64
pub fn captures_processed(&self) -> u64
Get the total number of captures processed.
Sourcepub fn record_capture(&self) -> u64
pub fn record_capture(&self) -> u64
Increment the capture counter and return the new value.
Sourcepub fn active_sse_connections(&self) -> u64
pub fn active_sse_connections(&self) -> u64
Get the number of active SSE connections.
Sourcepub fn increment_sse_connections(&self) -> u64
pub fn increment_sse_connections(&self) -> u64
Increment the SSE connection counter.
Sourcepub fn decrement_sse_connections(&self) -> u64
pub fn decrement_sse_connections(&self) -> u64
Decrement the SSE connection counter.
Uses saturating subtraction to prevent underflow.
Sourcepub fn record_latency_us(&self, latency_us: u64)
pub fn record_latency_us(&self, latency_us: u64)
Record a request latency in microseconds.
Sourcepub fn record_latency(&self, duration: Duration)
pub fn record_latency(&self, duration: Duration)
Record a request latency duration.
Sourcepub fn latency_metrics(&self) -> LatencyMetrics
pub fn latency_metrics(&self) -> LatencyMetrics
Get the latency metrics.
Sourcepub fn total_requests(&self) -> u64
pub fn total_requests(&self) -> u64
Get the total number of requests processed.
Sourcepub fn record_error(&self) -> u64
pub fn record_error(&self) -> u64
Record an error.
Sourcepub fn error_count(&self) -> u64
pub fn error_count(&self) -> u64
Get the total error count.
Sourcepub fn reset_metrics(&self)
pub fn reset_metrics(&self)
Reset all metrics (useful for testing).
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for AppState
impl !RefUnwindSafe for AppState
impl Send for AppState
impl Sync for AppState
impl Unpin for AppState
impl UnwindSafe for AppState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more